userver: userver/storages/redis/transaction.hpp Source File
Loading...
Searching...
No Matches
transaction.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/redis/transaction.hpp
4/// @brief @copybrief storages::redis::Transaction
5
6#include <memory>
7#include <string>
8
9#include <userver/storages/redis/bit_operation.hpp>
10#include <userver/storages/redis/command_options.hpp>
11#include <userver/storages/redis/request.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace storages::redis {
16
17/// @brief Atomic sequence of Redis commands (https://redis.io/topics/transactions), that is usually retrieved from
18/// storages::redis::Client::Multi().
19///
20/// @note Redis transaction implements isolation, but not
21/// all-or-nothing semantics (IOW a subcommand may fail, but the following
22/// subcommands will succeed).
23///
24/// Membef functions add commands to the `Transaction` object. For each added command a future-like object is returned.
25/// You can get the result of each transaction's subcommand by calling `Get()` method for these objects.
26/// Commands are be sent to a server after calling `Exec()` that returns `RequestExec` object.
27/// You should not call `Get()` method in a future-like subcommand's object
28/// before calling `Get()` method on `RequestExec` object.
29///
30/// @snippet redis/src/storages/redis/client_redistest.cpp redis transaction sample
32public:
33 enum class CheckShards { kNo, kSame };
34
35 virtual ~Transaction() = default;
36
37 /// Finish current atomic sequence of commands and send it to a server.
38 /// Returns 'future-like' request object.
39 /// The data will not be set for the future-like objects for subcommands if
40 /// `Get()` method of the returned object is not called or redis did not return an array with command responses.
41 /// In the last case `Get()` will throw a corresponding exception.
42 virtual RequestExec Exec(const CommandControl& command_control) = 0;
43
44 // redis commands:
45
46 virtual RequestAppend Append(std::string key, std::string value) = 0;
47
48 virtual RequestBitop Bitop(BitOperation op, std::string dest, std::vector<std::string> srcs) = 0;
49
50 virtual RequestDbsize Dbsize(size_t shard) = 0;
51
52 virtual RequestDecr Decr(std::string key) = 0;
53
54 virtual RequestDel Del(std::string key) = 0;
55
56 virtual RequestDel Del(std::vector<std::string> keys) = 0;
57
58 virtual RequestUnlink Unlink(std::string key) = 0;
59
60 virtual RequestUnlink Unlink(std::vector<std::string> keys) = 0;
61
62 virtual RequestExists Exists(std::string key) = 0;
63
64 virtual RequestExists Exists(std::vector<std::string> keys) = 0;
65
66 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl) = 0;
67
68 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member) = 0;
69
70 virtual RequestGeoadd Geoadd(std::string key, std::vector<GeoaddArg> point_members) = 0;
71
72 virtual RequestGeoradius Georadius(
73 std::string key,
74 Longitude lon,
75 Latitude lat,
76 double radius,
77 const GeoradiusOptions& georadius_options
78 ) = 0;
79
80 virtual RequestGeosearch
81 Geosearch(std::string key, std::string member, double radius, const GeosearchOptions& geosearch_options) = 0;
82
83 virtual RequestGeosearch Geosearch(
84 std::string key,
85 std::string member,
86 BoxWidth width,
87 BoxHeight height,
88 const GeosearchOptions& geosearch_options
89 ) = 0;
90
91 virtual RequestGeosearch Geosearch(
92 std::string key,
93 Longitude lon,
94 Latitude lat,
95 double radius,
96 const GeosearchOptions& geosearch_options
97 ) = 0;
98
99 virtual RequestGeosearch Geosearch(
100 std::string key,
101 Longitude lon,
102 Latitude lat,
103 BoxWidth width,
104 BoxHeight height,
105 const GeosearchOptions& geosearch_options
106 ) = 0;
107
108 virtual RequestGet Get(std::string key) = 0;
109
110 virtual RequestGetset Getset(std::string key, std::string value) = 0;
111
112 virtual RequestHdel Hdel(std::string key, std::string field) = 0;
113
114 virtual RequestHdel Hdel(std::string key, std::vector<std::string> fields) = 0;
115
116 virtual RequestHexists Hexists(std::string key, std::string field) = 0;
117
118 virtual RequestHget Hget(std::string key, std::string field) = 0;
119
120 virtual RequestHgetall Hgetall(std::string key) = 0;
121
122 virtual RequestHincrby Hincrby(std::string key, std::string field, int64_t increment) = 0;
123
124 virtual RequestHincrbyfloat Hincrbyfloat(std::string key, std::string field, double increment) = 0;
125
126 virtual RequestHkeys Hkeys(std::string key) = 0;
127
128 virtual RequestHlen Hlen(std::string key) = 0;
129
130 virtual RequestHmget Hmget(std::string key, std::vector<std::string> fields) = 0;
131
132 virtual RequestHmset Hmset(std::string key, std::vector<std::pair<std::string, std::string>> field_values) = 0;
133
134 virtual RequestHset Hset(std::string key, std::string field, std::string value) = 0;
135
136 virtual RequestHsetnx Hsetnx(std::string key, std::string field, std::string value) = 0;
137
138 virtual RequestHvals Hvals(std::string key) = 0;
139
140 virtual RequestIncr Incr(std::string key) = 0;
141
142 virtual RequestKeys Keys(std::string keys_pattern, size_t shard) = 0;
143
144 virtual RequestLindex Lindex(std::string key, int64_t index) = 0;
145
146 virtual RequestLlen Llen(std::string key) = 0;
147
148 virtual RequestLpop Lpop(std::string key) = 0;
149
150 virtual RequestLpush Lpush(std::string key, std::string value) = 0;
151
152 virtual RequestLpush Lpush(std::string key, std::vector<std::string> values) = 0;
153
154 virtual RequestLpushx Lpushx(std::string key, std::string element) = 0;
155
156 virtual RequestLrange Lrange(std::string key, int64_t start, int64_t stop) = 0;
157
158 virtual RequestLrem Lrem(std::string key, int64_t count, std::string element) = 0;
159
160 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop) = 0;
161
162 virtual RequestMget Mget(std::vector<std::string> keys) = 0;
163
164 virtual RequestMset Mset(std::vector<std::pair<std::string, std::string>> key_values) = 0;
165
166 virtual RequestPersist Persist(std::string key) = 0;
167
168 virtual RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl) = 0;
169
170 virtual RequestPing Ping(size_t shard) = 0;
171
172 virtual RequestPingMessage PingMessage(size_t shard, std::string message) = 0;
173
174 virtual RequestRename Rename(std::string key, std::string new_key) = 0;
175
176 virtual RequestRpop Rpop(std::string key) = 0;
177
178 virtual RequestRpush Rpush(std::string key, std::string value) = 0;
179
180 virtual RequestRpush Rpush(std::string key, std::vector<std::string> values) = 0;
181
182 virtual RequestRpushx Rpushx(std::string key, std::string element) = 0;
183
184 virtual RequestSadd Sadd(std::string key, std::string member) = 0;
185
186 virtual RequestSadd Sadd(std::string key, std::vector<std::string> members) = 0;
187
188 virtual RequestScard Scard(std::string key) = 0;
189
190 virtual RequestSet Set(std::string key, std::string value) = 0;
191
192 virtual RequestSet Set(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
193
194 virtual RequestSetIfExist SetIfExist(std::string key, std::string value) = 0;
195
196 virtual RequestSetIfExist SetIfExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
197
198 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value) = 0;
199
200 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
201
202 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value) = 0;
203
204 virtual RequestSetIfNotExistOrGet
205 SetIfNotExistOrGet(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
206
207 virtual RequestSetex Setex(std::string key, std::chrono::seconds seconds, std::string value) = 0;
208
209 virtual RequestSismember Sismember(std::string key, std::string member) = 0;
210
211 virtual RequestSmembers Smembers(std::string key) = 0;
212
213 virtual RequestSrandmember Srandmember(std::string key) = 0;
214
215 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count) = 0;
216
217 virtual RequestSrem Srem(std::string key, std::string member) = 0;
218
219 virtual RequestSrem Srem(std::string key, std::vector<std::string> members) = 0;
220
221 virtual RequestStrlen Strlen(std::string key) = 0;
222
223 virtual RequestTime Time(size_t shard) = 0;
224
225 virtual RequestTtl Ttl(std::string key) = 0;
226
227 virtual RequestType Type(std::string key) = 0;
228
229 virtual RequestZadd Zadd(std::string key, double score, std::string member) = 0;
230
231 virtual RequestZadd Zadd(std::string key, double score, std::string member, const ZaddOptions& options) = 0;
232
233 virtual RequestZadd Zadd(std::string key, std::vector<std::pair<double, std::string>> scored_members) = 0;
234
235 virtual RequestZadd
236 Zadd(std::string key, std::vector<std::pair<double, std::string>> scored_members, const ZaddOptions& options) = 0;
237
238 virtual RequestZaddIncr ZaddIncr(std::string key, double score, std::string member) = 0;
239
240 virtual RequestZaddIncrExisting ZaddIncrExisting(std::string key, double score, std::string member) = 0;
241
242 virtual RequestZcard Zcard(std::string key) = 0;
243
244 virtual RequestZcount Zcount(std::string key, double min, double max) = 0;
245
246 virtual RequestZrange Zrange(std::string key, int64_t start, int64_t stop) = 0;
247
248 virtual RequestZrangeWithScores ZrangeWithScores(std::string key, int64_t start, int64_t stop) = 0;
249
250 virtual RequestZrangebyscore Zrangebyscore(std::string key, double min, double max) = 0;
251
252 virtual RequestZrangebyscore Zrangebyscore(std::string key, std::string min, std::string max) = 0;
253
254 virtual RequestZrangebyscore
255 Zrangebyscore(std::string key, double min, double max, const RangeOptions& range_options) = 0;
256
257 virtual RequestZrangebyscore
258 Zrangebyscore(std::string key, std::string min, std::string max, const RangeOptions& range_options) = 0;
259
260 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(std::string key, double min, double max) = 0;
261
262 virtual RequestZrangebyscoreWithScores
263 ZrangebyscoreWithScores(std::string key, std::string min, std::string max) = 0;
264
265 virtual RequestZrangebyscoreWithScores
266 ZrangebyscoreWithScores(std::string key, double min, double max, const RangeOptions& range_options) = 0;
267
268 virtual RequestZrangebyscoreWithScores
269 ZrangebyscoreWithScores(std::string key, std::string min, std::string max, const RangeOptions& range_options) = 0;
270
271 virtual RequestZrem Zrem(std::string key, std::string member) = 0;
272
273 virtual RequestZrem Zrem(std::string key, std::vector<std::string> members) = 0;
274
275 virtual RequestZremrangebyrank Zremrangebyrank(std::string key, int64_t start, int64_t stop) = 0;
276
277 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, double min, double max) = 0;
278
279 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, std::string min, std::string max) = 0;
280
281 virtual RequestZscore Zscore(std::string key, std::string member) = 0;
282
283 // end of redis commands
284};
285
286using TransactionPtr = std::unique_ptr<Transaction>;
287
289public:
290 using Exception::Exception;
291};
292
294public:
295 using Exception::Exception;
296};
297
298} // namespace storages::redis
299
300USERVER_NAMESPACE_END