10#include <userver/storages/redis/base.hpp>
11#include <userver/storages/redis/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 {
41 virtual ~Client() =
default;
43 virtual size_t ShardsCount()
const = 0;
44 virtual bool IsInClusterMode()
const = 0;
46 virtual size_t ShardByKey(
const std::string& key)
const = 0;
48 void CheckShardIdx(size_t shard_idx)
const;
50 virtual const std::string& GetAnyKeyForShard(size_t shard_idx)
const = 0;
52 virtual std::shared_ptr<
Client> GetClientForShard(size_t shard_idx) = 0;
58 virtual RequestAppend Append(std::string key, std::string value,
const CommandControl& command_control) = 0;
60 virtual RequestBitop Bitop(
63 std::vector<std::string> src_keys,
64 const CommandControl& command_control
67 virtual RequestDbsize Dbsize(size_t shard,
const CommandControl& command_control) = 0;
69 virtual RequestDecr Decr(std::string key,
const CommandControl& command_control) = 0;
71 virtual RequestDel Del(std::string key,
const CommandControl& command_control) = 0;
73 virtual RequestDel Del(std::vector<std::string> keys,
const CommandControl& command_control) = 0;
75 virtual RequestUnlink Unlink(std::string key,
const CommandControl& command_control) = 0;
77 virtual RequestUnlink Unlink(std::vector<std::string> keys,
const CommandControl& command_control) = 0;
79 template <
typename ScriptResult,
typename ReplyType = ScriptResult>
80 RequestEval<ScriptResult, ReplyType> Eval(
82 std::vector<std::string> keys,
83 std::vector<std::string> args,
86 return RequestEval<ScriptResult, ReplyType>{
87 EvalCommon(std::move(script), std::move(keys), std::move(args), command_control)};
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,
96 return RequestEvalSha<ScriptResult, ReplyType>{
97 EvalShaCommon(std::move(script_hash), std::move(keys), std::move(args), command_control)};
100 virtual RequestScriptLoad ScriptLoad(std::string script, size_t shard,
const CommandControl& command_control) = 0;
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
109 return RequestEval<std::decay_t<ScriptInfo>, ReplyType>{
110 EvalCommon(script_info.GetScript(), std::move(keys), std::move(args), command_control)};
113 virtual RequestExists Exists(std::string key,
const CommandControl& command_control) = 0;
115 virtual RequestExists Exists(std::vector<std::string> keys,
const CommandControl& command_control) = 0;
117 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl,
const CommandControl& command_control) = 0;
119 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member,
const CommandControl& command_control) = 0;
121 virtual RequestGeoadd
122 Geoadd(std::string key, std::vector<GeoaddArg> point_members,
const CommandControl& command_control) = 0;
124 virtual RequestGeoradius Georadius(
129 const GeoradiusOptions& georadius_options,
130 const CommandControl& command_control
133 virtual RequestGeosearch Geosearch(
137 const GeosearchOptions& geosearch_options,
138 const CommandControl& command_control
141 virtual RequestGeosearch Geosearch(
146 const GeosearchOptions& geosearch_options,
147 const CommandControl& command_control
150 virtual RequestGeosearch Geosearch(
155 const GeosearchOptions& geosearch_options,
156 const CommandControl& command_control
159 virtual RequestGeosearch Geosearch(
165 const GeosearchOptions& geosearch_options,
166 const CommandControl& command_control
169 virtual RequestGet Get(std::string key,
const CommandControl& command_control) = 0;
171 virtual RequestGetset Getset(std::string key, std::string value,
const CommandControl& command_control) = 0;
173 virtual RequestHdel Hdel(std::string key, std::string field,
const CommandControl& command_control) = 0;
176 Hdel(std::string key, std::vector<std::string> fields,
const CommandControl& command_control) = 0;
178 virtual RequestHexists Hexists(std::string key, std::string field,
const CommandControl& command_control) = 0;
180 virtual RequestHget Hget(std::string key, std::string field,
const CommandControl& command_control) = 0;
183 virtual RequestHgetall Hgetall(std::string key,
const CommandControl& command_control) = 0;
185 virtual RequestHincrby
186 Hincrby(std::string key, std::string field, int64_t increment,
const CommandControl& command_control) = 0;
188 virtual RequestHincrbyfloat
189 Hincrbyfloat(std::string key, std::string field,
double increment,
const CommandControl& command_control) = 0;
192 virtual RequestHkeys Hkeys(std::string key,
const CommandControl& command_control) = 0;
194 virtual RequestHlen Hlen(std::string key,
const CommandControl& command_control) = 0;
197 Hmget(std::string key, std::vector<std::string> fields,
const CommandControl& command_control) = 0;
199 virtual RequestHmset Hmset(
201 std::vector<std::pair<std::string, std::string>> field_values,
205 virtual RequestHscan Hscan(std::string key, HscanOptions options,
const CommandControl& command_control) = 0;
208 Hset(std::string key, std::string field, std::string value,
const CommandControl& command_control) = 0;
210 virtual RequestHsetnx
211 Hsetnx(std::string key, std::string field, std::string value,
const CommandControl& command_control) = 0;
214 virtual RequestHvals Hvals(std::string key,
const CommandControl& command_control) = 0;
216 virtual RequestIncr Incr(std::string key,
const CommandControl& command_control) = 0;
218 [[
deprecated(
"use Scan")]]
virtual RequestKeys
219 Keys(std::string keys_pattern, size_t shard,
const CommandControl& command_control) = 0;
221 virtual RequestLindex Lindex(std::string key, int64_t index,
const CommandControl& command_control) = 0;
223 virtual RequestLlen Llen(std::string key,
const CommandControl& command_control) = 0;
225 virtual RequestLpop Lpop(std::string key,
const CommandControl& command_control) = 0;
227 virtual RequestLpush Lpush(std::string key, std::string value,
const CommandControl& command_control) = 0;
230 Lpush(std::string key, std::vector<std::string> values,
const CommandControl& command_control) = 0;
232 virtual RequestLpushx Lpushx(std::string key, std::string element,
const CommandControl& command_control) = 0;
234 virtual RequestLrange
235 Lrange(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
238 Lrem(std::string key, int64_t count, std::string element,
const CommandControl& command_control) = 0;
240 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
242 virtual RequestMget Mget(std::vector<std::string> keys,
const CommandControl& command_control) = 0;
245 Mset(std::vector<std::pair<std::string, std::string>> key_values,
const CommandControl& command_control) = 0;
247 virtual TransactionPtr Multi() = 0;
249 virtual TransactionPtr Multi(Transaction::CheckShards check_shards) = 0;
251 virtual RequestPersist Persist(std::string key,
const CommandControl& command_control) = 0;
253 virtual RequestPexpire
254 Pexpire(std::string key, std::chrono::milliseconds ttl,
const CommandControl& command_control) = 0;
256 virtual RequestPing Ping(size_t shard,
const CommandControl& command_control) = 0;
258 virtual RequestPingMessage Ping(size_t shard, std::string message,
const CommandControl& command_control) = 0;
261 Publish(std::string channel, std::string message,
const CommandControl& command_control, PubShard policy) = 0;
263 virtual void Spublish(std::string channel, std::string message,
const CommandControl& command_control) = 0;
265 virtual RequestRename Rename(std::string key, std::string new_key,
const CommandControl& command_control) = 0;
267 virtual RequestRpop Rpop(std::string key,
const CommandControl& command_control) = 0;
269 virtual RequestRpush Rpush(std::string key, std::string value,
const CommandControl& command_control) = 0;
272 Rpush(std::string key, std::vector<std::string> values,
const CommandControl& command_control) = 0;
274 virtual RequestRpushx Rpushx(std::string key, std::string element,
const CommandControl& command_control) = 0;
276 virtual RequestSadd Sadd(std::string key, std::string member,
const CommandControl& command_control) = 0;
279 Sadd(std::string key, std::vector<std::string> members,
const CommandControl& command_control) = 0;
281 virtual RequestScan Scan(size_t shard, ScanOptions options,
const CommandControl& command_control) = 0;
283 virtual RequestScard Scard(std::string key,
const CommandControl& command_control) = 0;
285 virtual RequestSet Set(std::string key, std::string value,
const CommandControl& command_control) = 0;
288 Set(std::string key, std::string value, std::chrono::milliseconds ttl,
const CommandControl& command_control) = 0;
290 virtual RequestSetIfExist SetIfExist(std::string key, std::string value,
const CommandControl& command_control) = 0;
292 virtual RequestSetIfExist SetIfExist(
295 std::chrono::milliseconds ttl,
299 virtual RequestSetIfNotExist
300 SetIfNotExist(std::string key, std::string value,
const CommandControl& command_control) = 0;
302 virtual RequestSetIfNotExist SetIfNotExist(
305 std::chrono::milliseconds ttl,
310 Setex(std::string key, std::chrono::seconds seconds, std::string value,
const CommandControl& command_control) = 0;
312 virtual RequestSismember Sismember(std::string key, std::string member,
const CommandControl& command_control) = 0;
315 virtual RequestSmembers Smembers(std::string key,
const CommandControl& command_control) = 0;
317 virtual RequestSrandmember Srandmember(std::string key,
const CommandControl& command_control) = 0;
319 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count,
const CommandControl& command_control) = 0;
321 virtual RequestSrem Srem(std::string key, std::string member,
const CommandControl& command_control) = 0;
324 Srem(std::string key, std::vector<std::string> members,
const CommandControl& command_control) = 0;
326 virtual RequestSscan Sscan(std::string key, SscanOptions options,
const CommandControl& command_control) = 0;
328 virtual RequestStrlen Strlen(std::string key,
const CommandControl& command_control) = 0;
330 virtual RequestTime Time(size_t shard,
const CommandControl& command_control) = 0;
332 virtual RequestTtl Ttl(std::string key,
const CommandControl& command_control) = 0;
334 virtual RequestType Type(std::string key,
const CommandControl& command_control) = 0;
337 Zadd(std::string key,
double score, std::string member,
const CommandControl& command_control) = 0;
339 virtual RequestZadd Zadd(
343 const ZaddOptions& options,
344 const CommandControl& command_control
347 virtual RequestZadd Zadd(
349 std::vector<std::pair<
double, std::string>> scored_members,
350 const CommandControl& command_control
353 virtual RequestZadd Zadd(
355 std::vector<std::pair<
double, std::string>> scored_members,
356 const ZaddOptions& options,
357 const CommandControl& command_control
360 virtual RequestZaddIncr
361 ZaddIncr(std::string key,
double score, std::string member,
const CommandControl& command_control) = 0;
363 virtual RequestZaddIncrExisting
364 ZaddIncrExisting(std::string key,
double score, std::string member,
const CommandControl& command_control) = 0;
366 virtual RequestZcard Zcard(std::string key,
const CommandControl& command_control) = 0;
368 virtual RequestZcount Zcount(std::string key,
double min,
double max,
const CommandControl& command_control) = 0;
370 virtual RequestZrange
371 Zrange(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
373 virtual RequestZrangeWithScores
374 ZrangeWithScores(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
376 virtual RequestZrangebyscore
377 Zrangebyscore(std::string key,
double min,
double max,
const CommandControl& command_control) = 0;
379 virtual RequestZrangebyscore
380 Zrangebyscore(std::string key, std::string min, std::string max,
const CommandControl& command_control) = 0;
382 virtual RequestZrangebyscore Zrangebyscore(
386 const RangeOptions& range_options,
387 const CommandControl& command_control
390 virtual RequestZrangebyscore Zrangebyscore(
394 const RangeOptions& range_options,
395 const CommandControl& command_control
398 virtual RequestZrangebyscoreWithScores
399 ZrangebyscoreWithScores(std::string key,
double min,
double max,
const CommandControl& command_control) = 0;
401 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
405 const CommandControl& command_control
408 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
412 const RangeOptions& range_options,
413 const CommandControl& command_control
416 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
420 const RangeOptions& range_options,
421 const CommandControl& command_control
424 virtual RequestZrem Zrem(std::string key, std::string member,
const CommandControl& command_control) = 0;
427 Zrem(std::string key, std::vector<std::string> members,
const CommandControl& command_control) = 0;
429 virtual RequestZremrangebyrank
430 Zremrangebyrank(std::string key, int64_t start, int64_t stop,
const CommandControl& command_control) = 0;
432 virtual RequestZremrangebyscore
433 Zremrangebyscore(std::string key,
double min,
double max,
const CommandControl& command_control) = 0;
435 virtual RequestZremrangebyscore
436 Zremrangebyscore(std::string key, std::string min, std::string max,
const CommandControl& command_control) = 0;
438 virtual RequestZscan Zscan(std::string key, ZscanOptions options,
const CommandControl& command_control) = 0;
440 virtual RequestZscore Zscore(std::string key, std::string member,
const CommandControl& command_control) = 0;
444 RequestGet Get(std::string key, RetryNilFromMaster,
const CommandControl& command_control);
446 RequestHget Hget(std::string key, std::string field, RetryNilFromMaster,
const CommandControl& command_control);
449 Zscore(std::string key, std::string member, RetryNilFromMaster,
const CommandControl& command_control);
451 void Publish(std::string channel, std::string message,
const CommandControl& command_control);
453 RequestScan Scan(size_t shard,
const CommandControl& command_control);
455 RequestHscan Hscan(std::string key,
const CommandControl& command_control);
457 RequestSscan Sscan(std::string key,
const CommandControl& command_control);
459 RequestZscan Zscan(std::string key,
const CommandControl& command_control);
462 virtual RequestEvalCommon EvalCommon(
464 std::vector<std::string> keys,
465 std::vector<std::string> args,
466 const CommandControl& command_control
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
476std::string CreateTmpKey(
const std::string& key, std::string prefix);