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