userver: userver/storages/redis/client.hpp Source File
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/redis/client.hpp
4/// @brief @copybrief storages::redis::Client
5
6#include <chrono>
7#include <memory>
8#include <string>
9
10#include <userver/storages/redis/base.hpp>
11#include <userver/storages/redis/wait_connected_mode.hpp>
12
13#include <userver/storages/redis/bit_operation.hpp>
14#include <userver/storages/redis/client_fwd.hpp>
15#include <userver/storages/redis/command_options.hpp>
16#include <userver/storages/redis/request.hpp>
17#include <userver/storages/redis/request_eval.hpp>
18#include <userver/storages/redis/request_evalsha.hpp>
19#include <userver/storages/redis/request_generic.hpp>
20#include <userver/storages/redis/transaction.hpp>
21
22USERVER_NAMESPACE_BEGIN
23
24namespace storages::redis {
25
26enum class PubShard {
27 kZeroShard,
28 kRoundRobin,
29};
30
31/// @ingroup userver_clients
32///
33/// @brief Valkey or Redis client.
34///
35/// Usually retrieved from components::Redis component.
36///
37/// ## Example usage:
38///
39/// @snippet storages/redis/client_redistest.cpp Sample Redis Client usage
40class Client {
41public:
42 virtual ~Client() = default;
43
44 virtual size_t ShardsCount() const = 0;
45 virtual bool IsInClusterMode() const = 0;
46
47 virtual size_t ShardByKey(const std::string& key) const = 0;
48
49 void CheckShardIdx(size_t shard_idx) const;
50
51 virtual void WaitConnectedOnce(RedisWaitConnected wait_connected) = 0;
52
53 // redis commands:
54
55 virtual RequestAppend Append(std::string key, std::string value, const CommandControl& command_control) = 0;
56
57 virtual RequestBitop Bitop(
58 BitOperation op,
59 std::string dest_key,
60 std::vector<std::string> src_keys,
61 const CommandControl& command_control
62 ) = 0;
63
64 virtual RequestDbsize Dbsize(size_t shard, const CommandControl& command_control) = 0;
65
66 virtual RequestDecr Decr(std::string key, const CommandControl& command_control) = 0;
67
68 virtual RequestDel Del(std::string key, const CommandControl& command_control) = 0;
69
70 virtual RequestDel Del(std::vector<std::string> keys, const CommandControl& command_control) = 0;
71
72 virtual RequestUnlink Unlink(std::string key, const CommandControl& command_control) = 0;
73
74 virtual RequestUnlink Unlink(std::vector<std::string> keys, const CommandControl& command_control) = 0;
75
76 /// @brief Invoke the execution of a server-side Lua script.
77 ///
78 /// For huge scripts consider EvalSha() to save network bandwidth.
79 ///
80 /// Sample usage:
81 /// @snippet redis/src/storages/redis/client_cluster_redistest.cpp Sample eval usage
82 template <typename ScriptResult, typename ReplyType = ScriptResult>
83 RequestEval<ScriptResult, ReplyType> Eval(
84 std::string script,
85 std::vector<std::string> keys,
86 std::vector<std::string> args,
87 const CommandControl& command_control
88 ) {
89 return RequestEval<ScriptResult, ReplyType>{
90 EvalCommon(std::move(script), std::move(keys), std::move(args), command_control)};
91 }
92
93 /// @brief Invoke the execution of a server-side Lua script that was previously uploaded to the server via
94 /// ScriptLoad() member function.
95 ///
96 /// For small scripts consider using a simpler Eval() member function.
97 ///
98 /// Sample usage:
99 /// @snippet redis/src/storages/redis/client_cluster_redistest.cpp Sample evalsha usage
100 template <typename ScriptResult, typename ReplyType = ScriptResult>
101 RequestEvalSha<ScriptResult, ReplyType> EvalSha(
102 std::string script_hash,
103 std::vector<std::string> keys,
104 std::vector<std::string> args,
105 const CommandControl& command_control
106 ) {
107 return RequestEvalSha<ScriptResult, ReplyType>{
108 EvalShaCommon(std::move(script_hash), std::move(keys), std::move(args), command_control)};
109 }
110
111 /// @brief Execute a custom Redis command.
112 /// @param key_index Index of the key in the args vector used to determine the shard
113 ///
114 /// Sample usage:
115 /// @snippet redis/src/storages/redis/client_cluster_redistest.cpp Sample generic command usage
116 template <typename ReplyType>
117 RequestGeneric<ReplyType> GenericCommand(
118 std::string command,
119 std::vector<std::string> args,
120 size_t key_index,
121 const CommandControl& command_control
122 ) {
123 return RequestGeneric<ReplyType>{
124 GenericCommon(std::move(command), std::move(args), key_index, command_control)};
125 }
126
127 /// @brief Load the script to the server for further execution via EvalSha() member function.
128 ///
129 /// Sample usage:
130 /// @snippet redis/src/storages/redis/client_cluster_redistest.cpp Sample evalsha usage
131 virtual RequestScriptLoad ScriptLoad(std::string script, size_t shard, const CommandControl& command_control) = 0;
132
133 /// @overload
134 template <typename ScriptInfo, typename ReplyType = std::decay_t<ScriptInfo>>
135 RequestEval<std::decay_t<ScriptInfo>, ReplyType> Eval(
136 const ScriptInfo& script_info,
137 std::vector<std::string> keys,
138 std::vector<std::string> args,
139 const CommandControl& command_control
140 ) {
141 return RequestEval<std::decay_t<ScriptInfo>, ReplyType>{
142 EvalCommon(script_info.GetScript(), std::move(keys), std::move(args), command_control)};
143 }
144
145 virtual RequestExists Exists(std::string key, const CommandControl& command_control) = 0;
146
147 virtual RequestExists Exists(std::vector<std::string> keys, const CommandControl& command_control) = 0;
148
149 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl, const CommandControl& command_control) = 0;
150
151 virtual RequestExpire
152 Expire(std::string key, std::chrono::seconds ttl, ExpireOptions options, const CommandControl& command_control) = 0;
153
154 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member, const CommandControl& command_control) = 0;
155
156 virtual RequestGeoadd
157 Geoadd(std::string key, std::vector<GeoaddArg> point_members, const CommandControl& command_control) = 0;
158
159 virtual RequestGeopos
160 Geopos(std::string key, std::vector<std::string> members, const CommandControl& command_control) = 0;
161
162 virtual RequestGeoradius Georadius(
163 std::string key,
164 Longitude lon,
165 Latitude lat,
166 double radius,
167 const GeoradiusOptions& georadius_options,
168 const CommandControl& command_control
169 ) = 0;
170
171 virtual RequestGeosearch Geosearch(
172 std::string key,
173 std::string member,
174 double radius,
175 const GeosearchOptions& geosearch_options,
176 const CommandControl& command_control
177 ) = 0;
178
179 virtual RequestGeosearch Geosearch(
180 std::string key,
181 std::string member,
182 BoxWidth width,
183 BoxHeight height,
184 const GeosearchOptions& geosearch_options,
185 const CommandControl& command_control
186 ) = 0;
187
188 virtual RequestGeosearch Geosearch(
189 std::string key,
190 Longitude lon,
191 Latitude lat,
192 double radius,
193 const GeosearchOptions& geosearch_options,
194 const CommandControl& command_control
195 ) = 0;
196
197 virtual RequestGeosearch Geosearch(
198 std::string key,
199 Longitude lon,
200 Latitude lat,
201 BoxWidth width,
202 BoxHeight height,
203 const GeosearchOptions& geosearch_options,
204 const CommandControl& command_control
205 ) = 0;
206
207 virtual RequestGet Get(std::string key, const CommandControl& command_control) = 0;
208
209 virtual RequestGetset Getset(std::string key, std::string value, const CommandControl& command_control) = 0;
210
211 virtual RequestHdel Hdel(std::string key, std::string field, const CommandControl& command_control) = 0;
212
213 virtual RequestHdel
214 Hdel(std::string key, std::vector<std::string> fields, const CommandControl& command_control) = 0;
215
216 virtual RequestHexists Hexists(std::string key, std::string field, const CommandControl& command_control) = 0;
217
218 virtual RequestHget Hget(std::string key, std::string field, const CommandControl& command_control) = 0;
219
220 // use Hscan in case of a big hash
221 virtual RequestHgetall Hgetall(std::string key, const CommandControl& command_control) = 0;
222
223 virtual RequestHincrby
224 Hincrby(std::string key, std::string field, int64_t increment, const CommandControl& command_control) = 0;
225
226 virtual RequestHincrbyfloat
227 Hincrbyfloat(std::string key, std::string field, double increment, const CommandControl& command_control) = 0;
228
229 // use Hscan in case of a big hash
230 virtual RequestHkeys Hkeys(std::string key, const CommandControl& command_control) = 0;
231
232 virtual RequestHlen Hlen(std::string key, const CommandControl& command_control) = 0;
233
234 virtual RequestHmget
235 Hmget(std::string key, std::vector<std::string> fields, const CommandControl& command_control) = 0;
236
237 virtual RequestHmset Hmset(
238 std::string key,
239 std::vector<std::pair<std::string, std::string>> field_values,
240 const CommandControl& command_control
241 ) = 0;
242
243 /// @brief Iterate over a collection of elements.
244 ///
245 /// Sample usage:
246 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Hscan usage
247 virtual RequestHscan Hscan(std::string key, HscanOptions options, const CommandControl& command_control) = 0;
248
249 virtual RequestHset
250 Hset(std::string key, std::string field, std::string value, const CommandControl& command_control) = 0;
251
252 virtual RequestHsetnx
253 Hsetnx(std::string key, std::string field, std::string value, const CommandControl& command_control) = 0;
254
255 // use Hscan in case of a big hash
256 virtual RequestHvals Hvals(std::string key, const CommandControl& command_control) = 0;
257
258 virtual RequestIncr Incr(std::string key, const CommandControl& command_control) = 0;
259
260 [[deprecated("use Scan")]] virtual RequestKeys
261 Keys(std::string keys_pattern, size_t shard, const CommandControl& command_control) = 0;
262
263 virtual RequestLindex Lindex(std::string key, int64_t index, const CommandControl& command_control) = 0;
264
265 virtual RequestLlen Llen(std::string key, const CommandControl& command_control) = 0;
266
267 virtual RequestLpop Lpop(std::string key, const CommandControl& command_control) = 0;
268
269 virtual RequestLpush Lpush(std::string key, std::string value, const CommandControl& command_control) = 0;
270
271 virtual RequestLpush
272 Lpush(std::string key, std::vector<std::string> values, const CommandControl& command_control) = 0;
273
274 virtual RequestLpushx Lpushx(std::string key, std::string element, const CommandControl& command_control) = 0;
275
276 virtual RequestLrange
277 Lrange(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) = 0;
278
279 virtual RequestLrem
280 Lrem(std::string key, int64_t count, std::string element, const CommandControl& command_control) = 0;
281
282 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) = 0;
283
284 virtual RequestMget Mget(std::vector<std::string> keys, const CommandControl& command_control) = 0;
285
286 virtual RequestMset
287 Mset(std::vector<std::pair<std::string, std::string>> key_values, const CommandControl& command_control) = 0;
288
289 virtual TransactionPtr Multi() = 0;
290
291 virtual TransactionPtr Multi(Transaction::CheckShards check_shards) = 0;
292
293 virtual RequestPersist Persist(std::string key, const CommandControl& command_control) = 0;
294
295 virtual RequestPexpire
296 Pexpire(std::string key, std::chrono::milliseconds ttl, const CommandControl& command_control) = 0;
297
298 virtual RequestPing Ping(size_t shard, const CommandControl& command_control) = 0;
299
300 virtual RequestPingMessage Ping(size_t shard, std::string message, const CommandControl& command_control) = 0;
301
302 virtual void
303 Publish(std::string channel, std::string message, const CommandControl& command_control, PubShard policy) = 0;
304
305 virtual void Spublish(std::string channel, std::string message, const CommandControl& command_control) = 0;
306
307 virtual RequestRename Rename(std::string key, std::string new_key, const CommandControl& command_control) = 0;
308
309 virtual RequestRpop Rpop(std::string key, const CommandControl& command_control) = 0;
310
311 virtual RequestRpush Rpush(std::string key, std::string value, const CommandControl& command_control) = 0;
312
313 virtual RequestRpush
314 Rpush(std::string key, std::vector<std::string> values, const CommandControl& command_control) = 0;
315
316 virtual RequestRpushx Rpushx(std::string key, std::string element, const CommandControl& command_control) = 0;
317
318 /// @brief Add member to a set of elements.
319 ///
320 /// Sample usage:
321 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Sadd and Sscan usage
322 virtual RequestSadd Sadd(std::string key, std::string member, const CommandControl& command_control) = 0;
323
324 /// @overload
325 virtual RequestSadd
326 Sadd(std::string key, std::vector<std::string> members, const CommandControl& command_control) = 0;
327
328 /// @brief Iterate over a collection of elements.
329 ///
330 /// Sample usage:
331 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Scan usage
332 virtual RequestScan Scan(size_t shard, ScanOptions options, const CommandControl& command_control) = 0;
333
334 virtual RequestScard Scard(std::string key, const CommandControl& command_control) = 0;
335
336 virtual RequestSet Set(std::string key, std::string value, const CommandControl& command_control) = 0;
337
338 virtual RequestSet
339 Set(std::string key, std::string value, std::chrono::milliseconds ttl, const CommandControl& command_control) = 0;
340
341 virtual RequestSetIfExist SetIfExist(std::string key, std::string value, const CommandControl& command_control) = 0;
342
343 virtual RequestSetIfExist SetIfExist(
344 std::string key,
345 std::string value,
346 std::chrono::milliseconds ttl,
347 const CommandControl& command_control
348 ) = 0;
349
350 virtual RequestSetIfNotExist
351 SetIfNotExist(std::string key, std::string value, const CommandControl& command_control) = 0;
352
353 virtual RequestSetIfNotExist SetIfNotExist(
354 std::string key,
355 std::string value,
356 std::chrono::milliseconds ttl,
357 const CommandControl& command_control
358 ) = 0;
359
360 virtual RequestSetIfNotExistOrGet
361 SetIfNotExistOrGet(std::string key, std::string value, const CommandControl& command_control) = 0;
362
363 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(
364 std::string key,
365 std::string value,
366 std::chrono::milliseconds ttl,
367 const CommandControl& command_control
368 ) = 0;
369
370 virtual RequestSetex
371 Setex(std::string key, std::chrono::seconds seconds, std::string value, const CommandControl& command_control) = 0;
372
373 virtual RequestSismember Sismember(std::string key, std::string member, const CommandControl& command_control) = 0;
374
375 // use Sscan in case of a big set
376 virtual RequestSmembers Smembers(std::string key, const CommandControl& command_control) = 0;
377
378 virtual RequestSrandmember Srandmember(std::string key, const CommandControl& command_control) = 0;
379
380 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count, const CommandControl& command_control) = 0;
381
382 virtual RequestSrem Srem(std::string key, std::string member, const CommandControl& command_control) = 0;
383
384 virtual RequestSrem
385 Srem(std::string key, std::vector<std::string> members, const CommandControl& command_control) = 0;
386
387 /// @brief Iterate over a collection of elements.
388 ///
389 /// Sample usage:
390 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Sadd and Sscan usage
391 virtual RequestSscan Sscan(std::string key, SscanOptions options, const CommandControl& command_control) = 0;
392
393 virtual RequestStrlen Strlen(std::string key, const CommandControl& command_control) = 0;
394
395 virtual RequestTime Time(size_t shard, const CommandControl& command_control) = 0;
396
397 virtual RequestTtl Ttl(std::string key, const CommandControl& command_control) = 0;
398
399 virtual RequestType Type(std::string key, const CommandControl& command_control) = 0;
400
401 virtual RequestZadd
402 Zadd(std::string key, double score, std::string member, const CommandControl& command_control) = 0;
403
404 virtual RequestZadd Zadd(
405 std::string key,
406 double score,
407 std::string member,
408 const ZaddOptions& options,
409 const CommandControl& command_control
410 ) = 0;
411
412 virtual RequestZadd Zadd(
413 std::string key,
414 std::vector<std::pair<double, std::string>> scored_members,
415 const CommandControl& command_control
416 ) = 0;
417
418 virtual RequestZadd Zadd(
419 std::string key,
420 std::vector<std::pair<double, std::string>> scored_members,
421 const ZaddOptions& options,
422 const CommandControl& command_control
423 ) = 0;
424
425 virtual RequestZaddIncr
426 ZaddIncr(std::string key, double score, std::string member, const CommandControl& command_control) = 0;
427
428 virtual RequestZaddIncrExisting
429 ZaddIncrExisting(std::string key, double score, std::string member, const CommandControl& command_control) = 0;
430
431 virtual RequestZcard Zcard(std::string key, const CommandControl& command_control) = 0;
432
433 virtual RequestZcount Zcount(std::string key, double min, double max, const CommandControl& command_control) = 0;
434
435 virtual RequestZrange
436 Zrange(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) = 0;
437
438 virtual RequestZrangeWithScores
439 ZrangeWithScores(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) = 0;
440
441 virtual RequestZrangebyscore
442 Zrangebyscore(std::string key, double min, double max, const CommandControl& command_control) = 0;
443
444 virtual RequestZrangebyscore
445 Zrangebyscore(std::string key, std::string min, std::string max, const CommandControl& command_control) = 0;
446
447 virtual RequestZrangebyscore Zrangebyscore(
448 std::string key,
449 double min,
450 double max,
451 const RangeOptions& range_options,
452 const CommandControl& command_control
453 ) = 0;
454
455 virtual RequestZrangebyscore Zrangebyscore(
456 std::string key,
457 std::string min,
458 std::string max,
459 const RangeOptions& range_options,
460 const CommandControl& command_control
461 ) = 0;
462
463 virtual RequestZrangebyscoreWithScores
464 ZrangebyscoreWithScores(std::string key, double min, double max, const CommandControl& command_control) = 0;
465
466 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
467 std::string key,
468 std::string min,
469 std::string max,
470 const CommandControl& command_control
471 ) = 0;
472
473 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
474 std::string key,
475 double min,
476 double max,
477 const RangeOptions& range_options,
478 const CommandControl& command_control
479 ) = 0;
480
481 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
482 std::string key,
483 std::string min,
484 std::string max,
485 const RangeOptions& range_options,
486 const CommandControl& command_control
487 ) = 0;
488
489 virtual RequestZrem Zrem(std::string key, std::string member, const CommandControl& command_control) = 0;
490
491 virtual RequestZrem
492 Zrem(std::string key, std::vector<std::string> members, const CommandControl& command_control) = 0;
493
494 virtual RequestZremrangebyrank
495 Zremrangebyrank(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) = 0;
496
497 virtual RequestZremrangebyscore
498 Zremrangebyscore(std::string key, double min, double max, const CommandControl& command_control) = 0;
499
500 virtual RequestZremrangebyscore
501 Zremrangebyscore(std::string key, std::string min, std::string max, const CommandControl& command_control) = 0;
502
503 /// @brief Iterate over a collection of elements.
504 ///
505 /// Sample usage:
506 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Zscan usage
507 virtual RequestZscan Zscan(std::string key, ZscanOptions options, const CommandControl& command_control) = 0;
508
509 virtual RequestZscore Zscore(std::string key, std::string member, const CommandControl& command_control) = 0;
510
511 // end of redis commands
512
513 RequestGet Get(std::string key, RetryNilFromMaster, const CommandControl& command_control);
514
515 RequestHget Hget(std::string key, std::string field, RetryNilFromMaster, const CommandControl& command_control);
516
517 RequestZscore
518 Zscore(std::string key, std::string member, RetryNilFromMaster, const CommandControl& command_control);
519
520 void Publish(std::string channel, std::string message, const CommandControl& command_control);
521
522 RequestScan Scan(size_t shard, const CommandControl& command_control);
523
524 RequestHscan Hscan(std::string key, const CommandControl& command_control);
525
526 RequestSscan Sscan(std::string key, const CommandControl& command_control);
527
528 RequestZscan Zscan(std::string key, const CommandControl& command_control);
529
530protected:
531 virtual RequestEvalCommon EvalCommon(
532 std::string script,
533 std::vector<std::string> keys,
534 std::vector<std::string> args,
535 const CommandControl& command_control
536 ) = 0;
537 virtual RequestEvalShaCommon EvalShaCommon(
538 std::string script_hash,
539 std::vector<std::string> keys,
540 std::vector<std::string> args,
541 const CommandControl& command_control
542 ) = 0;
543 virtual RequestGenericCommon GenericCommon(
544 std::string command,
545 std::vector<std::string> args,
546 size_t key_index,
547 const CommandControl& command_control
548 ) = 0;
549};
550
551std::string CreateTmpKey(const std::string& key, std::string prefix);
552
553} // namespace storages::redis
554
555USERVER_NAMESPACE_END