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