Boost.Redis 1.84.0 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
22 namespace asio = boost::asio;
23 using boost::redis::request;
24 using boost::redis::response;
25 using boost::redis::operation;
26 using boost::redis::ignore_t;
27 using boost::redis::config;
28 using boost::redis::connection;
29
30// The protobuf type described in example/person.proto
31 using tutorial::person;
32
33// Boost.Redis customization points (example/protobuf.hpp)
34 namespace 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.
40 void 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
49 void 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))
53 ec = boost::redis::error::invalid_data_type;
54}
55
56} // tutorial
57
58 using tutorial::boost_redis_to_bulk;
59 using 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)
boost::redis::connection
A basic_connection that type erases the executor.
Definition: connection.hpp:339
boost::redis::request
Creates Redis requests.
Definition: request.hpp:46
boost::redis::ignore_t
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition: ignore.hpp:31
boost::redis::response
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition: response.hpp:25
boost::redis::operation
operation
Connection operations that can be cancelled.
Definition: operation.hpp:18
boost::redis::error::invalid_data_type
@ invalid_data_type
Invalid RESP3 type.
boost::redis::config
Configure parameters used by the connection classes.
Definition: config.hpp:30