boost_redis 1.4.2
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/redis/connection.hpp>
8#include <boost/asio/deferred.hpp>
9#include <boost/asio/detached.hpp>
10#include <boost/asio/co_spawn.hpp>
11#include <map>
12#include <vector>
13#include <iostream>
14
15#if defined(BOOST_ASIO_HAS_CO_AWAIT)
16
17namespace net = boost::asio;
24
25void print(std::map<std::string, std::string> const& cont)
26{
27 for (auto const& e: cont)
28 std::cout << e.first << ": " << e.second << "\n";
29}
30
31void print(std::vector<int> const& cont)
32{
33 for (auto const& e: cont) std::cout << e << " ";
34 std::cout << "\n";
35}
36
37// Stores the content of some STL containers in Redis.
38auto store(std::shared_ptr<connection> conn) -> net::awaitable<void>
39{
40 std::vector<int> vec
41 {1, 2, 3, 4, 5, 6};
42
43 std::map<std::string, std::string> map
44 {{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}};
45
46 request req;
47 req.push_range("RPUSH", "rpush-key", vec);
48 req.push_range("HSET", "hset-key", map);
49
50 co_await conn->async_exec(req, ignore, net::deferred);
51}
52
53auto hgetall(std::shared_ptr<connection> conn) -> net::awaitable<void>
54{
55 // A request contains multiple commands.
56 request req;
57 req.push("HGETALL", "hset-key");
58
59 // Responses as tuple elements.
60 response<std::map<std::string, std::string>> resp;
61
62 // Executes the request and reads the response.
63 co_await conn->async_exec(req, resp, net::deferred);
64
65 print(std::get<0>(resp).value());
66}
67
68// Retrieves in a transaction.
69auto transaction(std::shared_ptr<connection> conn) -> net::awaitable<void>
70{
71 request req;
72 req.push("MULTI");
73 req.push("LRANGE", "rpush-key", 0, -1); // Retrieves
74 req.push("HGETALL", "hset-key"); // Retrieves
75 req.push("EXEC");
76
78 ignore_t, // multi
79 ignore_t, // lrange
80 ignore_t, // hgetall
81 response<std::optional<std::vector<int>>, std::optional<std::map<std::string, std::string>>> // exec
82 > resp;
83
84 co_await conn->async_exec(req, resp, net::deferred);
85
86 print(std::get<0>(std::get<3>(resp).value()).value().value());
87 print(std::get<1>(std::get<3>(resp).value()).value().value());
88}
89
90// Called from the main function (see main.cpp)
91net::awaitable<void> co_main(config cfg)
92{
93 auto conn = std::make_shared<connection>(co_await net::this_coro::executor);
94 conn->async_run(cfg, {}, net::consign(net::detached, conn));
95
96 co_await store(conn);
97 co_await transaction(conn);
98 co_await hgetall(conn);
99 conn->cancel();
100}
101
102#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Definition: connection.hpp:308
Creates Redis requests.
Definition: request.hpp:46
ignore_t ignore
Global ignore object.
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:23
Configure parameters used by the connection classes.
Definition: config.hpp:30