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