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
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 void Spublish(std::string channel, std::string message,
272 const CommandControl& command_control) = 0;
273
274 virtual RequestRename Rename(std::string key, std::string new_key,
275 const CommandControl& command_control) = 0;
276
277 virtual RequestRpop Rpop(std::string key,
278 const CommandControl& command_control) = 0;
279
280 virtual RequestRpush Rpush(std::string key, std::string value,
281 const CommandControl& command_control) = 0;
282
283 virtual RequestRpush Rpush(std::string key, std::vector<std::string> values,
284 const CommandControl& command_control) = 0;
285
286 virtual RequestRpushx Rpushx(std::string key, std::string element,
287 const CommandControl& command_control) = 0;
288
289 virtual RequestSadd Sadd(std::string key, std::string member,
290 const CommandControl& command_control) = 0;
291
292 virtual RequestSadd Sadd(std::string key, std::vector<std::string> members,
293 const CommandControl& command_control) = 0;
294
295 virtual RequestScan Scan(size_t shard, ScanOptions options,
296 const CommandControl& command_control) = 0;
297
298 virtual RequestScard Scard(std::string key,
299 const CommandControl& command_control) = 0;
300
301 virtual RequestSet Set(std::string key, std::string value,
302 const CommandControl& command_control) = 0;
303
304 virtual RequestSet Set(std::string key, std::string value,
305 std::chrono::milliseconds ttl,
306 const CommandControl& command_control) = 0;
307
308 virtual RequestSetIfExist SetIfExist(
309 std::string key, std::string value,
310 const CommandControl& command_control) = 0;
311
312 virtual RequestSetIfExist SetIfExist(
313 std::string key, std::string value, std::chrono::milliseconds ttl,
314 const CommandControl& command_control) = 0;
315
316 virtual RequestSetIfNotExist SetIfNotExist(
317 std::string key, std::string value,
318 const CommandControl& command_control) = 0;
319
320 virtual RequestSetIfNotExist SetIfNotExist(
321 std::string key, std::string value, std::chrono::milliseconds ttl,
322 const CommandControl& command_control) = 0;
323
324 virtual RequestSetex Setex(std::string key, std::chrono::seconds seconds,
325 std::string value,
326 const CommandControl& command_control) = 0;
327
328 virtual RequestSismember Sismember(std::string key, std::string member,
329 const CommandControl& command_control) = 0;
330
331 // use Sscan in case of a big set
332 virtual RequestSmembers Smembers(std::string key,
333 const CommandControl& command_control) = 0;
334
335 virtual RequestSrandmember Srandmember(
336 std::string key, const CommandControl& command_control) = 0;
337
338 virtual RequestSrandmembers Srandmembers(
339 std::string key, int64_t count,
340 const CommandControl& command_control) = 0;
341
342 virtual RequestSrem Srem(std::string key, std::string member,
343 const CommandControl& command_control) = 0;
344
345 virtual RequestSrem Srem(std::string key, std::vector<std::string> members,
346 const CommandControl& command_control) = 0;
347
348 virtual RequestSscan Sscan(std::string key, SscanOptions options,
349 const CommandControl& command_control) = 0;
350
351 virtual RequestStrlen Strlen(std::string key,
352 const CommandControl& command_control) = 0;
353
354 virtual RequestTime Time(size_t shard,
355 const CommandControl& command_control) = 0;
356
357 virtual RequestTtl Ttl(std::string key,
358 const CommandControl& command_control) = 0;
359
360 virtual RequestType Type(std::string key,
361 const CommandControl& command_control) = 0;
362
363 virtual RequestZadd Zadd(std::string key, double score, std::string member,
364 const CommandControl& command_control) = 0;
365
366 virtual RequestZadd Zadd(std::string key, double score, std::string member,
367 const ZaddOptions& options,
368 const CommandControl& command_control) = 0;
369
370 virtual RequestZadd Zadd(
371 std::string key,
372 std::vector<std::pair<double, std::string>> scored_members,
373 const CommandControl& command_control) = 0;
374
375 virtual RequestZadd Zadd(
376 std::string key,
377 std::vector<std::pair<double, std::string>> scored_members,
378 const ZaddOptions& options, const CommandControl& command_control) = 0;
379
380 virtual RequestZaddIncr ZaddIncr(std::string key, double score,
381 std::string member,
382 const CommandControl& command_control) = 0;
383
384 virtual RequestZaddIncrExisting ZaddIncrExisting(
385 std::string key, double score, std::string member,
386 const CommandControl& command_control) = 0;
387
388 virtual RequestZcard Zcard(std::string key,
389 const CommandControl& command_control) = 0;
390
391 virtual RequestZcount Zcount(std::string key, double min, double max,
392 const CommandControl& command_control) = 0;
393
394 virtual RequestZrange Zrange(std::string key, int64_t start, int64_t stop,
395 const CommandControl& command_control) = 0;
396
397 virtual RequestZrangeWithScores ZrangeWithScores(
398 std::string key, int64_t start, int64_t stop,
399 const CommandControl& command_control) = 0;
400
401 virtual RequestZrangebyscore Zrangebyscore(
402 std::string key, double min, double max,
403 const CommandControl& command_control) = 0;
404
405 virtual RequestZrangebyscore Zrangebyscore(
406 std::string key, std::string min, std::string max,
407 const CommandControl& command_control) = 0;
408
409 virtual RequestZrangebyscore Zrangebyscore(
410 std::string key, double min, double max,
411 const RangeOptions& range_options,
412 const CommandControl& command_control) = 0;
413
414 virtual RequestZrangebyscore Zrangebyscore(
415 std::string key, std::string min, std::string max,
416 const RangeOptions& range_options,
417 const CommandControl& command_control) = 0;
418
419 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
420 std::string key, double min, double max,
421 const CommandControl& command_control) = 0;
422
423 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
424 std::string key, std::string min, std::string max,
425 const CommandControl& command_control) = 0;
426
427 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
428 std::string key, double min, double max,
429 const RangeOptions& range_options,
430 const CommandControl& command_control) = 0;
431
432 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
433 std::string key, std::string min, std::string max,
434 const RangeOptions& range_options,
435 const CommandControl& command_control) = 0;
436
437 virtual RequestZrem Zrem(std::string key, std::string member,
438 const CommandControl& command_control) = 0;
439
440 virtual RequestZrem Zrem(std::string key, std::vector<std::string> members,
441 const CommandControl& command_control) = 0;
442
443 virtual RequestZremrangebyrank Zremrangebyrank(
444 std::string key, int64_t start, int64_t stop,
445 const CommandControl& command_control) = 0;
446
447 virtual RequestZremrangebyscore Zremrangebyscore(
448 std::string key, double min, double max,
449 const CommandControl& command_control) = 0;
450
451 virtual RequestZremrangebyscore Zremrangebyscore(
452 std::string key, std::string min, std::string max,
453 const CommandControl& command_control) = 0;
454
455 virtual RequestZscan Zscan(std::string key, ZscanOptions options,
456 const CommandControl& command_control) = 0;
457
458 virtual RequestZscore Zscore(std::string key, std::string member,
459 const CommandControl& command_control) = 0;
460
461 // end of redis commands
462
463 RequestGet Get(std::string key, RetryNilFromMaster,
464 const CommandControl& command_control);
465
466 RequestHget Hget(std::string key, std::string field, RetryNilFromMaster,
467 const CommandControl& command_control);
468
469 RequestZscore Zscore(std::string key, std::string member, RetryNilFromMaster,
470 const CommandControl& command_control);
471
472 void Publish(std::string channel, std::string message,
473 const CommandControl& command_control);
474
475 RequestScan Scan(size_t shard, const CommandControl& command_control);
476
477 RequestHscan Hscan(std::string key, const CommandControl& command_control);
478
479 RequestSscan Sscan(std::string key, const CommandControl& command_control);
480
481 RequestZscan Zscan(std::string key, const CommandControl& command_control);
482
483 protected:
484 virtual RequestEvalCommon EvalCommon(
485 std::string script, std::vector<std::string> keys,
486 std::vector<std::string> args, const CommandControl& command_control) = 0;
487 virtual RequestEvalShaCommon EvalShaCommon(
488 std::string script_hash, std::vector<std::string> keys,
489 std::vector<std::string> args, const CommandControl& command_control) = 0;
490};
491
492std::string CreateTmpKey(const std::string& key, std::string prefix);
493
494} // namespace storages::redis
495
496USERVER_NAMESPACE_END