Aedis 1.4.1
A redis client library
cpp17_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
19auto main(int argc, char * argv[]) -> int
20{
21 try {
22 std::string host = "127.0.0.1";
23 std::string port = "6379";
24
25 if (argc == 3) {
26 host = argv[1];
27 port = argv[2];
28 }
29
30 net::io_context ioc;
31 net::ip::tcp::resolver resv{ioc};
32 auto const res = resv.resolve(host, port);
33 net::ip::tcp::socket socket{ioc};
34 net::connect(socket, res);
35
36 // Creates the request and writes to the socket.
38 req.push("HELLO", 3);
39 req.push("PING", "Hello world");
40 req.push("QUIT");
41 resp3::write(socket, req);
42
43 // Responses
44 std::string buffer, resp;
45
46 // Reads the responses to all commands in the request.
47 auto dbuffer = net::dynamic_buffer(buffer);
48 resp3::read(socket, dbuffer);
49 resp3::read(socket, dbuffer, adapt2(resp));
50 resp3::read(socket, dbuffer);
51
52 std::cout << "Ping: " << resp << std::endl;
53
54 } catch (std::exception const& e) {
55 std::cerr << e.what() << std::endl;
56 exit(EXIT_FAILURE);
57 }
58}
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 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