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
//! Backend implementations for different storage systems.
//!
//! This module provides various cache backend implementations that can be used
//! with the fncache library. Each backend implements the `CacheBackend` trait,
//! which defines a common interface for cache operations such as get, set, remove,
//! and clear.
//!
//! # Available Backends
//!
//! * **Memory Backend** (always available): In-memory cache using `dashmap` with support
//! for configurable eviction policies (LRU, LFU).
//!
//! * **File Backend** (with `file-backend` feature): Persistent cache stored on disk with
//! optional compression.
//!
//! * **Redis Backend** (with `redis-backend` feature): Distributed cache using Redis.
//!
//! * **RocksDB Backend** (with `rocksdb-backend` feature): Persistent embedded key-value
//! store with high performance.
//!
//! * **WASM Backend** (with `wasm` feature): Backend optimized for WebAssembly environments.
//!
//! # Example: Using the Memory Backend
//!
//! ```
//! use fncache::backends::{CacheBackend, memory::MemoryBackend};
//!
//! # async fn example() -> fncache::Result<()> {
//! let backend = MemoryBackend::new();
//!
//! // Store a value
//! let key = "user:123".to_string();
//! let value = vec![1, 2, 3, 4];
//! backend.set(key.clone(), value.clone(), None).await?;
//!
//! // Retrieve the value
//! let result = backend.get(&key).await?;
//! assert_eq!(result, Some(value));
//! # Ok(())
//! # }
//! ```
use async_trait;
use ;
/// A key in the cache.
///
/// Keys are represented as strings for maximum flexibility and compatibility
/// across different backend implementations. For best performance, keep keys
/// relatively short and avoid extremely long strings.
pub type Key = String;
/// A value in the cache.
///
/// Values are stored as byte vectors (`Vec<u8>`), allowing any serializable data
/// to be cached. You'll typically need to serialize your data structures to bytes
/// before storing them and deserialize them after retrieval.
///
/// The `fncache` proc macro handles serialization/deserialization automatically
/// for cached functions.
pub type Value = ;
/// Trait defining the interface for all cache backends.
///
/// This trait provides a uniform interface for interacting with different cache
/// storage systems. All cache backends must implement this trait to be used with
/// the fncache library.
///
/// The trait requires implementing five async methods for basic cache operations:
/// get, set, remove, contains_key, and clear. It also requires that implementors
/// are Send, Sync, and implement Debug.
///
/// # Examples
///
/// Implementing a custom cache backend:
///
/// ```
/// use async_trait::async_trait;
/// use fncache::backends::{CacheBackend, Key, Value};
/// use std::collections::HashMap;
/// use std::sync::Mutex;
/// use std::time::Duration;
///
/// #[derive(Debug)]
/// struct MyCustomBackend {
/// store: Mutex<HashMap<Key, Value>>,
/// }
///
/// impl MyCustomBackend {
/// fn new() -> Self {
/// Self {
/// store: Mutex::new(HashMap::new()),
/// }
/// }
/// }
///
/// #[async_trait]
/// impl CacheBackend for MyCustomBackend {
/// async fn get(&self, key: &Key) -> fncache::Result<Option<Value>> {
/// let store = self.store.lock().unwrap();
/// Ok(store.get(key).cloned())
/// }
///
/// async fn set(&self, key: Key, value: Value, _ttl: Option<Duration>) -> fncache::Result<()> {
/// let mut store = self.store.lock().unwrap();
/// store.insert(key, value);
/// Ok(())
/// }
///
/// async fn remove(&self, key: &Key) -> fncache::Result<()> {
/// let mut store = self.store.lock().unwrap();
/// store.remove(key);
/// Ok(())
/// }
///
/// async fn contains_key(&self, key: &Key) -> fncache::Result<bool> {
/// let store = self.store.lock().unwrap();
/// Ok(store.contains_key(key))
/// }
///
/// async fn clear(&self) -> fncache::Result<()> {
/// let mut store = self.store.lock().unwrap();
/// store.clear();
/// Ok(())
/// }
/// }
/// ```
/// A boxed cache backend that can be used as a trait object.
///
/// This type alias represents a heap-allocated `CacheBackend` trait object.
/// It's useful when you need to store or pass around different backend
/// implementations through a common interface.
///
/// # Examples
///
/// ```
/// use fncache::backends::{Backend, memory::MemoryBackend};
///
/// fn create_backend() -> Backend {
/// Box::new(MemoryBackend::new())
/// }
/// ```
pub type Backend = ;