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
94 /// @brief Invoke the execution of a server-side Lua script that was previously uploaded to the server via
95 /// ScriptLoad() member function.
96 ///
97 /// For small scripts consider using a simpler Eval() member function.
98 ///
99 /// Sample usage:
100 /// @snippet redis/src/storages/redis/client_cluster_redistest.cpp Sample evalsha usage
101 template <typename ScriptResult, typename ReplyType = ScriptResult>
102 RequestEvalSha<ScriptResult, ReplyType> EvalSha(
103 std::string script_hash,
104 std::vector<std::string> keys,
105 std::vector<std::string> args,
106 const CommandControl& command_control
107 ) {
108 return RequestEvalSha<ScriptResult, ReplyType>{
109 EvalShaCommon(std::move(script_hash), std::move(keys), std::move(args), command_control)
110 };
111 }
112
113 /// @brief Execute a custom Redis command.
114 /// @param key_index Index of the key in the args vector used to determine the shard
115 ///
116 /// Sample usage:
117 /// @snippet redis/src/storages/redis/client_cluster_redistest.cpp Sample generic command usage
118 template <typename ReplyType>
119 RequestGeneric<ReplyType> GenericCommand(
120 std::string command,
121 std::vector<std::string> args,
122 size_t key_index,
123 const CommandControl& command_control
124 ) {
125 return RequestGeneric<ReplyType>{GenericCommon(std::move(command), std::move(args), key_index, command_control)
126 };
127 }
128
129 /// @brief Load the script to the server for further execution via EvalSha() member function.
130 ///
131 /// Sample usage:
132 /// @snippet redis/src/storages/redis/client_cluster_redistest.cpp Sample evalsha usage
133 virtual RequestScriptLoad ScriptLoad(std::string script, size_t shard, const CommandControl& command_control) = 0;
134
135 /// @overload
136 template <typename ScriptInfo, typename ReplyType = std::decay_t<ScriptInfo>>
137 RequestEval<std::decay_t<ScriptInfo>, ReplyType> Eval(
138 const ScriptInfo& script_info,
139 std::vector<std::string> keys,
140 std::vector<std::string> args,
141 const CommandControl& command_control
142 ) {
143 return RequestEval<std::decay_t<ScriptInfo>, ReplyType>{
144 EvalCommon(script_info.GetScript(), std::move(keys), std::move(args), command_control)
145 };
146 }
147
148 virtual RequestExists Exists(std::string key, const CommandControl& command_control) = 0;
149
150 virtual RequestExists Exists(std::vector<std::string> keys, const CommandControl& command_control) = 0;
151
152 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl, const CommandControl& command_control) = 0;
153
154 virtual RequestExpire Expire(
155 std::string key,
156 std::chrono::seconds ttl,
157 ExpireOptions options,
158 const CommandControl& command_control
159 ) = 0;
160
161 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member, const CommandControl& command_control) = 0;
162
163 virtual RequestGeoadd Geoadd(
164 std::string key,
165 std::vector<GeoaddArg> point_members,
166 const CommandControl& command_control
167 ) = 0;
168
169 virtual RequestGeopos Geopos(
170 std::string key,
171 std::vector<std::string> members,
172 const CommandControl& command_control
173 ) = 0;
174
175 virtual RequestGeoradius Georadius(
176 std::string key,
177 Longitude lon,
178 Latitude lat,
179 double radius,
180 const GeoradiusOptions& georadius_options,
181 const CommandControl& command_control
182 ) = 0;
183
184 virtual RequestGeosearch Geosearch(
185 std::string key,
186 std::string member,
187 double radius,
188 const GeosearchOptions& geosearch_options,
189 const CommandControl& command_control
190 ) = 0;
191
192 virtual RequestGeosearch Geosearch(
193 std::string key,
194 std::string member,
195 BoxWidth width,
196 BoxHeight height,
197 const GeosearchOptions& geosearch_options,
198 const CommandControl& command_control
199 ) = 0;
200
201 virtual RequestGeosearch Geosearch(
202 std::string key,
203 Longitude lon,
204 Latitude lat,
205 double radius,
206 const GeosearchOptions& geosearch_options,
207 const CommandControl& command_control
208 ) = 0;
209
210 virtual RequestGeosearch Geosearch(
211 std::string key,
212 Longitude lon,
213 Latitude lat,
214 BoxWidth width,
215 BoxHeight height,
216 const GeosearchOptions& geosearch_options,
217 const CommandControl& command_control
218 ) = 0;
219
220 virtual RequestGet Get(std::string key, const CommandControl& command_control) = 0;
221
222 virtual RequestGetset Getset(std::string key, std::string value, const CommandControl& command_control) = 0;
223
224 virtual RequestHdel Hdel(std::string key, std::string field, const CommandControl& command_control) = 0;
225
226 virtual RequestHdel Hdel(
227 std::string key,
228 std::vector<std::string> fields,
229 const CommandControl& command_control
230 ) = 0;
231
232 virtual RequestHexists Hexists(std::string key, std::string field, const CommandControl& command_control) = 0;
233
234 virtual RequestHget Hget(std::string key, std::string field, const CommandControl& command_control) = 0;
235
236 // use Hscan in case of a big hash
237 virtual RequestHgetall Hgetall(std::string key, const CommandControl& command_control) = 0;
238
239 virtual RequestHincrby Hincrby(
240 std::string key,
241 std::string field,
242 int64_t increment,
243 const CommandControl& command_control
244 ) = 0;
245
246 virtual RequestHincrbyfloat Hincrbyfloat(
247 std::string key,
248 std::string field,
249 double increment,
250 const CommandControl& command_control
251 ) = 0;
252
253 // use Hscan in case of a big hash
254 virtual RequestHkeys Hkeys(std::string key, const CommandControl& command_control) = 0;
255
256 virtual RequestHlen Hlen(std::string key, const CommandControl& command_control) = 0;
257
258 virtual RequestHmget Hmget(
259 std::string key,
260 std::vector<std::string> fields,
261 const CommandControl& command_control
262 ) = 0;
263
264 virtual RequestHmset Hmset(
265 std::string key,
266 std::vector<std::pair<std::string, std::string>> field_values,
267 const CommandControl& command_control
268 ) = 0;
269
270 /// @brief Iterate over a collection of elements.
271 ///
272 /// Sample usage:
273 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Hscan usage
274 virtual RequestHscan Hscan(std::string key, HscanOptions options, const CommandControl& command_control) = 0;
275
276 virtual RequestHset Hset(
277 std::string key,
278 std::string field,
279 std::string value,
280 const CommandControl& command_control
281 ) = 0;
282
283 virtual RequestHsetnx Hsetnx(
284 std::string key,
285 std::string field,
286 std::string value,
287 const CommandControl& command_control
288 ) = 0;
289
290 // use Hscan in case of a big hash
291 virtual RequestHvals Hvals(std::string key, const CommandControl& command_control) = 0;
292
293 virtual RequestIncr Incr(std::string key, const CommandControl& command_control) = 0;
294
295 [[deprecated("use Scan")]] virtual RequestKeys Keys(
296 std::string keys_pattern,
297 size_t shard,
298 const CommandControl& command_control
299 ) = 0;
300
301 virtual RequestLindex Lindex(std::string key, int64_t index, const CommandControl& command_control) = 0;
302
303 virtual RequestLlen Llen(std::string key, const CommandControl& command_control) = 0;
304
305 virtual RequestLpop Lpop(std::string key, const CommandControl& command_control) = 0;
306
307 virtual RequestLpush Lpush(std::string key, std::string value, const CommandControl& command_control) = 0;
308
309 virtual RequestLpush Lpush(
310 std::string key,
311 std::vector<std::string> values,
312 const CommandControl& command_control
313 ) = 0;
314
315 virtual RequestLpushx Lpushx(std::string key, std::string element, const CommandControl& command_control) = 0;
316
317 virtual RequestLrange Lrange(
318 std::string key,
319 int64_t start,
320 int64_t stop,
321 const CommandControl& command_control
322 ) = 0;
323
324 virtual RequestLrem Lrem(
325 std::string key,
326 int64_t count,
327 std::string element,
328 const CommandControl& command_control
329 ) = 0;
330
331 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) = 0;
332
333 virtual RequestMget Mget(std::vector<std::string> keys, const CommandControl& command_control) = 0;
334
335 virtual RequestMset Mset(
336 std::vector<std::pair<std::string, std::string>> key_values,
337 const CommandControl& command_control
338 ) = 0;
339
340 virtual TransactionPtr Multi() = 0;
341
342 virtual TransactionPtr Multi(Transaction::CheckShards check_shards) = 0;
343
344 virtual RequestPersist Persist(std::string key, const CommandControl& command_control) = 0;
345
346 virtual RequestPexpire Pexpire(
347 std::string key,
348 std::chrono::milliseconds ttl,
349 const CommandControl& command_control
350 ) = 0;
351
352 virtual RequestPing Ping(size_t shard, const CommandControl& command_control) = 0;
353
354 virtual RequestPingMessage Ping(size_t shard, std::string message, const CommandControl& command_control) = 0;
355
356 virtual void Publish(
357 std::string channel,
358 std::string message,
359 const CommandControl& command_control,
360 PubShard policy
361 ) = 0;
362
363 virtual void Spublish(std::string channel, std::string message, const CommandControl& command_control) = 0;
364
365 virtual RequestRename Rename(std::string key, std::string new_key, const CommandControl& command_control) = 0;
366
367 virtual RequestRpop Rpop(std::string key, const CommandControl& command_control) = 0;
368
369 virtual RequestRpush Rpush(std::string key, std::string value, const CommandControl& command_control) = 0;
370
371 virtual RequestRpush Rpush(
372 std::string key,
373 std::vector<std::string> values,
374 const CommandControl& command_control
375 ) = 0;
376
377 virtual RequestRpushx Rpushx(std::string key, std::string element, const CommandControl& command_control) = 0;
378
379 /// @brief Add member to a set of elements.
380 ///
381 /// Sample usage:
382 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Sadd and Sscan usage
383 virtual RequestSadd Sadd(std::string key, std::string member, const CommandControl& command_control) = 0;
384
385 /// @overload
386 virtual RequestSadd Sadd(
387 std::string key,
388 std::vector<std::string> members,
389 const CommandControl& command_control
390 ) = 0;
391
392 /// @brief Iterate over a collection of elements.
393 ///
394 /// Sample usage:
395 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Scan usage
396 virtual RequestScan Scan(size_t shard, ScanOptions options, const CommandControl& command_control) = 0;
397
398 virtual RequestScard Scard(std::string key, const CommandControl& command_control) = 0;
399
400 virtual RequestSet Set(std::string key, std::string value, const CommandControl& command_control) = 0;
401
402 virtual RequestSet Set(
403 std::string key,
404 std::string value,
405 std::chrono::milliseconds ttl,
406 const CommandControl& command_control
407 ) = 0;
408
409 virtual RequestSetIfExist SetIfExist(std::string key, std::string value, const CommandControl& command_control) = 0;
410
411 virtual RequestSetIfExist SetIfExist(
412 std::string key,
413 std::string value,
414 std::chrono::milliseconds ttl,
415 const CommandControl& command_control
416 ) = 0;
417
418 virtual RequestSetIfNotExist SetIfNotExist(
419 std::string key,
420 std::string value,
421 const CommandControl& command_control
422 ) = 0;
423
424 virtual RequestSetIfNotExist SetIfNotExist(
425 std::string key,
426 std::string value,
427 std::chrono::milliseconds ttl,
428 const CommandControl& command_control
429 ) = 0;
430
431 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(
432 std::string key,
433 std::string value,
434 const CommandControl& command_control
435 ) = 0;
436
437 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(
438 std::string key,
439 std::string value,
440 std::chrono::milliseconds ttl,
441 const CommandControl& command_control
442 ) = 0;
443
444 virtual RequestSetex Setex(
445 std::string key,
446 std::chrono::seconds seconds,
447 std::string value,
448 const CommandControl& command_control
449 ) = 0;
450
451 virtual RequestSismember Sismember(std::string key, std::string member, const CommandControl& command_control) = 0;
452
453 // use Sscan in case of a big set
454 virtual RequestSmembers Smembers(std::string key, const CommandControl& command_control) = 0;
455
456 virtual RequestSrandmember Srandmember(std::string key, const CommandControl& command_control) = 0;
457
458 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count, const CommandControl& command_control) = 0;
459
460 virtual RequestSrem Srem(std::string key, std::string member, const CommandControl& command_control) = 0;
461
462 virtual RequestSrem Srem(
463 std::string key,
464 std::vector<std::string> members,
465 const CommandControl& command_control
466 ) = 0;
467
468 /// @brief Iterate over a collection of elements.
469 ///
470 /// Sample usage:
471 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Sadd and Sscan usage
472 virtual RequestSscan Sscan(std::string key, SscanOptions options, const CommandControl& command_control) = 0;
473
474 virtual RequestStrlen Strlen(std::string key, const CommandControl& command_control) = 0;
475
476 virtual RequestTime Time(size_t shard, const CommandControl& command_control) = 0;
477
478 virtual RequestTtl Ttl(std::string key, const CommandControl& command_control) = 0;
479
480 virtual RequestType Type(std::string key, const CommandControl& command_control) = 0;
481
482 virtual RequestZadd Zadd(
483 std::string key,
484 double score,
485 std::string member,
486 const CommandControl& command_control
487 ) = 0;
488
489 virtual RequestZadd Zadd(
490 std::string key,
491 double score,
492 std::string member,
493 const ZaddOptions& options,
494 const CommandControl& command_control
495 ) = 0;
496
497 virtual RequestZadd Zadd(
498 std::string key,
499 std::vector<std::pair<double, std::string>> scored_members,
500 const CommandControl& command_control
501 ) = 0;
502
503 virtual RequestZadd Zadd(
504 std::string key,
505 std::vector<std::pair<double, std::string>> scored_members,
506 const ZaddOptions& options,
507 const CommandControl& command_control
508 ) = 0;
509
510 virtual RequestZaddIncr ZaddIncr(
511 std::string key,
512 double score,
513 std::string member,
514 const CommandControl& command_control
515 ) = 0;
516
517 virtual RequestZaddIncrExisting ZaddIncrExisting(
518 std::string key,
519 double score,
520 std::string member,
521 const CommandControl& command_control
522 ) = 0;
523
524 virtual RequestZcard Zcard(std::string key, const CommandControl& command_control) = 0;
525
526 virtual RequestZcount Zcount(std::string key, double min, double max, const CommandControl& command_control) = 0;
527
528 virtual RequestZrange Zrange(
529 std::string key,
530 int64_t start,
531 int64_t stop,
532 const CommandControl& command_control
533 ) = 0;
534
535 virtual RequestZrangeWithScores ZrangeWithScores(
536 std::string key,
537 int64_t start,
538 int64_t stop,
539 const CommandControl& command_control
540 ) = 0;
541
542 virtual RequestZrangebyscore Zrangebyscore(
543 std::string key,
544 double min,
545 double max,
546 const CommandControl& command_control
547 ) = 0;
548
549 virtual RequestZrangebyscore Zrangebyscore(
550 std::string key,
551 std::string min,
552 std::string max,
553 const CommandControl& command_control
554 ) = 0;
555
556 virtual RequestZrangebyscore Zrangebyscore(
557 std::string key,
558 double min,
559 double max,
560 const RangeOptions& range_options,
561 const CommandControl& command_control
562 ) = 0;
563
564 virtual RequestZrangebyscore Zrangebyscore(
565 std::string key,
566 std::string min,
567 std::string max,
568 const RangeOptions& range_options,
569 const CommandControl& command_control
570 ) = 0;
571
572 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
573 std::string key,
574 double min,
575 double max,
576 const CommandControl& command_control
577 ) = 0;
578
579 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
580 std::string key,
581 std::string min,
582 std::string max,
583 const CommandControl& command_control
584 ) = 0;
585
586 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
587 std::string key,
588 double min,
589 double max,
590 const RangeOptions& range_options,
591 const CommandControl& command_control
592 ) = 0;
593
594 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
595 std::string key,
596 std::string min,
597 std::string max,
598 const RangeOptions& range_options,
599 const CommandControl& command_control
600 ) = 0;
601
602 virtual RequestZrem Zrem(std::string key, std::string member, const CommandControl& command_control) = 0;
603
604 virtual RequestZrem Zrem(
605 std::string key,
606 std::vector<std::string> members,
607 const CommandControl& command_control
608 ) = 0;
609
610 virtual RequestZremrangebyrank Zremrangebyrank(
611 std::string key,
612 int64_t start,
613 int64_t stop,
614 const CommandControl& command_control
615 ) = 0;
616
617 virtual RequestZremrangebyscore Zremrangebyscore(
618 std::string key,
619 double min,
620 double max,
621 const CommandControl& command_control
622 ) = 0;
623
624 virtual RequestZremrangebyscore Zremrangebyscore(
625 std::string key,
626 std::string min,
627 std::string max,
628 const CommandControl& command_control
629 ) = 0;
630
631 /// @brief Iterate over a collection of elements.
632 ///
633 /// Sample usage:
634 /// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Zscan usage
635 virtual RequestZscan Zscan(std::string key, ZscanOptions options, const CommandControl& command_control) = 0;
636
637 virtual RequestZscore Zscore(std::string key, std::string member, const CommandControl& command_control) = 0;
638
639 // end of redis commands
640
641 RequestGet Get(std::string key, RetryNilFromMaster, const CommandControl& command_control);
642
643 RequestHget Hget(std::string key, std::string field, RetryNilFromMaster, const CommandControl& command_control);
644
645 RequestZscore Zscore(
646 std::string key,
647 std::string member,
649 const CommandControl& command_control
650 );
651
652 void Publish(std::string channel, std::string message, const CommandControl& command_control);
653
654 RequestScan Scan(size_t shard, const CommandControl& command_control);
655
656 RequestHscan Hscan(std::string key, const CommandControl& command_control);
657
658 RequestSscan Sscan(std::string key, const CommandControl& command_control);
659
660 RequestZscan Zscan(std::string key, const CommandControl& command_control);
661
662protected:
663 virtual RequestEvalCommon EvalCommon(
664 std::string script,
665 std::vector<std::string> keys,
666 std::vector<std::string> args,
667 const CommandControl& command_control
668 ) = 0;
669 virtual RequestEvalShaCommon EvalShaCommon(
670 std::string script_hash,
671 std::vector<std::string> keys,
672 std::vector<std::string> args,
673 const CommandControl& command_control
674 ) = 0;
675 virtual RequestGenericCommon GenericCommon(
676 std::string command,
677 std::vector<std::string> args,
678 size_t key_index,
679 const CommandControl& command_control
680 ) = 0;
681};
682
683std::string CreateTmpKey(const std::string& key, std::string prefix);
684
685} // namespace storages::redis
686
687USERVER_NAMESPACE_END