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 RequestExpire Expire(std::string key, std::chrono::seconds ttl, ExpireOptions options) = 0;
69
70 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member) = 0;
71
72 virtual RequestGeoadd Geoadd(std::string key, std::vector<GeoaddArg> point_members) = 0;
73
74 virtual RequestGeopos Geopos(std::string key, std::vector<std::string> members) = 0;
75
76 virtual RequestGeoradius Georadius(
77 std::string key,
78 Longitude lon,
79 Latitude lat,
80 double radius,
81 const GeoradiusOptions& georadius_options
82 ) = 0;
83
84 virtual RequestGeosearch Geosearch(
85 std::string key,
86 std::string member,
87 double radius,
88 const GeosearchOptions& geosearch_options
89 ) = 0;
90
91 virtual RequestGeosearch Geosearch(
92 std::string key,
93 std::string member,
94 BoxWidth width,
95 BoxHeight height,
96 const GeosearchOptions& geosearch_options
97 ) = 0;
98
99 virtual RequestGeosearch Geosearch(
100 std::string key,
101 Longitude lon,
102 Latitude lat,
103 double radius,
104 const GeosearchOptions& geosearch_options
105 ) = 0;
106
107 virtual RequestGeosearch Geosearch(
108 std::string key,
109 Longitude lon,
110 Latitude lat,
111 BoxWidth width,
112 BoxHeight height,
113 const GeosearchOptions& geosearch_options
114 ) = 0;
115
116 virtual RequestGet Get(std::string key) = 0;
117
118 virtual RequestGetset Getset(std::string key, std::string value) = 0;
119
120 virtual RequestHdel Hdel(std::string key, std::string field) = 0;
121
122 virtual RequestHdel Hdel(std::string key, std::vector<std::string> fields) = 0;
123
124 virtual RequestHexists Hexists(std::string key, std::string field) = 0;
125
126 virtual RequestHget Hget(std::string key, std::string field) = 0;
127
128 virtual RequestHgetall Hgetall(std::string key) = 0;
129
130 virtual RequestHincrby Hincrby(std::string key, std::string field, int64_t increment) = 0;
131
132 virtual RequestHincrbyfloat Hincrbyfloat(std::string key, std::string field, double increment) = 0;
133
134 virtual RequestHkeys Hkeys(std::string key) = 0;
135
136 virtual RequestHlen Hlen(std::string key) = 0;
137
138 virtual RequestHmget Hmget(std::string key, std::vector<std::string> fields) = 0;
139
140 virtual RequestHmset Hmset(std::string key, std::vector<std::pair<std::string, std::string>> field_values) = 0;
141
142 virtual RequestHset Hset(std::string key, std::string field, std::string value) = 0;
143
144 virtual RequestHsetnx Hsetnx(std::string key, std::string field, std::string value) = 0;
145
146 virtual RequestHvals Hvals(std::string key) = 0;
147
148 virtual RequestIncr Incr(std::string key) = 0;
149
150 virtual RequestKeys Keys(std::string keys_pattern, size_t shard) = 0;
151
152 virtual RequestLindex Lindex(std::string key, int64_t index) = 0;
153
154 virtual RequestLlen Llen(std::string key) = 0;
155
156 virtual RequestLpop Lpop(std::string key) = 0;
157
158 virtual RequestLpush Lpush(std::string key, std::string value) = 0;
159
160 virtual RequestLpush Lpush(std::string key, std::vector<std::string> values) = 0;
161
162 virtual RequestLpushx Lpushx(std::string key, std::string element) = 0;
163
164 virtual RequestLrange Lrange(std::string key, int64_t start, int64_t stop) = 0;
165
166 virtual RequestLrem Lrem(std::string key, int64_t count, std::string element) = 0;
167
168 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop) = 0;
169
170 virtual RequestMget Mget(std::vector<std::string> keys) = 0;
171
172 virtual RequestMset Mset(std::vector<std::pair<std::string, std::string>> key_values) = 0;
173
174 virtual RequestPersist Persist(std::string key) = 0;
175
176 virtual RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl) = 0;
177
178 virtual RequestPing Ping(size_t shard) = 0;
179
180 virtual RequestPingMessage PingMessage(size_t shard, std::string message) = 0;
181
182 virtual RequestRename Rename(std::string key, std::string new_key) = 0;
183
184 virtual RequestRpop Rpop(std::string key) = 0;
185
186 virtual RequestRpush Rpush(std::string key, std::string value) = 0;
187
188 virtual RequestRpush Rpush(std::string key, std::vector<std::string> values) = 0;
189
190 virtual RequestRpushx Rpushx(std::string key, std::string element) = 0;
191
192 virtual RequestSadd Sadd(std::string key, std::string member) = 0;
193
194 virtual RequestSadd Sadd(std::string key, std::vector<std::string> members) = 0;
195
196 virtual RequestScard Scard(std::string key) = 0;
197
198 virtual RequestSet Set(std::string key, std::string value) = 0;
199
200 virtual RequestSet Set(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
201
202 virtual RequestSetIfExist SetIfExist(std::string key, std::string value) = 0;
203
204 virtual RequestSetIfExist SetIfExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
205
206 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value) = 0;
207
208 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
209
210 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value) = 0;
211
212 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(
213 std::string key,
214 std::string value,
215 std::chrono::milliseconds ttl
216 ) = 0;
217
218 virtual RequestSetex Setex(std::string key, std::chrono::seconds seconds, std::string value) = 0;
219
220 virtual RequestSismember Sismember(std::string key, std::string member) = 0;
221
222 virtual RequestSmembers Smembers(std::string key) = 0;
223
224 virtual RequestSrandmember Srandmember(std::string key) = 0;
225
226 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count) = 0;
227
228 virtual RequestSrem Srem(std::string key, std::string member) = 0;
229
230 virtual RequestSrem Srem(std::string key, std::vector<std::string> members) = 0;
231
232 virtual RequestStrlen Strlen(std::string key) = 0;
233
234 virtual RequestTime Time(size_t shard) = 0;
235
236 virtual RequestTtl Ttl(std::string key) = 0;
237
238 virtual RequestType Type(std::string key) = 0;
239
240 virtual RequestZadd Zadd(std::string key, double score, std::string member) = 0;
241
242 virtual RequestZadd Zadd(std::string key, double score, std::string member, const ZaddOptions& options) = 0;
243
244 virtual RequestZadd Zadd(std::string key, std::vector<std::pair<double, std::string>> scored_members) = 0;
245
246 virtual RequestZadd Zadd(
247 std::string key,
248 std::vector<std::pair<double, std::string>> scored_members,
249 const ZaddOptions& options
250 ) = 0;
251
252 virtual RequestZaddIncr ZaddIncr(std::string key, double score, std::string member) = 0;
253
254 virtual RequestZaddIncrExisting ZaddIncrExisting(std::string key, double score, std::string member) = 0;
255
256 virtual RequestZcard Zcard(std::string key) = 0;
257
258 virtual RequestZcount Zcount(std::string key, double min, double max) = 0;
259
260 virtual RequestZrange Zrange(std::string key, int64_t start, int64_t stop) = 0;
261
262 virtual RequestZrangeWithScores ZrangeWithScores(std::string key, int64_t start, int64_t stop) = 0;
263
264 virtual RequestZrangebyscore Zrangebyscore(std::string key, double min, double max) = 0;
265
266 virtual RequestZrangebyscore Zrangebyscore(std::string key, std::string min, std::string max) = 0;
267
268 virtual RequestZrangebyscore Zrangebyscore(
269 std::string key,
270 double min,
271 double max,
272 const RangeOptions& range_options
273 ) = 0;
274
275 virtual RequestZrangebyscore Zrangebyscore(
276 std::string key,
277 std::string min,
278 std::string max,
279 const RangeOptions& range_options
280 ) = 0;
281
282 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(std::string key, double min, double max) = 0;
283
284 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
285 std::string key,
286 std::string min,
287 std::string max
288 ) = 0;
289
290 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
291 std::string key,
292 double min,
293 double max,
294 const RangeOptions& range_options
295 ) = 0;
296
297 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
298 std::string key,
299 std::string min,
300 std::string max,
301 const RangeOptions& range_options
302 ) = 0;
303
304 virtual RequestZrem Zrem(std::string key, std::string member) = 0;
305
306 virtual RequestZrem Zrem(std::string key, std::vector<std::string> members) = 0;
307
308 virtual RequestZremrangebyrank Zremrangebyrank(std::string key, int64_t start, int64_t stop) = 0;
309
310 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, double min, double max) = 0;
311
312 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, std::string min, std::string max) = 0;
313
314 virtual RequestZscore Zscore(std::string key, std::string member) = 0;
315
316 // end of redis commands
317};
318
319using TransactionPtr = std::unique_ptr<Transaction>;
320
322public:
323 using Exception::Exception;
324};
325
327public:
328 using Exception::Exception;
329};
330
331} // namespace storages::redis
332
333USERVER_NAMESPACE_END