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