Aedis 1.4.1
A redis client library
low_level_async.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 <string>
11#include <iostream>
12
13namespace net = boost::asio;
14namespace resp3 = aedis::resp3;
15using resolver = net::use_awaitable_t<>::as_default_on_t<net::ip::tcp::resolver>;
16using tcp_socket = net::use_awaitable_t<>::as_default_on_t<net::ip::tcp::socket>;
18using net::ip::tcp;
19
20auto async_main() -> net::awaitable<void>
21{
22 auto ex = co_await net::this_coro::executor;
23
24 resolver resv{ex};
25 auto const addrs = co_await resv.async_resolve("127.0.0.1", "6379");
26 tcp_socket socket{ex};
27 co_await net::async_connect(socket, addrs);
28
29 // Creates the request and writes to the socket.
31 req.push("HELLO", 3);
32 req.push("PING", "Hello world");
33 req.push("QUIT");
34 co_await resp3::async_write(socket, req);
35
36 // Responses
37 std::string buffer, resp;
38
39 // Reads the responses to all commands in the request.
40 auto dbuffer = net::dynamic_buffer(buffer);
41 co_await resp3::async_read(socket, dbuffer);
42 co_await resp3::async_read(socket, dbuffer, adapt2(resp));
43 co_await resp3::async_read(socket, dbuffer);
44
45 std::cout << "Ping: " << resp << std::endl;
46}
47
48#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
Creates Redis requests.
Definition: request.hpp:168
void push(std::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
Definition: request.hpp:260
auto adapt2() noexcept
Creates a dummy response adapter.
Definition: adapt.hpp:40
auto async_write(AsyncWriteStream &stream, Request const &req, CompletionToken &&token=boost::asio::default_completion_token_t< typename AsyncWriteStream::executor_type >{})
Writes a request asynchronously.
Definition: write.hpp:53
auto async_read(AsyncReadStream &stream, DynamicBuffer buffer, ResponseAdapter adapter=ResponseAdapter{}, CompletionToken &&token=boost::asio::default_completion_token_t< typename AsyncReadStream::executor_type >{})
Reads a complete response to a Redis command asynchronously.
Definition: read.hpp:169