10#include <userver/storages/redis/impl/base.hpp>
11#include <userver/storages/redis/impl/wait_connected_mode.hpp>
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>
21USERVER_NAMESPACE_BEGIN
23namespace storages::
redis {
32inline constexpr RetryNilFromMaster kRetryNilFromMaster{};
45 virtual ~Client() =
default;
47 virtual size_t ShardsCount()
const = 0;
48 virtual bool IsInClusterMode()
const = 0;
50 virtual size_t ShardByKey(
const std::string& key)
const = 0;
52 void CheckShardIdx(size_t shard_idx)
const;
54 virtual const std::string& GetAnyKeyForShard(size_t shard_idx)
const = 0;
56 virtual std::shared_ptr<
Client> GetClientForShard(size_t shard_idx) = 0;
58 virtual void WaitConnectedOnce(USERVER_NAMESPACE::redis::RedisWaitConnected wait_connected) = 0;
62 virtual RequestAppend Append(std::string key, std::string value,
const CommandControl& command_control) = 0;
64 virtual RequestBitop Bitop(
67 std::vector<std::string> src_keys,
68 const CommandControl& command_control
71 virtual RequestDbsize Dbsize(size_t shard,
const CommandControl& command_control) = 0;
73 virtual RequestDecr Decr(std::string key,
const CommandControl& command_control) = 0;
75 virtual RequestDel Del(std::string key,
const CommandControl& command_control) = 0;
77 virtual RequestDel Del(std::vector<std::string> keys,
const CommandControl& command_control) = 0;
79 virtual RequestUnlink Unlink(std::string key,
const CommandControl& command_control) = 0;
81 virtual RequestUnlink Unlink(std::vector<std::string> keys,
const CommandControl& command_control) = 0;
83 template <
typename ScriptResult,
typename ReplyType = ScriptResult>
84 RequestEval<ScriptResult, ReplyType> Eval(
86 std::vector<std::string> keys,
87 std::vector<std::string> args,
88 const CommandControl& command_control
90 return RequestEval<ScriptResult, ReplyType>{
91 EvalCommon(std::move(script), std::move(keys), std::move(args), command_control)};
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
100 return RequestEvalSha<ScriptResult, ReplyType>{
101 EvalShaCommon(std::move(script_hash), std::move(keys), std::move(args), command_control)};
104 virtual RequestScriptLoad ScriptLoad(std::string script, size_t shard,
const CommandControl& command_control) = 0;
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
113 return RequestEval<std::decay_t<ScriptInfo>, ReplyType>{
114 EvalCommon(script_info.GetScript(), std::move(keys), std::move(args), command_control)};
117 virtual RequestExists Exists(std::string key,
const CommandControl& command_control) = 0;
119 virtual RequestExists Exists(std::vector<std::string> keys,
const CommandControl& command_control) = 0;
121 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl,
const CommandControl& command_control) = 0;
123 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member,
const CommandControl& command_control) = 0;
125 virtual RequestGeoadd
126 Geoadd(std::string key, std::vector<GeoaddArg> point_members,
const CommandControl& command_control) = 0;
128 virtual RequestGeoradius Georadius(
133 const GeoradiusOptions& georadius_options,
134 const CommandControl& command_control
137 virtual RequestGeosearch Geosearch(
141 const GeosearchOptions& geosearch_options,
142 const CommandControl& command_control
145 virtual RequestGeosearch Geosearch(
150 const GeosearchOptions& geosearch_options,
151 const CommandControl& command_control
154 virtual RequestGeosearch Geosearch(
159 const GeosearchOptions& geosearch_options,
160 const CommandControl& command_control
163 virtual RequestGeosearch Geosearch(
169 const GeosearchOptions& geosearch_options,
170 const CommandControl& command_control
173 virtual RequestGet Get(std::string key,
const CommandControl& command_control) = 0;
175 virtual RequestGetset Getset(std::string key, std::string value,
const CommandControl& command_control) = 0;
177 virtual RequestHdel Hdel(std::string key, std::string field,
const CommandControl& command_control) = 0;
180 Hdel(std::string key, std::vector<std::string> fields,
const CommandControl& command_control) = 0;
182 virtual RequestHexists Hexists(std::string key, std::string field,
const CommandControl& command_control) = 0;
184 virtual RequestHget Hget(std::string key, std::string field,
const CommandControl& command_control) = 0;
187 virtual RequestHgetall Hgetall(std::string key,
const CommandControl& command_control) = 0;
189 virtual RequestHincrby
190 Hincrby(std::string key, std::string field, int64_t increment,
const CommandControl& command_control) = 0;
192 virtual RequestHincrbyfloat
193 Hincrbyfloat(std::string key, std::string field,
double increment,
const CommandControl& command_control) = 0;
196 virtual RequestHkeys Hkeys(std::string key,
const CommandControl& command_control) = 0;
198 virtual RequestHlen Hlen(std::string key,
const CommandControl& command_control) = 0;
201 Hmget(std::string key, std::vector<std::string> fields,
const CommandControl& command_control) = 0;
203 virtual RequestHmset Hmset(
205 std::vector<std::pair<std::string, std::string>> field_values,
206 const CommandControl& command_control
209 virtual RequestHscan Hscan(std::string key, HscanOptions options,
const CommandControl& command_control) = 0;
212 Hset(std::string key, std::string field, std::string value,
const CommandControl& command_control) = 0;
214 virtual RequestHsetnx
215 Hsetnx(std::string key, std::string field, std::string value,
const CommandControl& command_control) = 0;
218 virtual RequestHvals Hvals(std::string key,
const CommandControl& command_control) = 0;
220 virtual RequestIncr Incr(std::string key,
const CommandControl& command_control) = 0;
222 [[
deprecated(
"use Scan")]]
virtual RequestKeys
223 Keys(std::string keys_pattern, size_t shard,
const CommandControl& command_control) = 0;
225 virtual RequestLindex Lindex(std::string key, int64_t index,
const CommandControl& command_control) = 0;
227 virtual RequestLlen Llen(std::string key,
const CommandControl& command_control) = 0;
229 virtual RequestLpop Lpop(std::string key,
const CommandControl& command_control) = 0;
231 virtual RequestLpush Lpush(std::string key, std::string value,
const CommandControl& command_control) = 0;
234 Lpush(std::string key, std::vector<std::string> values,
const CommandControl& command_control) = 0;
236 virtual RequestLpushx Lpushx(std::string key, std::string element,
const CommandControl& command_control) = 0;
238 virtual RequestLrange
239 Lrange(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
242 Lrem(std::string key, int64_t count, std::string element,
const CommandControl& command_control) = 0;
244 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
246 virtual RequestMget Mget(std::vector<std::string> keys,
const CommandControl& command_control) = 0;
249 Mset(std::vector<std::pair<std::string, std::string>> key_values,
const CommandControl& command_control) = 0;
251 virtual TransactionPtr Multi() = 0;
253 virtual TransactionPtr Multi(Transaction::CheckShards check_shards) = 0;
255 virtual RequestPersist Persist(std::string key,
const CommandControl& command_control) = 0;
257 virtual RequestPexpire
258 Pexpire(std::string key, std::chrono::milliseconds ttl,
const CommandControl& command_control) = 0;
260 virtual RequestPing Ping(size_t shard,
const CommandControl& command_control) = 0;
262 virtual RequestPingMessage Ping(size_t shard, std::string message,
const CommandControl& command_control) = 0;
265 Publish(std::string channel, std::string message,
const CommandControl& command_control, PubShard policy) = 0;
267 virtual void Spublish(std::string channel, std::string message,
const CommandControl& command_control) = 0;
269 virtual RequestRename Rename(std::string key, std::string new_key,
const CommandControl& command_control) = 0;
271 virtual RequestRpop Rpop(std::string key,
const CommandControl& command_control) = 0;
273 virtual RequestRpush Rpush(std::string key, std::string value,
const CommandControl& command_control) = 0;
276 Rpush(std::string key, std::vector<std::string> values,
const CommandControl& command_control) = 0;
278 virtual RequestRpushx Rpushx(std::string key, std::string element,
const CommandControl& command_control) = 0;
280 virtual RequestSadd Sadd(std::string key, std::string member,
const CommandControl& command_control) = 0;
283 Sadd(std::string key, std::vector<std::string> members,
const CommandControl& command_control) = 0;
285 virtual RequestScan Scan(size_t shard, ScanOptions options,
const CommandControl& command_control) = 0;
287 virtual RequestScard Scard(std::string key,
const CommandControl& command_control) = 0;
289 virtual RequestSet Set(std::string key, std::string value,
const CommandControl& command_control) = 0;
292 Set(std::string key, std::string value, std::chrono::milliseconds ttl,
const CommandControl& command_control) = 0;
294 virtual RequestSetIfExist SetIfExist(std::string key, std::string value,
const CommandControl& command_control) = 0;
296 virtual RequestSetIfExist SetIfExist(
299 std::chrono::milliseconds ttl,
300 const CommandControl& command_control
303 virtual RequestSetIfNotExist
304 SetIfNotExist(std::string key, std::string value,
const CommandControl& command_control) = 0;
306 virtual RequestSetIfNotExist SetIfNotExist(
309 std::chrono::milliseconds ttl,
310 const CommandControl& command_control
314 Setex(std::string key, std::chrono::seconds seconds, std::string value,
const CommandControl& command_control) = 0;
316 virtual RequestSismember Sismember(std::string key, std::string member,
const CommandControl& command_control) = 0;
319 virtual RequestSmembers Smembers(std::string key,
const CommandControl& command_control) = 0;
321 virtual RequestSrandmember Srandmember(std::string key,
const CommandControl& command_control) = 0;
323 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count,
const CommandControl& command_control) = 0;
325 virtual RequestSrem Srem(std::string key, std::string member,
const CommandControl& command_control) = 0;
328 Srem(std::string key, std::vector<std::string> members,
const CommandControl& command_control) = 0;
330 virtual RequestSscan Sscan(std::string key, SscanOptions options,
const CommandControl& command_control) = 0;
332 virtual RequestStrlen Strlen(std::string key,
const CommandControl& command_control) = 0;
334 virtual RequestTime Time(size_t shard,
const CommandControl& command_control) = 0;
336 virtual RequestTtl Ttl(std::string key,
const CommandControl& command_control) = 0;
338 virtual RequestType Type(std::string key,
const CommandControl& command_control) = 0;
341 Zadd(std::string key,
double score, std::string member,
const CommandControl& command_control) = 0;
343 virtual RequestZadd Zadd(
347 const ZaddOptions& options,
348 const CommandControl& command_control
351 virtual RequestZadd Zadd(
353 std::vector<std::pair<
double, std::string>> scored_members,
354 const CommandControl& command_control
357 virtual RequestZadd Zadd(
359 std::vector<std::pair<
double, std::string>> scored_members,
360 const ZaddOptions& options,
361 const CommandControl& command_control
364 virtual RequestZaddIncr
365 ZaddIncr(std::string key,
double score, std::string member,
const CommandControl& command_control) = 0;
367 virtual RequestZaddIncrExisting
368 ZaddIncrExisting(std::string key,
double score, std::string member,
const CommandControl& command_control) = 0;
370 virtual RequestZcard Zcard(std::string key,
const CommandControl& command_control) = 0;
372 virtual RequestZcount Zcount(std::string key,
double min,
double max,
const CommandControl& command_control) = 0;
374 virtual RequestZrange
375 Zrange(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
377 virtual RequestZrangeWithScores
378 ZrangeWithScores(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
380 virtual RequestZrangebyscore
381 Zrangebyscore(std::string key,
double min,
double max,
const CommandControl& command_control) = 0;
383 virtual RequestZrangebyscore
384 Zrangebyscore(std::string key, std::string min, std::string max,
const CommandControl& command_control) = 0;
386 virtual RequestZrangebyscore Zrangebyscore(
390 const RangeOptions& range_options,
391 const CommandControl& command_control
394 virtual RequestZrangebyscore Zrangebyscore(
398 const RangeOptions& range_options,
399 const CommandControl& command_control
402 virtual RequestZrangebyscoreWithScores
403 ZrangebyscoreWithScores(std::string key,
double min,
double max,
const CommandControl& command_control) = 0;
405 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
409 const CommandControl& command_control
412 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
416 const RangeOptions& range_options,
417 const CommandControl& command_control
420 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
424 const RangeOptions& range_options,
425 const CommandControl& command_control
428 virtual RequestZrem Zrem(std::string key, std::string member,
const CommandControl& command_control) = 0;
431 Zrem(std::string key, std::vector<std::string> members,
const CommandControl& command_control) = 0;
433 virtual RequestZremrangebyrank
434 Zremrangebyrank(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
436 virtual RequestZremrangebyscore
437 Zremrangebyscore(std::string key,
double min,
double max,
const CommandControl& command_control) = 0;
439 virtual RequestZremrangebyscore
440 Zremrangebyscore(std::string key, std::string min, std::string max,
const CommandControl& command_control) = 0;
442 virtual RequestZscan Zscan(std::string key, ZscanOptions options,
const CommandControl& command_control) = 0;
444 virtual RequestZscore Zscore(std::string key, std::string member,
const CommandControl& command_control) = 0;
448 RequestGet Get(std::string key, RetryNilFromMaster,
const CommandControl& command_control);
450 RequestHget Hget(std::string key, std::string field, RetryNilFromMaster,
const CommandControl& command_control);
453 Zscore(std::string key, std::string member, RetryNilFromMaster,
const CommandControl& command_control);
455 void Publish(std::string channel, std::string message,
const CommandControl& command_control);
457 RequestScan Scan(size_t shard,
const CommandControl& command_control);
459 RequestHscan Hscan(std::string key,
const CommandControl& command_control);
461 RequestSscan Sscan(std::string key,
const CommandControl& command_control);
463 RequestZscan Zscan(std::string key,
const CommandControl& command_control);
466 virtual RequestEvalCommon EvalCommon(
468 std::vector<std::string> keys,
469 std::vector<std::string> args,
470 const CommandControl& command_control
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
480std::string CreateTmpKey(
const std::string& key, std::string prefix);