Boost.Redis: example/cpp20_containers.cpp Source File

Boost.Redis 1.84.0 A redis client library

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