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
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, Notify, RwLock};
use tokio::task::JoinHandle;
/// TTL-keyed async cache with thundering-herd protection.
///
/// `store` holds the actual `(value, expires_at)` entries; `fetch_locks`
/// holds per-key `Arc<Mutex<()>>` so concurrent callers asking for the
/// same key serialize behind a single fetch.
#[derive(Debug)]
pub struct Cache<K, V> {
store: Arc<RwLock<HashMap<K, (V, Instant)>>>,
fetch_locks: Arc<RwLock<HashMap<K, Arc<Mutex<()>>>>>,
cleanup_notify: Arc<Notify>,
_cleanup_handle: Option<JoinHandle<()>>,
}
impl<K, V> Cache<K, V>
where
K: Eq + Hash + Clone + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
{
/// Creates an empty cache without a background cleanup task.
///
/// Expired entries are evicted lazily on read.
pub fn new() -> Self {
Self {
store: Arc::new(RwLock::new(HashMap::new())),
fetch_locks: Arc::new(RwLock::new(HashMap::new())),
cleanup_notify: Arc::new(Notify::new()),
_cleanup_handle: None,
}
}
/// Creates a cache with a background task that evicts expired entries
/// and sweeps orphaned fetch-locks on the same cadence.
pub fn with_smart_cleanup() -> Self {
let store: Arc<RwLock<HashMap<K, (V, Instant)>>> = Arc::new(RwLock::new(HashMap::new()));
let fetch_locks: Arc<RwLock<HashMap<K, Arc<Mutex<()>>>>> =
Arc::new(RwLock::new(HashMap::new()));
let cleanup_notify = Arc::new(Notify::new());
let store_bg = Arc::clone(&store);
let fetch_locks_bg = Arc::clone(&fetch_locks);
let notify_bg = Arc::clone(&cleanup_notify);
let handle = tokio::spawn(async move {
const MIN_WAIT: Duration = Duration::from_secs(1);
const MAX_WAIT: Duration = Duration::from_secs(300);
loop {
let wait = {
let map = store_bg.read().await;
map.values()
.map(|(_, expire_at)| *expire_at)
.min()
.map(|next| {
next.saturating_duration_since(Instant::now())
.clamp(MIN_WAIT, MAX_WAIT)
})
.unwrap_or(MAX_WAIT)
};
// Edge-triggered Notify: future is constructed before await,
// so notify_waiters() during the read above is captured by
// tokio's permit and consumed on the next iteration.
tokio::select! {
_ = tokio::time::sleep(wait) => {}
_ = notify_bg.notified() => {}
}
let now = Instant::now();
let expired_keys: Vec<K> = {
let map = store_bg.read().await;
map.iter()
.filter(|(_, (_, exp))| now >= *exp)
.map(|(k, _)| k.clone())
.collect()
};
let removed = if expired_keys.is_empty() {
0
} else {
let mut map = store_bg.write().await;
let now = Instant::now(); // re-check under write lock
let before = map.len();
for key in &expired_keys {
if let Some((_, exp)) = map.get(key) {
if now >= *exp {
map.remove(key);
}
}
}
before - map.len()
};
if removed > 0 {
lighty_core::trace_info!(
removed = removed,
"Cache cleaned expired entries"
);
}
// Sweep orphaned fetch-locks: strong_count==1 means no caller
// holds a clone, try_lock succeeding means no waiter is active.
let swept = {
let mut locks = fetch_locks_bg.write().await;
let before = locks.len();
locks.retain(|_k, lock| {
Arc::strong_count(lock) > 1 || lock.try_lock().is_err()
});
before - locks.len()
};
if swept > 0 {
lighty_core::trace_debug!(
swept = swept,
"Cache swept orphan fetch-locks"
);
}
}
});
Self {
store,
fetch_locks,
cleanup_notify,
_cleanup_handle: Some(handle),
}
}
/// Inserts (or replaces) an entry with the given TTL.
pub async fn insert_with_ttl(&self, key: K, value: V, ttl: Duration) {
{
let mut store = self.store.write().await;
let expire_at = Instant::now() + ttl;
store.insert(key, (value, expire_at));
}
// Wake the cleanup task in case the new TTL is shorter than the
// current sleep. No-op if the cache was built with `new()`.
self.cleanup_notify.notify_waiters();
}
/// Returns the cached value for `key` if present and unexpired.
pub async fn get_with_ttl(&self, key: &K) -> Option<V> {
let store = self.store.read().await;
if let Some((value, expire_at)) = store.get(key) {
let now = Instant::now();
if now < *expire_at {
return Some(value.clone());
}
// Drop the read lock before requesting the write lock to
// avoid deadlocking with any concurrent writer.
drop(store);
// Double-checked locking: another task may have refreshed
// the entry between releasing the read lock and acquiring write.
let mut store = self.store.write().await;
match store.get(key) {
Some((value, expire_at)) => {
if Instant::now() < *expire_at {
return Some(value.clone());
}
store.remove(key);
}
None => {}
}
}
None
}
/// Get or compute with a Result-returning closure, with thundering
/// herd protection: multiple concurrent calls for the same key only
/// run the closure once. Errors are propagated and not cached.
pub async fn get_or_try_insert_with<F, Fut, E>(
&self,
key: K,
ttl: Duration,
f: F,
) -> Result<V, E>
where
F: FnOnce() -> Fut,
Fut: std::future::Future<Output = Result<V, E>>,
{
if let Some(v) = self.get_with_ttl(&key).await {
return Ok(v);
}
// Per-key Arc<Mutex<()>>: thundering-herd guard. Does NOT protect
// the value store; only ensures a single in-flight fetch per key.
let lock = {
let mut locks = self.fetch_locks.write().await;
locks
.entry(key.clone())
.or_insert_with(|| Arc::new(Mutex::new(())))
.clone()
};
let _guard = lock.lock().await;
// Double-check: another task may have populated the cache while
// we waited on the mutex.
if let Some(v) = self.get_with_ttl(&key).await {
return Ok(v);
}
match f().await {
Ok(value) => {
self.insert_with_ttl(key, value.clone(), ttl).await;
Ok(value)
}
Err(e) => Err(e),
}
}
/// Removes every entry in the cache.
pub async fn clear(&self) {
let mut store = self.store.write().await;
store.clear();
}
/// Keeps only the entries for which `keep(&K)` returns `true`.
pub async fn retain<P>(&self, keep: P)
where
P: Fn(&K) -> bool,
{
let mut store = self.store.write().await;
store.retain(|k, _| keep(k));
}
/// Returns the number of entries currently stored.
pub async fn len(&self) -> usize {
let store = self.store.read().await;
store.len()
}
/// Returns `true` if [`Self::len`] would be zero.
pub async fn is_empty(&self) -> bool {
let store = self.store.read().await;
store.is_empty()
}
/// Returns the number of pending fetch-locks currently held in the map.
#[doc(hidden)]
pub async fn fetch_locks_len(&self) -> usize {
let locks = self.fetch_locks.read().await;
locks.len()
}
}
impl<K, V> Default for Cache<K, V>
where
K: Eq + Hash + Clone + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
{
fn default() -> Self {
Self::new()
}
}
impl<K, V> Drop for Cache<K, V> {
fn drop(&mut self) {
if let Some(handle) = self._cleanup_handle.take() {
handle.abort();
}
}
}