10#include <userver/storages/redis/impl/base.hpp>
11#include <userver/storages/redis/impl/wait_connected_mode.hpp>
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>
20USERVER_NAMESPACE_BEGIN
31inline constexpr RetryNilFromMaster kRetryNilFromMaster{};
44 virtual ~Client() =
default;
46 virtual size_t ShardsCount()
const = 0;
48 virtual size_t ShardByKey(
const std::string& key)
const = 0;
50 void CheckShardIdx(size_t shard_idx)
const;
52 virtual const std::string& GetAnyKeyForShard(size_t shard_idx)
const = 0;
54 virtual std::shared_ptr<
Client> GetClientForShard(size_t shard_idx) = 0;
56 virtual void WaitConnectedOnce(
61 virtual RequestAppend Append(std::string key, std::string value,
62 const CommandControl& command_control) = 0;
64 virtual RequestDbsize Dbsize(size_t shard,
65 const CommandControl& command_control) = 0;
67 virtual RequestDel Del(std::string key,
68 const CommandControl& command_control) = 0;
70 virtual RequestDel Del(std::vector<std::string> keys,
71 const CommandControl& command_control) = 0;
73 virtual RequestUnlink Unlink(std::string key,
74 const CommandControl& command_control) = 0;
76 virtual RequestUnlink Unlink(std::vector<std::string> keys,
77 const CommandControl& command_control) = 0;
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)};
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),
95 virtual RequestScriptLoad ScriptLoad(
96 std::string script, size_t shard,
97 const CommandControl& command_control) = 0;
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),
108 virtual RequestExists Exists(std::string key,
109 const CommandControl& command_control) = 0;
111 virtual RequestExists Exists(std::vector<std::string> keys,
112 const CommandControl& command_control) = 0;
114 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl,
115 const CommandControl& command_control) = 0;
117 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member,
118 const CommandControl& command_control) = 0;
120 virtual RequestGeoadd Geoadd(std::string key,
121 std::vector<GeoaddArg> point_members,
122 const CommandControl& command_control) = 0;
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;
129 virtual RequestGeosearch Geosearch(std::string key, std::string member,
131 const GeosearchOptions& geosearch_options,
132 const CommandControl& command_control) = 0;
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;
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;
144 virtual RequestGeosearch Geosearch(std::string key, Longitude lon,
145 Latitude lat, BoxWidth width,
147 const GeosearchOptions& geosearch_options,
148 const CommandControl& command_control) = 0;
150 virtual RequestGet Get(std::string key,
151 const CommandControl& command_control) = 0;
153 virtual RequestGetset Getset(std::string key, std::string value,
154 const CommandControl& command_control) = 0;
156 virtual RequestHdel Hdel(std::string key, std::string field,
157 const CommandControl& command_control) = 0;
159 virtual RequestHdel Hdel(std::string key, std::vector<std::string> fields,
160 const CommandControl& command_control) = 0;
162 virtual RequestHexists Hexists(std::string key, std::string field,
163 const CommandControl& command_control) = 0;
165 virtual RequestHget Hget(std::string key, std::string field,
166 const CommandControl& command_control) = 0;
169 virtual RequestHgetall Hgetall(std::string key,
170 const CommandControl& command_control) = 0;
172 virtual RequestHincrby Hincrby(std::string key, std::string field,
174 const CommandControl& command_control) = 0;
176 virtual RequestHincrbyfloat Hincrbyfloat(
177 std::string key, std::string field,
double increment,
178 const CommandControl& command_control) = 0;
181 virtual RequestHkeys Hkeys(std::string key,
182 const CommandControl& command_control) = 0;
184 virtual RequestHlen Hlen(std::string key,
185 const CommandControl& command_control) = 0;
187 virtual RequestHmget Hmget(std::string key, std::vector<std::string> fields,
188 const CommandControl& command_control) = 0;
190 virtual RequestHmset Hmset(
192 std::vector<std::pair<std::string, std::string>> field_values,
193 const CommandControl& command_control) = 0;
195 virtual RequestHscan Hscan(std::string key, HscanOptions options,
196 const CommandControl& command_control) = 0;
198 virtual RequestHset Hset(std::string key, std::string field,
200 const CommandControl& command_control) = 0;
202 virtual RequestHsetnx Hsetnx(std::string key, std::string field,
204 const CommandControl& command_control) = 0;
207 virtual RequestHvals Hvals(std::string key,
208 const CommandControl& command_control) = 0;
210 virtual RequestIncr Incr(std::string key,
211 const CommandControl& command_control) = 0;
213 [[
deprecated(
"use Scan")]]
virtual RequestKeys Keys(
214 std::string keys_pattern, size_t shard,
215 const CommandControl& command_control) = 0;
217 virtual RequestLindex Lindex(std::string key, int64_t index,
218 const CommandControl& command_control) = 0;
220 virtual RequestLlen Llen(std::string key,
221 const CommandControl& command_control) = 0;
223 virtual RequestLpop Lpop(std::string key,
224 const CommandControl& command_control) = 0;
226 virtual RequestLpush Lpush(std::string key, std::string value,
227 const CommandControl& command_control) = 0;
229 virtual RequestLpush Lpush(std::string key, std::vector<std::string> values,
230 const CommandControl& command_control) = 0;
232 virtual RequestLpushx Lpushx(std::string key, std::string element,
233 const CommandControl& command_control) = 0;
235 virtual RequestLrange Lrange(std::string key, int64_t start, int64_t stop,
236 const CommandControl& command_control) = 0;
238 virtual RequestLrem Lrem(std::string key, int64_t count, std::string element,
239 const CommandControl& command_control) = 0;
241 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop,
242 const CommandControl& command_control) = 0;
244 virtual RequestMget Mget(std::vector<std::string> keys,
245 const CommandControl& command_control) = 0;
247 virtual RequestMset Mset(
248 std::vector<std::pair<std::string, std::string>> key_values,
249 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,
256 const CommandControl& command_control) = 0;
258 virtual RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl,
259 const CommandControl& command_control) = 0;
261 virtual RequestPing Ping(size_t shard,
262 const CommandControl& command_control) = 0;
264 virtual RequestPingMessage Ping(size_t shard, std::string message,
265 const CommandControl& command_control) = 0;
267 virtual void Publish(std::string channel, std::string message,
268 const CommandControl& command_control,
269 PubShard policy) = 0;
271 virtual RequestRename Rename(std::string key, std::string new_key,
272 const CommandControl& command_control) = 0;
274 virtual RequestRpop Rpop(std::string key,
275 const CommandControl& command_control) = 0;
277 virtual RequestRpush Rpush(std::string key, std::string value,
278 const CommandControl& command_control) = 0;
280 virtual RequestRpush Rpush(std::string key, std::vector<std::string> values,
281 const CommandControl& command_control) = 0;
283 virtual RequestRpushx Rpushx(std::string key, std::string element,
284 const CommandControl& command_control) = 0;
286 virtual RequestSadd Sadd(std::string key, std::string member,
287 const CommandControl& command_control) = 0;
289 virtual RequestSadd Sadd(std::string key, std::vector<std::string> members,
290 const CommandControl& command_control) = 0;
292 virtual RequestScan Scan(size_t shard, ScanOptions options,
293 const CommandControl& command_control) = 0;
295 virtual RequestScard Scard(std::string key,
296 const CommandControl& command_control) = 0;
298 virtual RequestSet Set(std::string key, std::string value,
299 const CommandControl& command_control) = 0;
301 virtual RequestSet Set(std::string key, std::string value,
302 std::chrono::milliseconds ttl,
303 const CommandControl& command_control) = 0;
305 virtual RequestSetIfExist SetIfExist(
306 std::string key, std::string value,
307 const CommandControl& command_control) = 0;
309 virtual RequestSetIfExist SetIfExist(
310 std::string key, std::string value, std::chrono::milliseconds ttl,
311 const CommandControl& command_control) = 0;
313 virtual RequestSetIfNotExist SetIfNotExist(
314 std::string key, std::string value,
315 const CommandControl& command_control) = 0;
317 virtual RequestSetIfNotExist SetIfNotExist(
318 std::string key, std::string value, std::chrono::milliseconds ttl,
319 const CommandControl& command_control) = 0;
321 virtual RequestSetex Setex(std::string key, std::chrono::seconds seconds,
323 const CommandControl& command_control) = 0;
325 virtual RequestSismember Sismember(std::string key, std::string member,
326 const CommandControl& command_control) = 0;
329 virtual RequestSmembers Smembers(std::string key,
330 const CommandControl& command_control) = 0;
332 virtual RequestSrandmember Srandmember(
333 std::string key,
const CommandControl& command_control) = 0;
335 virtual RequestSrandmembers Srandmembers(
336 std::string key, int64_t count,
337 const CommandControl& command_control) = 0;
339 virtual RequestSrem Srem(std::string key, std::string member,
340 const CommandControl& command_control) = 0;
342 virtual RequestSrem Srem(std::string key, std::vector<std::string> members,
343 const CommandControl& command_control) = 0;
345 virtual RequestSscan Sscan(std::string key, SscanOptions options,
346 const CommandControl& command_control) = 0;
348 virtual RequestStrlen Strlen(std::string key,
349 const CommandControl& command_control) = 0;
351 virtual RequestTime Time(size_t shard,
352 const CommandControl& command_control) = 0;
354 virtual RequestTtl Ttl(std::string key,
355 const CommandControl& command_control) = 0;
357 virtual RequestType Type(std::string key,
358 const CommandControl& command_control) = 0;
360 virtual RequestZadd Zadd(std::string key,
double score, std::string member,
361 const CommandControl& command_control) = 0;
363 virtual RequestZadd Zadd(std::string key,
double score, std::string member,
364 const ZaddOptions& options,
365 const CommandControl& command_control) = 0;
367 virtual RequestZadd Zadd(
369 std::vector<std::pair<
double, std::string>> scored_members,
370 const CommandControl& command_control) = 0;
372 virtual RequestZadd Zadd(
374 std::vector<std::pair<
double, std::string>> scored_members,
375 const ZaddOptions& options,
const CommandControl& command_control) = 0;
377 virtual RequestZaddIncr ZaddIncr(std::string key,
double score,
379 const CommandControl& command_control) = 0;
381 virtual RequestZaddIncrExisting ZaddIncrExisting(
382 std::string key,
double score, std::string member,
383 const CommandControl& command_control) = 0;
385 virtual RequestZcard Zcard(std::string key,
386 const CommandControl& command_control) = 0;
388 virtual RequestZcount Zcount(std::string key,
double min,
double max,
389 const CommandControl& command_control) = 0;
391 virtual RequestZrange Zrange(std::string key, int64_t start, int64_t stop,
392 const CommandControl& command_control) = 0;
394 virtual RequestZrangeWithScores ZrangeWithScores(
395 std::string key, int64_t start, int64_t stop,
396 const CommandControl& command_control) = 0;
398 virtual RequestZrangebyscore Zrangebyscore(
399 std::string key,
double min,
double max,
400 const CommandControl& command_control) = 0;
402 virtual RequestZrangebyscore Zrangebyscore(
403 std::string key, std::string min, std::string max,
404 const CommandControl& command_control) = 0;
406 virtual RequestZrangebyscore Zrangebyscore(
407 std::string key,
double min,
double max,
408 const RangeOptions& range_options,
409 const CommandControl& command_control) = 0;
411 virtual RequestZrangebyscore Zrangebyscore(
412 std::string key, std::string min, std::string max,
413 const RangeOptions& range_options,
414 const CommandControl& command_control) = 0;
416 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
417 std::string key,
double min,
double max,
418 const CommandControl& command_control) = 0;
420 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
421 std::string key, std::string min, std::string max,
422 const CommandControl& command_control) = 0;
424 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
425 std::string key,
double min,
double max,
426 const RangeOptions& range_options,
427 const CommandControl& command_control) = 0;
429 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
430 std::string key, std::string min, std::string max,
431 const RangeOptions& range_options,
432 const CommandControl& command_control) = 0;
434 virtual RequestZrem Zrem(std::string key, std::string member,
435 const CommandControl& command_control) = 0;
437 virtual RequestZrem Zrem(std::string key, std::vector<std::string> members,
438 const CommandControl& command_control) = 0;
440 virtual RequestZremrangebyrank Zremrangebyrank(
441 std::string key, int64_t start, int64_t stop,
442 const CommandControl& command_control) = 0;
444 virtual RequestZremrangebyscore Zremrangebyscore(
445 std::string key,
double min,
double max,
446 const CommandControl& command_control) = 0;
448 virtual RequestZremrangebyscore Zremrangebyscore(
449 std::string key, std::string min, std::string max,
450 const CommandControl& command_control) = 0;
452 virtual RequestZscan Zscan(std::string key, ZscanOptions options,
453 const CommandControl& command_control) = 0;
455 virtual RequestZscore Zscore(std::string key, std::string member,
456 const CommandControl& command_control) = 0;
460 RequestGet Get(std::string key, RetryNilFromMaster,
461 const CommandControl& command_control);
463 RequestHget Hget(std::string key, std::string field, RetryNilFromMaster,
464 const CommandControl& command_control);
466 RequestZscore Zscore(std::string key, std::string member, RetryNilFromMaster,
467 const CommandControl& command_control);
469 void Publish(std::string channel, std::string message,
470 const CommandControl& command_control);
472 RequestScan Scan(size_t shard,
const CommandControl& command_control);
474 RequestHscan Hscan(std::string key,
const CommandControl& command_control);
476 RequestSscan Sscan(std::string key,
const CommandControl& command_control);
478 RequestZscan Zscan(std::string key,
const CommandControl& command_control);
481 virtual RequestEvalCommon EvalCommon(
482 std::string script, std::vector<std::string> keys,
483 std::vector<std::string> args,
const CommandControl& command_control) = 0;
484 virtual RequestEvalShaCommon EvalShaCommon(
485 std::string script_hash, std::vector<std::string> keys,
486 std::vector<std::string> args,
const CommandControl& command_control) = 0;
489std::string CreateTmpKey(
const std::string& key, std::string prefix);