Boost.Redis: example/cpp20_streams.cpp Source File

Boost.Redis 1.84.0 A redis client library

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