Aedis 1.4.1
A redis client library
cpp20_intro.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/asio.hpp>
8#if defined(BOOST_ASIO_HAS_CO_AWAIT)
9#include <aedis.hpp>
10#include "common/common.hpp"
11
12namespace net = boost::asio;
13namespace resp3 = aedis::resp3;
14using aedis::adapt;
16
17auto run(std::shared_ptr<connection> conn, std::string host, std::string port) -> net::awaitable<void>
18{
19 co_await connect(conn, host, port);
20 co_await conn->async_run();
21}
22
23auto hello(std::shared_ptr<connection> conn) -> net::awaitable<void>
24{
26 req.push("HELLO", 3);
27
28 co_await conn->async_exec(req);
29}
30
31auto ping(std::shared_ptr<connection> conn) -> net::awaitable<void>
32{
34 req.push("PING", "Hello world");
35
36 std::tuple<std::string> resp;
37 co_await conn->async_exec(req, adapt(resp));
38
39 std::cout << "PING: " << std::get<0>(resp) << std::endl;
40}
41
42auto quit(std::shared_ptr<connection> conn) -> net::awaitable<void>
43{
45 req.push("QUIT");
46
47 co_await conn->async_exec(req);
48}
49
50// Called from the main function (see main.cpp)
51auto co_main(std::string host, std::string port) -> net::awaitable<void>
52{
53 auto ex = co_await net::this_coro::executor;
54 auto conn = std::make_shared<connection>(ex);
55 net::co_spawn(ex, run(conn, host, port), net::detached);
56 co_await hello(conn);
57 co_await ping(conn);
58 co_await quit(conn);
59}
60
61#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
Creates Redis requests.
Definition: request.hpp:169
void push(std::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
Definition: request.hpp:261
auto adapt(std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)()) noexcept
Creates an adapter that ignores responses.
Definition: adapt.hpp:199
operation
Connection operations that can be cancelled.
Definition: operation.hpp:18
@ run
Refers to connection::async_run operations.