Aedis 1.3.1  
A redis client designed for performance and scalability
serialization.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/asio.hpp>
8#if defined(BOOST_ASIO_HAS_CO_AWAIT)
9#include <boost/asio/experimental/awaitable_operators.hpp>
10#include <boost/json.hpp>
11#include <aedis.hpp>
12#include <algorithm>
13#include <cstdint>
14#include <iostream>
15#include <set>
16#include <iterator>
17#include <string>
18#include "common/common.hpp"
19
20// Include this in no more than one .cpp file.
21#include <boost/json/src.hpp>
22
23namespace net = boost::asio;
24using namespace net::experimental::awaitable_operators;
26using aedis::adapt;
27using namespace boost::json;
28
29struct user {
30 std::string name;
31 std::string age;
32 std::string country;
33
34 friend auto operator<(user const& a, user const& b)
35 {
36 return std::tie(a.name, a.age, a.country) < std::tie(b.name, b.age, b.country);
37 }
38
39 friend auto operator<<(std::ostream& os, user const& u) -> std::ostream&
40 {
41 os << "Name: " << u.name << "\n"
42 << "Age: " << u.age << "\n"
43 << "Country: " << u.country;
44
45 return os;
46 }
47};
48
49// Boost.Json serialization.
50void tag_invoke(value_from_tag, value& jv, user const& u)
51{
52 jv =
53 { {"name", u.name}
54 , {"age", u.age}
55 , {"country", u.country}
56 };
57}
58
59template<class T>
60void extract(object const& obj, T& t, boost::string_view key)
61{
62 t = value_to<T>(obj.at(key));
63}
64
65auto tag_invoke(value_to_tag<user>, value const& jv)
66{
67 user u;
68 object const& obj = jv.as_object();
69 extract(obj, u.name, "name");
70 extract(obj, u.age, "age");
71 extract(obj, u.country, "country");
72 return u;
73}
74
75// Aedis serialization
76void to_bulk(std::pmr::string& to, user const& u)
77{
78 aedis::resp3::to_bulk(to, serialize(value_from(u)));
79}
80
81void from_bulk(user& u, boost::string_view sv, boost::system::error_code&)
82{
83 value jv = parse(sv);
84 u = value_to<user>(jv);
85}
86
87net::awaitable<void> async_main()
88{
89 std::set<user> users
90 {{"Joao", "58", "Brazil"} , {"Serge", "60", "France"}};
91
92 request req;
93 req.get_config().cancel_on_connection_lost = true;
94 req.push("HELLO", 3);
95 req.push_range("SADD", "sadd-key", users); // Sends
96 req.push("SMEMBERS", "sadd-key"); // Retrieves
97 req.push("QUIT");
98
99 std::tuple<aedis::ignore, int, std::set<user>, std::string> resp;
100
101 auto conn = std::make_shared<connection>(co_await net::this_coro::executor);
102 co_await connect(conn, "127.0.0.1", "6379");
103 co_await (conn->async_run() || conn->async_exec(req, adapt(resp)));
104
105 for (auto const& e: std::get<2>(resp))
106 std::cout << e << "\n";
107}
108
109#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
Creates Redis requests.
Definition: request.hpp:176
void to_bulk(Request &to, boost::string_view data)
Adds a bulk to the request.
Definition: request.hpp:49
auto operator<<(std::ostream &os, type t) -> std::ostream &
Writes the type to the output stream.
auto adapt(std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)()) noexcept
Creates an adapter that ignores responses.
Definition: adapt.hpp:199