boost_redis 1.4.2
A redis client library
request.hpp
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#ifndef BOOST_REDIS_REQUEST_HPP
8#define BOOST_REDIS_REQUEST_HPP
9
10#include <boost/redis/resp3/type.hpp>
11#include <boost/redis/resp3/serialization.hpp>
12
13#include <string>
14#include <tuple>
15#include <algorithm>
16
17// NOTE: For some commands like hset it would be a good idea to assert
18// the value type is a pair.
19
20namespace boost::redis {
21
22namespace detail{
23auto has_response(std::string_view cmd) -> bool;
24}
25
46class request {
47public:
49 struct config {
56
62
70
77 };
78
83 explicit
84 request(config cfg = config{true, false, true, true})
85 : cfg_{cfg} {}
86
88 [[nodiscard]] auto size() const noexcept -> std::size_t
89 { return commands_;};
90
91 [[nodiscard]] auto payload() const noexcept -> std::string_view
92 { return payload_;}
93
94 [[nodiscard]] auto has_hello_priority() const noexcept -> auto const&
95 { return has_hello_priority_;}
96
98 void clear()
99 {
100 payload_.clear();
101 commands_ = 0;
102 has_hello_priority_ = false;
103 }
104
106 void reserve(std::size_t new_cap = 0)
107 { payload_.reserve(new_cap); }
108
110 [[nodiscard]] auto get_config() const noexcept -> auto const& {return cfg_; }
111
113 [[nodiscard]] auto get_config() noexcept -> auto& {return cfg_; }
114
140 template <class... Ts>
141 void push(std::string_view cmd, Ts const&... args)
142 {
143 auto constexpr pack_size = sizeof...(Ts);
144 resp3::add_header(payload_, resp3::type::array, 1 + pack_size);
145 resp3::add_bulk(payload_, cmd);
146 resp3::add_bulk(payload_, std::tie(std::forward<Ts const&>(args)...));
147
148 check_cmd(cmd);
149 }
150
182 template <class ForwardIterator>
183 void
185 std::string_view const& cmd,
186 std::string_view const& key,
187 ForwardIterator begin,
188 ForwardIterator end,
189 typename std::iterator_traits<ForwardIterator>::value_type * = nullptr)
190 {
191 using value_type = typename std::iterator_traits<ForwardIterator>::value_type;
192
193 if (begin == end)
194 return;
195
196 auto constexpr size = resp3::bulk_counter<value_type>::size;
197 auto const distance = std::distance(begin, end);
198 resp3::add_header(payload_, resp3::type::array, 2 + size * distance);
199 resp3::add_bulk(payload_, cmd);
200 resp3::add_bulk(payload_, key);
201
202 for (; begin != end; ++begin)
203 resp3::add_bulk(payload_, *begin);
204
205 check_cmd(cmd);
206 }
207
235 template <class ForwardIterator>
236 void
238 std::string_view const& cmd,
239 ForwardIterator begin,
240 ForwardIterator end,
241 typename std::iterator_traits<ForwardIterator>::value_type * = nullptr)
242 {
243 using value_type = typename std::iterator_traits<ForwardIterator>::value_type;
244
245 if (begin == end)
246 return;
247
248 auto constexpr size = resp3::bulk_counter<value_type>::size;
249 auto const distance = std::distance(begin, end);
250 resp3::add_header(payload_, resp3::type::array, 1 + size * distance);
251 resp3::add_bulk(payload_, cmd);
252
253 for (; begin != end; ++begin)
254 resp3::add_bulk(payload_, *begin);
255
256 check_cmd(cmd);
257 }
258
269 template <class Range>
270 void
272 std::string_view const& cmd,
273 std::string_view const& key,
274 Range const& range,
275 decltype(std::begin(range)) * = nullptr)
276 {
277 using std::begin;
278 using std::end;
279 push_range(cmd, key, begin(range), end(range));
280 }
281
291 template <class Range>
292 void
294 std::string_view cmd,
295 Range const& range,
296 decltype(std::cbegin(range)) * = nullptr)
297 {
298 using std::cbegin;
299 using std::cend;
300 push_range(cmd, cbegin(range), cend(range));
301 }
302
303private:
304 void check_cmd(std::string_view cmd)
305 {
306 if (!detail::has_response(cmd))
307 ++commands_;
308
309 if (cmd == "HELLO")
310 has_hello_priority_ = cfg_.hello_with_priority;
311 }
312
313 config cfg_;
314 std::string payload_;
315 std::size_t commands_ = 0;
316 bool has_hello_priority_ = false;
317};
318
319} // boost::redis::resp3
320
321#endif // BOOST_REDIS_REQUEST_HPP
Creates Redis requests.
Definition: request.hpp:46
bool cancel_if_not_connected
If true the request will complete with boost::redis::error::not_connected if async_exec is called bef...
Definition: request.hpp:61
auto get_config() noexcept -> auto &
Returns a reference to the config object.
Definition: request.hpp:113
bool hello_with_priority
If this request has a HELLO command and this flag is true, the boost::redis::connection will move it ...
Definition: request.hpp:76
void push_range(std::string_view cmd, Range const &range, decltype(std::cbegin(range)) *=nullptr)
Appends a new command to the end of the request.
Definition: request.hpp:293
void reserve(std::size_t new_cap=0)
Calls std::string::reserve on the internal storage.
Definition: request.hpp:106
request(config cfg=config{true, false, true, true})
Constructor.
Definition: request.hpp:84
bool cancel_if_unresponded
If false boost::redis::connection::async_exec will not automatically cancel this request if the conne...
Definition: request.hpp:69
void push(std::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
Definition: request.hpp:141
void clear()
Clears the request preserving allocated memory.
Definition: request.hpp:98
void push_range(std::string_view const &cmd, std::string_view const &key, ForwardIterator begin, ForwardIterator end, typename std::iterator_traits< ForwardIterator >::value_type *=nullptr)
Appends a new command to the end of the request.
Definition: request.hpp:184
bool cancel_on_connection_lost
If true boost::redis::connection::async_exec will complete with error if the connection is lost....
Definition: request.hpp:55
auto get_config() const noexcept -> auto const &
Returns a const reference to the config object.
Definition: request.hpp:110
void push_range(std::string_view const &cmd, ForwardIterator begin, ForwardIterator end, typename std::iterator_traits< ForwardIterator >::value_type *=nullptr)
Appends a new command to the end of the request.
Definition: request.hpp:237
void push_range(std::string_view const &cmd, std::string_view const &key, Range const &range, decltype(std::begin(range)) *=nullptr)
Appends a new command to the end of the request.
Definition: request.hpp:271
Request configuration options.
Definition: request.hpp:49