Boost.Redis: example/cpp20_intro.cpp Source File

Boost.Redis 1.84.0 A redis client library

Loading...
Searching...
No Matches
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/redis/connection.hpp>
8#include <boost/asio/co_spawn.hpp>
9#include <boost/asio/detached.hpp>
10#include <boost/asio/consign.hpp>
11#include <iostream>
12
13#if defined(BOOST_ASIO_HAS_CO_AWAIT)
14
15namespace asio = boost::asio;
16using boost::redis::request;
17using boost::redis::response;
18using boost::redis::config;
19using boost::redis::connection;
20
21// Called from the main function (see main.cpp)
22auto co_main(config cfg) -> asio::awaitable<void>
23{
24 auto conn = std::make_shared<connection>(co_await asio::this_coro::executor);
25 conn->async_run(cfg, {}, asio::consign(asio::detached, conn));
26
27 // A request containing only a ping command.
28 request req;
29 req.push("PING", "Hello world");
30
31 // Response where the PONG response will be stored.
32 response<std::string> resp;
33
34 // Executes the request.
35 co_await conn->async_exec(req, resp);
36 conn->cancel();
37
38 std::cout << "PING: " << std::get<0>(resp).value() << std::endl;
39}
40
41#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
boost::redis::connection
A basic_connection that type erases the executor.
Definition connection.hpp:1231
boost::redis::request
Creates Redis requests.
Definition request.hpp:46
boost::redis::response
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition response.hpp:25
boost::redis::config
Configure parameters used by the connection classes.
Definition config.hpp:30