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