boost_redis 1.4.2
A redis client library
cpp20_streams.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/deferred.hpp>
9#include <boost/asio/co_spawn.hpp>
10#include <boost/asio/detached.hpp>
11#include <boost/asio/consign.hpp>
12#include <boost/asio/signal_set.hpp>
13#include <boost/asio/awaitable.hpp>
14#include <iostream>
15
16#if defined(BOOST_ASIO_HAS_CO_AWAIT)
17
18#include <memory>
19#include <string>
20#include <thread>
21#include <vector>
22
23namespace net = boost::asio;
29using signal_set = net::deferred_t::as_default_on_t<net::signal_set>;
30
31auto stream_reader(std::shared_ptr<connection> conn) -> net::awaitable<void>
32{
33 std::string redisStreamKey_;
34 request req;
36
37 std::string stream_id{"$"};
38 std::string const field = "myfield";
39
40 for (;;) {
41 req.push("XREAD", "BLOCK", "0", "STREAMS", "test-topic", stream_id);
42 co_await conn->async_exec(req, resp, net::deferred);
43
44 //std::cout << "Response: ";
45 //for (auto i = 0UL; i < resp->size(); ++i) {
46 // std::cout << resp->at(i).value << ", ";
47 //}
48 //std::cout << std::endl;
49
50 // The following approach was taken in order to be able to
51 // deal with the responses, as generated by redis in the case
52 // that there are multiple stream 'records' within a single
53 // generic_response. The nesting and number of values in
54 // resp.value() are different, depending on the contents
55 // of the stream in redis. Uncomment the above commented-out
56 // code for examples while running the XADD command.
57
58 std::size_t item_index = 0;
59 while (item_index < std::size(resp.value())) {
60 auto const& val = resp.value().at(item_index).value;
61
62 if (field.compare(val) == 0) {
63 // We've hit a myfield field.
64 // The streamId is located at item_index - 2
65 // The payload is located at item_index + 1
66 stream_id = resp.value().at(item_index - 2).value;
67 std::cout
68 << "StreamId: " << stream_id << ", "
69 << "MyField: " << resp.value().at(item_index + 1).value
70 << std::endl;
71 ++item_index; // We can increase so we don't read this again
72 }
73
74 ++item_index;
75 }
76
77 req.clear();
78 resp.value().clear();
79 }
80}
81
82// Run this in another terminal:
83// redis-cli -r 100000 -i 0.0001 XADD "test-topic" "*" "myfield" "myfieldvalue1"
84auto co_main(config cfg) -> net::awaitable<void>
85{
86 auto ex = co_await net::this_coro::executor;
87 auto conn = std::make_shared<connection>(ex);
88 net::co_spawn(ex, stream_reader(conn), net::detached);
89
90 // Disable health checks.
91 cfg.health_check_interval = std::chrono::seconds::zero();
92 conn->async_run(cfg, {}, net::consign(net::detached, conn));
93
94 signal_set sig_set(ex, SIGINT, SIGTERM);
95 co_await sig_set.async_wait();
96 conn->cancel();
97}
98#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
A basic_connection that type erases the executor.
Definition: connection.hpp:308
Creates Redis requests.
Definition: request.hpp:46
adapter::result< std::vector< resp3::node > > generic_response
A generic response to a request.
Definition: response.hpp:33
operation
Connection operations that can be cancelled.
Definition: operation.hpp:18
Configure parameters used by the connection classes.
Definition: config.hpp:30