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