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