Boost.Redis 1.4.2
A redis client library
Loading...
Searching...
No Matches
cpp20_protobuf.cpp
1/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#include <boost/redis/connection.hpp>
8#include <boost/redis/resp3/serialization.hpp>
9#include <boost/asio/deferred.hpp>
10#include <boost/asio/co_spawn.hpp>
11#include <boost/asio/detached.hpp>
12#include <boost/asio/consign.hpp>
13#include <boost/system/errc.hpp>
14#include <iostream>
15
16// See the definition in person.proto. This header is automatically
17// generated by CMakeLists.txt.
18#include "person.pb.h"
19
20#if defined(BOOST_ASIO_HAS_CO_AWAIT)
21
22namespace asio = boost::asio;
29
30// The protobuf type described in example/person.proto
31using tutorial::person;
32
33// Boost.Redis customization points (example/protobuf.hpp)
34namespace tutorial
35{
36
37// Below I am using a Boost.Redis to indicate a protobuf error, this
38// is ok for an example, users however might want to define their own
39// error codes.
40void boost_redis_to_bulk(std::string& to, person const& u)
41{
42 std::string tmp;
43 if (!u.SerializeToString(&tmp))
44 throw boost::system::system_error(boost::redis::error::invalid_data_type);
45
46 boost::redis::resp3::boost_redis_to_bulk(to, tmp);
47}
48
49void boost_redis_from_bulk(person& u, std::string_view sv, boost::system::error_code& ec)
50{
51 std::string const tmp {sv};
52 if (!u.ParseFromString(tmp))
54}
55
56} // tutorial
57
58using tutorial::boost_redis_to_bulk;
59using tutorial::boost_redis_from_bulk;
60
61asio::awaitable<void> co_main(config cfg)
62{
63 auto ex = co_await asio::this_coro::executor;
64 auto conn = std::make_shared<connection>(ex);
65 conn->async_run(cfg, {}, asio::consign(asio::detached, conn));
66
67 person p;
68 p.set_name("Louis");
69 p.set_id(3);
70 p.set_email("No email yet.");
71
72 request req;
73 req.push("SET", "protobuf-key", p);
74 req.push("GET", "protobuf-key");
75
76 response<ignore_t, person> resp;
77
78 // Sends the request and receives the response.
79 co_await conn->async_exec(req, resp, asio::deferred);
80 conn->cancel();
81
82 std::cout
83 << "Name: " << std::get<1>(resp).value().name() << "\n"
84 << "Age: " << std::get<1>(resp).value().id() << "\n"
85 << "Email: " << std::get<1>(resp).value().email() << "\n";
86}
87
88#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Definition: connection.hpp:337
Creates Redis requests.
Definition: request.hpp:46
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition: ignore.hpp:31
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition: response.hpp:25
operation
Connection operations that can be cancelled.
Definition: operation.hpp:18
@ invalid_data_type
Invalid RESP3 type.
Configure parameters used by the connection classes.
Definition: config.hpp:30