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
//! Cache Backend Traits
//!
//! This module defines the trait abstractions that allow users to implement
//! custom cache backends for both L1 (in-memory) and L2 (distributed) caches.
//!
//! # Architecture
//!
//! - `CacheBackend`: Core trait for all cache implementations
//! - `L2CacheBackend`: Extended trait for L2 caches with TTL introspection
//! - `StreamingBackend`: Optional trait for event streaming capabilities
//!
//! # Example: Custom L1 Backend
//!
/// ```rust,ignore
/// use multi_tier_cache::error::{CacheError, CacheResult};
/// use bytes::Bytes;
/// use std::time::Duration;
/// use futures_util::future::BoxFuture;
/// use multi_tier_cache::CacheBackend;
///
/// struct MyCustomCache;
///
/// impl CacheBackend for MyCustomCache {
/// fn get<'a>(&'a self, _key: &'a str) -> BoxFuture<'a, Option<Bytes>> {
/// Box::pin(async move { None })
/// }
///
/// fn set_with_ttl<'a>(&'a self, _key: &'a str, _value: Bytes, _ttl: Duration) -> BoxFuture<'a, CacheResult<()>> {
/// Box::pin(async move { Ok(()) })
/// }
///
/// fn remove<'a>(&'a self, _key: &'a str) -> BoxFuture<'a, CacheResult<()>> {
/// Box::pin(async move { Ok(()) })
/// }
///
/// fn health_check(&self) -> BoxFuture<'_, bool> {
/// Box::pin(async move { true })
/// }
///
/// fn name(&self) -> &'static str { "MyCache" }
/// }
/// ```
use crateCacheResult;
use Bytes;
use BoxFuture;
use Duration;
/// Core cache backend trait for both L1 and L2 caches
///
/// This trait defines the essential operations that any cache backend must support.
/// Implement this trait to create custom L1 (in-memory) or L2 (distributed) cache backends.
///
/// # Required Operations
///
/// - `get`: Retrieve a value by key
/// - `set_with_ttl`: Store a value with a time-to-live
/// - `remove`: Delete a value by key
/// - `health_check`: Verify cache backend is operational
///
/// # Thread Safety
///
/// Implementations must be `Send + Sync` to support concurrent access across async tasks.
///
/// # Performance Considerations
///
/// - `get` operations should be optimized for low latency (target: <1ms for L1, <5ms for L2)
/// - `set_with_ttl` operations can be slightly slower but should still be fast
/// - Consider connection pooling for distributed backends
///
/// # Example
///
/// See module-level documentation for a complete example.
// (No longer needed since traits are now dyn-compatible)
/// Extended trait for L2 cache backends with TTL introspection
///
/// This trait extends `CacheBackend` with the ability to retrieve both a value
/// and its remaining TTL. This is essential for implementing efficient L2-to-L1
/// promotion with accurate TTL propagation.
///
/// # Use Cases
///
/// - L2-to-L1 promotion with same TTL
/// - TTL-based cache warming strategies
/// - Monitoring and analytics
///
/// # Example
///
/// ```rust,ignore,ignore
/// ```rust,ignore
/// use multi_tier_cache::error::{CacheError, CacheResult};
/// use bytes::Bytes;
/// use std::time::Duration;
/// use futures_util::future::BoxFuture;
/// use multi_tier_cache::{CacheBackend, L2CacheBackend};
///
/// struct MyDistributedCache;
///
/// impl CacheBackend for MyDistributedCache {
/// fn get<'a>(&'a self, _key: &'a str) -> BoxFuture<'a, Option<Bytes>> { Box::pin(async move { None }) }
/// fn set_with_ttl<'a>(&'a self, _k: &'a str, _v: Bytes, _t: Duration) -> BoxFuture<'a, CacheResult<()>> { Box::pin(async move { Ok(()) }) }
/// fn remove<'a>(&'a self, _k: &'a str) -> BoxFuture<'a, CacheResult<()>> { Box::pin(async move { Ok(()) }) }
/// fn health_check(&self) -> BoxFuture<'_, bool> { Box::pin(async move { true }) }
/// fn name(&self) -> &'static str { "MyDistCache" }
/// }
///
/// impl L2CacheBackend for MyDistributedCache {
/// fn get_with_ttl<'a>(&'a self, _key: &'a str) -> BoxFuture<'a, Option<(Bytes, Option<Duration>)>> {
/// Box::pin(async move { None })
/// }
/// }
/// ```
// (No longer needed since traits are now dyn-compatible)
/// Optional trait for cache backends that support event streaming
///
/// # Type Definitions
///
/// * `StreamEntry` - A single entry in a stream: `(id, fields)` where fields are `Vec<(key, value)>`
pub type StreamEntry = ;
/// Optional trait for cache backends that support event streaming
///
/// This trait defines operations for event-driven architectures using
/// streaming data structures like Redis Streams.
///
/// # Capabilities
///
/// - Publish events to streams with automatic trimming
/// - Read latest entries (newest first)
/// - Read entries with blocking support
///
/// # Backend Requirements
///
/// Not all cache backends support streaming. This trait is optional and
/// should only be implemented by backends with native streaming support
/// (e.g., Redis Streams, Kafka, Pulsar).
///
/// # Example
///
/// ```rust,ignore,ignore
/// ```rust,ignore
/// use multi_tier_cache::error::{CacheError, CacheResult};
/// use multi_tier_cache::{StreamingBackend, StreamEntry};
/// use futures_util::future::BoxFuture;
///
/// struct MyStreamingCache;
///
/// impl StreamingBackend for MyStreamingCache {
/// fn stream_add<'a>(
/// &'a self,
/// _stream_key: &'a str,
/// _fields: Vec<(String, String)>,
/// _maxlen: Option<usize>,
/// ) -> BoxFuture<'a, CacheResult<String>> {
/// Box::pin(async move { Ok("entry-id".to_string()) })
/// }
///
/// fn stream_read_latest<'a>(
/// &'a self,
/// _stream_key: &'a str,
/// _count: usize,
/// ) -> BoxFuture<'a, CacheResult<Vec<StreamEntry>>> {
/// Box::pin(async move { Ok(vec![]) })
/// }
///
/// fn stream_read<'a>(
/// &'a self,
/// _stream_key: &'a str,
/// _last_id: &'a str,
/// _count: usize,
/// _block_ms: Option<usize>,
/// ) -> BoxFuture<'a, CacheResult<Vec<StreamEntry>>> {
/// Box::pin(async move { Ok(vec![]) })
/// }
///
/// fn stream_create_group<'a>(&'a self, _: &'a str, _: &'a str, _: &'a str) -> BoxFuture<'a, CacheResult<()>> {
/// Box::pin(async move { Ok(()) })
/// }
///
/// fn stream_read_group<'a>(&'a self, _: &'a str, _: &'a str, _: &'a str, _: usize, _: Option<usize>) -> BoxFuture<'a, CacheResult<Vec<StreamEntry>>> {
/// Box::pin(async move { Ok(vec![]) })
/// }
///
/// fn stream_ack<'a>(&'a self, _: &'a str, _: &'a str, _: &'a [String]) -> BoxFuture<'a, CacheResult<()>> {
/// Box::pin(async move { Ok(()) })
/// }
/// }
/// ```