Aedis 1.4.1
A redis client library
low_level_sync.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 <string>
8#include <iostream>
9
10#include <boost/asio/connect.hpp>
11
12#include <aedis.hpp>
13#include <aedis/src.hpp>
14
15namespace net = boost::asio;
16namespace resp3 = aedis::resp3;
18
19int main()
20{
21 try {
22 net::io_context ioc;
23 net::ip::tcp::resolver resv{ioc};
24 auto const res = resv.resolve("127.0.0.1", "6379");
25 net::ip::tcp::socket socket{ioc};
26 net::connect(socket, res);
27
28 // Creates the request and writes to the socket.
30 req.push("HELLO", 3);
31 req.push("PING", "Hello world");
32 req.push("QUIT");
33 resp3::write(socket, req);
34
35 // Responses
36 std::string buffer, resp;
37
38 // Reads the responses to all commands in the request.
39 auto dbuffer = net::dynamic_buffer(buffer);
40 resp3::read(socket, dbuffer);
41 resp3::read(socket, dbuffer, adapt2(resp));
42 resp3::read(socket, dbuffer);
43
44 std::cout << "Ping: " << resp << std::endl;
45
46 } catch (std::exception const& e) {
47 std::cerr << e.what() << std::endl;
48 exit(EXIT_FAILURE);
49 }
50}
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 write(SyncWriteStream &stream, Request const &req)
Writes a request synchronously.
Definition: write.hpp:24
auto read(SyncReadStream &stream, DynamicBuffer buf, ResponseAdapter adapter, boost::system::error_code &ec) -> std::size_t
Reads a complete response to a command sychronously.
Definition: read.hpp:56