1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (C) 2025-2026 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
//! This crate provides several `DataSrc` and `DataConn` derived structs to enable data access to
//! Redis within the Rust sabi framework.
//! `DataSrc` and `DataConn` derived structs are provided based on the Redis server configuration
//! and whether commands are executed synchronously or asynchronously. They include the following
//! types:
//!
//! ## Features
//!
//! ### Standalone configuration and synchronous commands
//!
//! `RedisDataSrc` and `RedisDataConn` are designed for a standalone Redis server and provide
//! synchronous connections for processing Redis commands.
//!
//! This type requires the `"standalone-sync"` feature to be enabled.
//!
//! #### Example
//!
//! ```rust
//! use errs;
//! use sabi;
//! #[cfg(feature = "standalone-sync")]
//! use sabi_redis::RedisDataSrc;
//!
//! fn main() -> errs::Result<()> {
//! #[cfg(feature = "standalone-sync")]
//! sabi::uses("redis", RedisDataSrc::new("redis://127.0.0.1:6379/10"));
//!
//! #[cfg(feature = "standalone-sync")]
//! let _auto_shutdown = sabi::setup()?;
//!
//! // ...
//! Ok(())
//! }
//! ```
//!
//! ### Standalone configuration and asynchronous commands
//!
//! `RedisAsyncDataSrc` and `RedisAsyncDataConn` are designed for a standalone Redis server and
//! provide asynchronous connections for processing Redis commands within a Tokio runtime.
//!
//! This type requires the `"standalone-async"` feature to be enabled.
//!
//! #### Example
//!
//! ```ignore
//! use errs;
//! use crate::RedisAsyncDataSrc;
//! use tokio;
//!
//! #[tokio::main]
//! async fn main() -> errs::Result<()> {
//! sabi::tokio::uses("redis", RedisAsyncDataSrc::new("redis://127.0.0.1:6379/10"));
//!
//! let _auto_shutdown = sabi_tokio::setup_async().await?;
//!
//! // ...
//! Ok(())
//! }
//! ```
//!
//! ### Sentinel configuration and synchronous commands
//!
//! `RedisSentinelDataSrc` and `RedisSentinelDataConn` are designed for a Redis Sentinel setup
//! and provide synchronous connections for processing Redis commands.
//!
//! This type requires the `"sentinel-sync"` feature to be enabled.
//!
//! #### Example
//!
//! ```rust
//! use errs;
//! use sabi;
//! #[cfg(feature = "sentinel-sync")]
//! use sabi_redis::{RedisSentinelDataSrc, RedisSentinelDataConn};
//!
//! fn main() -> errs::Result<()> {
//! #[cfg(feature = "sentinel-sync")]
//! sabi::uses(
//! "redis",
//! RedisSentinelDataSrc::new(
//! vec![
//! "redis://127.0.0.1:26479",
//! "redis://127.0.0.1:26480",
//! "redis://127.0.0.1:26481",
//! ],
//! "mymaster",
//! ),
//! );
//!
//! #[cfg(feature = "sentinel-sync")]
//! let _auto_shutdown = sabi::setup()?;
//!
//! // ...
//! Ok(())
//! }
//! ```
//!
//! ### Sentinel configuration and asynchronous commands
//!
//! `RedisSentinelAsyncDataSrc` and `RedisSentinelAsyncDataConn` are designed for a Redis Sentinel
//! setup and provide asynchronous connections for processing Redis commands within a Tokio runtime.
//!
//! This type requires the `"sentinel-async"` feature to be enabled.
//!
//! #### Example
//!
//! ```ignore
//! use errs;
//! use sabi_redis::RedisSentinelAsyncDataSrc;
//! use tokio;
//!
//! #[tokio::main]
//! async fn main() -> errs::Result<()> {
//! sabi::tokio::uses(
//! "redis",
//! RedisSentinelAsyncDataSrc::new(
//! vec![
//! "redis://127.0.0.1:26479",
//! "redis://127.0.0.1:26480",
//! "redis://127.0.0.1:26481",
//! ],
//! "mymaster",
//! ),
//! );
//!
//! let _auto_shutdown = sabi_tokio::setup_async().await?;
//!
//! // ...
//! Ok(())
//! }
//! ```
//!
//! ### Cluster configuration and synchronous commands
//!
//! `RedisClusterDataSrc` and `RedisClusterDataConn` are designed for a Redis Cluster setup
//! and provide synchronous connections for processing Redis commands.
//!
//! This type requires the `"cluster-sync"` feature to be enabled.
//!
//! #### Example
//!
//! ```rust
//! use errs;
//! use sabi;
//! #[cfg(feature = "cluster-sync")]
//! use sabi_redis::{RedisClusterDataSrc, RedisClusterDataConn};
//!
//! fn main() -> errs::Result<()> {
//! #[cfg(feature = "cluster-sync")]
//! sabi::uses(
//! "redis",
//! RedisClusterDataSrc::new(
//! vec![
//! "redis://127.0.0.1:7000",
//! "redis://127.0.0.1:7001",
//! "redis://127.0.0.1:7002",
//! ],
//! ),
//! );
//!
//! #[cfg(feature = "cluster-sync")]
//! let _auto_shutdown = sabi::setup()?;
//!
//! // ...
//! Ok(())
//! }
//! ```
//!
//! ## Transaction Rollback Alternative
//!
//! Redis does not support transactions like relational databases (RDBs) and lacks the ability to
//! roll back updated data. Therefore, when used in conjunction with other databases, an
//! inconsistency can occur if an error happens mid-process: the RDB's updates might be rolled
//! back, but the Redis updates remain. To address this, this crate offers three features to give
//! developers an opportunity to revert updates: *"force back"*, *"pre-commit"* and *"post-commit"*.
//!
//! ### Force Back
//!
//! The `DataConn` derived struct provided by this crate is equipped with the `add_force_back`
//! method. You can use this method to store functions in the `DataConn` that will be executed
//! during the rollback process within `sabi::DataHub::txn`.
//!
//! This is useful for things like deleting newly added data or reverting data that is unlikely to
//! have concurrent updates, such as session data. For data that might have concurrent updates,
//! it would likely require measures like using `WATCH`, `MULTI`, and `EXEC`.
//!
//! #### Example
//!
//! ```rust
//! use errs;
//! use redis::TypedCommands;
//! use sabi;
//! #[cfg(feature = "standalone-sync")]
//! use sabi_redis::RedisDataConn;
//!
//! #[cfg(feature = "standalone-sync")]
//! trait RedisSampleDataAcc: sabi::DataAcc {
//! fn data_access_method_with_add_force_back(&mut self, value: i64) -> errs::Result<()> {
//! let data_conn = self.get_data_conn::<RedisDataConn>("redis")?;
//! let mut redis_conn = data_conn.get_connection()?;
//!
//! redis_conn.set("value", value)
//! .map_err(|e| errs::Err::with_source("fail to set value", e))?;
//!
//! data_conn.add_force_back(|redis_conn| {
//! redis_conn.del("value")
//! .map_err(|e| errs::Err::with_source("fail to force back value", e))?;
//! Ok(())
//! });
//! Ok(())
//! }
//! }
//! ```
//!
//! ### Pre-Commit
//!
//! The `DataConn` derived struct provided by this crate is equipped with the `add_pre_commit`
//! method. You can use this method to store functions in the `DataConn` that will be executed
//! right before the commit process within `sabi::DataHub::txn`.
//!
//! By performing Redis updates after all other database updates, you can avoid the need for a
//! rollback if an error occurs with the other databases. This is a good option if you can ensure
//! that the updated data will not be re-fetched within the same transaction.
//!
//! #### Example
//!
//! ```rust
//! use errs;
//! use redis::TypedCommands;
//! use sabi;
//! #[cfg(feature = "standalone-sync")]
//! use sabi_redis::RedisDataConn;
//!
//! #[cfg(feature = "standalone-sync")]
//! trait RedisSampleDataAcc: sabi::DataAcc {
//! fn data_access_method_with_add_pre_commit(&mut self, value: i64) -> errs::Result<()> {
//! let data_conn = self.get_data_conn::<RedisDataConn>("redis")?;
//! data_conn.add_pre_commit(move |redis_conn| {
//! redis_conn.set("value", value)
//! .map_err(|e| errs::Err::with_source("fail to set value", e))?;
//! Ok(())
//! });
//! Ok(())
//! }
//! }
//! ```
//!
//! ### Post-Commit
//!
//! The `DataConn` derived struct provided by this crate is equipped with the `add_post_commit`
//! method. You can use this method to store functions in the `DataConn` that will be executed
//! after the commit process within `sabi::DataHub::txn`.
//!
//! Since it's executed after the commit of the updates to other databases is complete, there's no
//! need to consider rolling back Redis updates even if an error occurs with the other database
//! updates. However, you must be aware that if an error occurs during the Redis update itself,
//! the partial updates to Redis cannot be undone, and the other database updates will already be
//! committed. It would be necessary to ensure that the impact on the system is not critical if
//! such a situation occurs, and to enable error detection so that manual recovery can be performed
//! later.
//!
//! #### Example
//!
//! ```rust
//! use errs;
//! use redis::TypedCommands;
//! use sabi;
//! #[cfg(feature = "standalone-sync")]
//! use sabi_redis::RedisDataConn;
//!
//! #[cfg(feature = "standalone-sync")]
//! trait RedisSampleDataAcc: sabi::DataAcc {
//! fn data_access_method_with_add_pre_commit(&mut self, value: i64) -> errs::Result<()> {
//! let data_conn = self.get_data_conn::<RedisDataConn>("redis")?;
//! data_conn.add_post_commit(move |redis_conn| {
//! redis_conn.set("value", value)
//! .map_err(|e| errs::Err::with_source("fail to set value", e))?;
//! Ok(())
//! });
//! Ok(())
//! }
//! }
//! ```
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;