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>
15#if defined(BOOST_ASIO_HAS_CO_AWAIT)
17namespace asio = boost::asio;
24using boost::asio::awaitable;
25using boost::asio::deferred;
26using boost::asio::detached;
27using boost::asio::consign;
29void print(std::map<std::string, std::string>
const& cont)
31 for (
auto const& e: cont)
32 std::cout << e.first <<
": " << e.second <<
"\n";
35void print(std::vector<int>
const& cont)
37 for (
auto const& e: cont) std::cout << e <<
" ";
42auto store(std::shared_ptr<connection> conn) -> awaitable<void>
47 std::map<std::string, std::string>
map
48 {{
"key1",
"value1"}, {
"key2",
"value2"}, {
"key3",
"value3"}};
51 req.push_range(
"RPUSH",
"rpush-key", vec);
52 req.push_range(
"HSET",
"hset-key", map);
54 co_await conn->async_exec(req, ignore, deferred);
57auto hgetall(std::shared_ptr<connection> conn) -> awaitable<void>
61 req.push(
"HGETALL",
"hset-key");
64 response<std::map<std::string, std::string>> resp;
67 co_await conn->async_exec(req, resp, deferred);
69 print(std::get<0>(resp).value());
73auto transaction(std::shared_ptr<connection> conn) -> awaitable<void>
77 req.push(
"LRANGE",
"rpush-key", 0, -1);
78 req.push(
"HGETALL",
"hset-key");
85 response<std::optional<std::vector<int>>, std::optional<std::map<std::string, std::string>>>
88 co_await conn->async_exec(req, resp, deferred);
90 print(std::get<0>(std::get<3>(resp).value()).value().value());
91 print(std::get<1>(std::get<3>(resp).value()).value().value());
95awaitable<void> co_main(config cfg)
97 auto conn = std::make_shared<connection>(
co_await asio::this_coro::executor);
98 conn->async_run(cfg, {}, consign(detached, conn));
100 co_await store(conn);
101 co_await transaction(conn);
102 co_await hgetall(conn);
A basic_connection that type erases the executor.
ignore_t ignore
Global ignore object.
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Configure parameters used by the connection classes.