Boost.Redis 1.84.0 A redis client library

Loading...
Searching...
No Matches
cpp20_json.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/asio/deferred.hpp>
9#include <boost/asio/detached.hpp>
10#include <boost/describe.hpp>
11#include <boost/asio/consign.hpp>
12#include <boost/asio/use_awaitable.hpp>
13#include <string>
14#include <iostream>
15
16#if defined(BOOST_ASIO_HAS_CO_AWAIT)
17
18#include <boost/json/serialize.hpp>
19#include <boost/json/parse.hpp>
20#include <boost/json/value_from.hpp>
21#include <boost/json/value_to.hpp>
22#include <boost/redis/resp3/serialization.hpp>
23
24 namespace asio = boost::asio;
25 using namespace boost::describe;
26 using boost::redis::request;
27 using boost::redis::response;
28 using boost::redis::ignore_t;
29 using boost::redis::config;
30 using boost::redis::connection;
31
32// Struct that will be stored in Redis using json serialization.
33 struct user {
34 std::string name;
35 std::string age;
36 std::string country;
37};
38
39// The type must be described for serialization to work.
40BOOST_DESCRIBE_STRUCT(user, (), (name, age, country))
41
42// Boost.Redis customization points (example/json.hpp)
43 void boost_redis_to_bulk(std::string& to, user const& u)
44 { boost::redis::resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u))); }
45
46 void boost_redis_from_bulk(user& u, std::string_view sv, boost::system::error_code&)
47 { u = boost::json::value_to<user>(boost::json::parse(sv)); }
48
49 auto co_main(config cfg) -> asio::awaitable<void>
50{
51 auto ex = co_await asio::this_coro::executor;
52 auto conn = std::make_shared<connection>(ex);
53 conn->async_run(cfg, {}, asio::consign(asio::detached, conn));
54
55 // user object that will be stored in Redis in json format.
56 user const u{"Joao", "58", "Brazil"};
57
58 // Stores and retrieves in the same request.
59 request req;
60 req.push("SET", "json-key", u); // Stores in Redis.
61 req.push("GET", "json-key"); // Retrieves from Redis.
62
63 response<ignore_t, user> resp;
64
65 co_await conn->async_exec(req, resp, asio::deferred);
66 conn->cancel();
67
68 // Prints the first ping
69 std::cout
70 << "Name: " << std::get<1>(resp).value().name << "\n"
71 << "Age: " << std::get<1>(resp).value().age << "\n"
72 << "Country: " << std::get<1>(resp).value().country << "\n";
73}
74
75#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::config
Configure parameters used by the connection classes.
Definition: config.hpp:30