Loading...
Searching...
No Matches
cpp20_containers.cpp
1/* Copyright (c) 2018-2025 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
9#include <boost/asio/co_spawn.hpp>
10#include <boost/asio/detached.hpp>
11
12#include <iostream>
13#include <map>
14#include <vector>
15
16#if defined(BOOST_ASIO_HAS_CO_AWAIT)
17
18namespace asio = boost::asio;
25using boost::asio::awaitable;
26using boost::asio::detached;
27using boost::asio::consign;
28
29template <class T>
30std::ostream& operator<<(std::ostream& os, std::optional<T> const& opt)
31{
32 if (opt.has_value())
33 std::cout << opt.value();
34 else
35 std::cout << "null";
36
37 return os;
38}
39
40void print(std::map<std::string, std::string> const& cont)
41{
42 for (auto const& e : cont)
43 std::cout << e.first << ": " << e.second << "\n";
44}
45
46template <class T>
47void print(std::vector<T> const& cont)
48{
49 for (auto const& e : cont)
50 std::cout << e << " ";
51 std::cout << "\n";
52}
53
54// Stores the content of some STL containers in Redis.
55auto store(std::shared_ptr<connection> conn) -> awaitable<void>
56{
57 std::vector<int> vec{1, 2, 3, 4, 5, 6};
58
59 std::map<std::string, std::string> map{
60 {"key1", "value1"},
61 {"key2", "value2"},
62 {"key3", "value3"}
63 };
64
65 request req;
66 req.push_range("RPUSH", "rpush-key", vec);
67 req.push_range("HSET", "hset-key", map);
68 req.push("SET", "key", "value");
69
70 co_await conn->async_exec(req, ignore);
71}
72
73auto hgetall(std::shared_ptr<connection> conn) -> awaitable<void>
74{
75 // A request contains multiple commands.
76 request req;
77 req.push("HGETALL", "hset-key");
78
79 // Responses as tuple elements.
80 response<std::map<std::string, std::string>> resp;
81
82 // Executes the request and reads the response.
83 co_await conn->async_exec(req, resp);
84
85 print(std::get<0>(resp).value());
86}
87
88auto mget(std::shared_ptr<connection> conn) -> awaitable<void>
89{
90 // A request contains multiple commands.
91 request req;
92 req.push("MGET", "key", "non-existing-key");
93
94 // Responses as tuple elements.
95 response<std::vector<std::optional<std::string>>> resp;
96
97 // Executes the request and reads the response.
98 co_await conn->async_exec(req, resp);
99
100 print(std::get<0>(resp).value());
101}
102
103// Retrieves in a transaction.
104auto transaction(std::shared_ptr<connection> conn) -> awaitable<void>
105{
106 request req;
107 req.push("MULTI");
108 req.push("LRANGE", "rpush-key", 0, -1); // Retrieves
109 req.push("HGETALL", "hset-key"); // Retrieves
110 req.push("MGET", "key", "non-existing-key");
111 req.push("EXEC");
112
113 response<
114 ignore_t, // multi
115 ignore_t, // lrange
116 ignore_t, // hgetall
117 ignore_t, // mget
118 response<
119 std::optional<std::vector<int>>,
120 std::optional<std::map<std::string, std::string>>,
121 std::optional<std::vector<std::optional<std::string>>>> // exec
122 >
123 resp;
124
125 co_await conn->async_exec(req, resp);
126
127 print(std::get<0>(std::get<4>(resp).value()).value().value());
128 print(std::get<1>(std::get<4>(resp).value()).value().value());
129 print(std::get<2>(std::get<4>(resp).value()).value().value());
130}
131
132// Called from the main function (see main.cpp)
133awaitable<void> co_main(config cfg)
134{
135 auto conn = std::make_shared<connection>(co_await asio::this_coro::executor);
136 conn->async_run(cfg, consign(detached, conn));
137
138 co_await store(conn);
139 co_await transaction(conn);
140 co_await hgetall(conn);
141 co_await mget(conn);
142 conn->cancel();
143}
144
145#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Creates Redis requests.
Definition request.hpp:46
ignore_t ignore
Global ignore object.
auto operator<<(std::ostream &os, type t) -> std::ostream &
Writes the type to the output stream.
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition ignore.hpp:30
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition response.hpp:25
Configure parameters used by the connection classes.
Definition config.hpp:30