Aedis 1.4.1
A redis client library
cpp20_containers.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 <aedis.hpp>
11#include <map>
12#include <vector>
13
14#include "common/common.hpp"
15
16namespace net = boost::asio;
17namespace resp3 = aedis::resp3;
18using namespace net::experimental::awaitable_operators;
19using aedis::adapt;
20
21void print(std::map<std::string, std::string> const& cont)
22{
23 for (auto const& e: cont)
24 std::cout << e.first << ": " << e.second << "\n";
25}
26
27void print(std::vector<int> const& cont)
28{
29 for (auto const& e: cont) std::cout << e << " ";
30 std::cout << "\n";
31}
32
33auto run(std::shared_ptr<connection> conn, std::string host, std::string port) -> net::awaitable<void>
34{
35 co_await connect(conn, host, port);
36 co_await conn->async_run();
37}
38
39// Stores the content of some STL containers in Redis.
40auto store(std::shared_ptr<connection> conn) -> net::awaitable<void>
41{
42 std::vector<int> vec
43 {1, 2, 3, 4, 5, 6};
44
45 std::map<std::string, std::string> map
46 {{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}};
47
49 req.push("HELLO", 3);
50 req.push_range("RPUSH", "rpush-key", vec);
51 req.push_range("HSET", "hset-key", map);
52
53 co_await conn->async_exec(req);
54}
55
56auto hgetall(std::shared_ptr<connection> conn) -> net::awaitable<void>
57{
58 // A request contains multiple commands.
60 req.push("HELLO", 3);
61 req.push("HGETALL", "hset-key");
62
63 // Responses as tuple elements.
64 std::tuple<aedis::ignore, std::map<std::string, std::string>> resp;
65
66 // Executes the request and reads the response.
67 co_await conn->async_exec(req, adapt(resp));
68
69 print(std::get<1>(resp));
70}
71
72// Retrieves in a transaction.
73auto transaction(std::shared_ptr<connection> conn) -> net::awaitable<void>
74{
76 req.push("HELLO", 3);
77 req.push("MULTI");
78 req.push("LRANGE", "rpush-key", 0, -1); // Retrieves
79 req.push("HGETALL", "hset-key"); // Retrieves
80 req.push("EXEC");
81
82 std::tuple<
83 aedis::ignore, // hello
84 aedis::ignore, // multi
85 aedis::ignore, // lrange
86 aedis::ignore, // hgetall
87 std::tuple<std::optional<std::vector<int>>, std::optional<std::map<std::string, std::string>>> // exec
88 > resp;
89
90 co_await conn->async_exec(req, adapt(resp));
91
92 print(std::get<0>(std::get<4>(resp)).value());
93 print(std::get<1>(std::get<4>(resp)).value());
94}
95
96auto quit(std::shared_ptr<connection> conn) -> net::awaitable<void>
97{
99 req.push("QUIT");
100
101 co_await conn->async_exec(req);
102}
103
104// Called from the main function (see main.cpp)
105net::awaitable<void> co_main(std::string host, std::string port)
106{
107 auto ex = co_await net::this_coro::executor;
108 auto conn = std::make_shared<connection>(ex);
109 net::co_spawn(ex, run(conn, host, port), net::detached);
110 co_await store(conn);
111 co_await transaction(conn);
112 co_await hgetall(conn);
113 co_await quit(conn);
114}
115
116#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 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
adapter::detail::ignore ignore
Tag used to ignore responses.
Definition: adapt.hpp:36
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
@ run
Refers to connection::async_run operations.