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