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