clache 0.2.0

Small utilities for caching data
Documentation
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Globally-accessible storage for performant, path-indexed caching.
//!
//! The global cache supports storage of **any type** implementing [`Any`], [`Send`], and [`Sync`],
//! dynamically.
//!
//! It is recommended to use namespacing in the paths in order to avoid conflicts, but this is not
//! required. To ensure consistency, use `namespace://path` for namespacing, and if you don't use
//! namespacing, avoid paths that begin that way. To obtain a list of all currently cached paths in
//! a namespace, use [`namespaced_paths`].
//!
//! ## Type Safety
//!
//! Cached items must implement [`Any`], [`Send`], and [`Sync`] in order to be stored in the global
//! cache. Unless using a dyn method, like [`get_dyn`], the cache uses downcasting. If a
//! downcast fails, [`None`] is returned. This runtime check makes the cache slightly less
//! efficient than [`LocalCache`], which uses static typing.
//!
//! ## Examples
//!
//! ```rust
//! use std::sync::Arc;
//! use clache::prelude::*;
//!
//! fn load_texture() -> String {
//!     "texture data".to_string()
//! }
//!
//! // Slow first lookup
//! let texture = GlobalCache::get_or_else("assets/texture.png", || {
//!     Arc::new(load_texture())
//! });
//!
//! // ...
//!
//! // Much faster lookup, since "assets/texture.png" is already in the cache
//! let texture = GlobalCache::get_or_else("assets/texture.png", || {
//!     Arc::new(load_texture())
//! });
//!
//! ```
//!
//! If only a single type is needed, and the path is not uniquely identifying, use [`LocalCache`].
//!
//! [`LocalCache`]: crate::LocalCache
//! [`get_dyn`]: GlobalCache::get_dyn
//! [`namespaced_paths`]: GlobalCache::namespaced_paths

use std::{
    any::Any,
    collections::{HashMap, HashSet},
    sync::{Arc, LazyLock, RwLock},
};

use crate::fused_rw::FusedRw;

type Item = Arc<FusedRw<Option<Arc<dyn Any + Send + Sync>>>>;

static CACHE: LazyLock<RwLock<HashMap<String, Item>>> =
    LazyLock::new(|| RwLock::new(HashMap::new()));

/// The shared, global cache, supporting dynamic typing. See the [module-level docs] for more info.
///
/// [module-level docs]: crate::global_cache
pub struct GlobalCache {
    _p: std::marker::PhantomData<[u8]>,
}

impl GlobalCache {
    /// Checks if a value exists in the **global** cache, regardless of type.
    pub fn is_cached(path: impl AsRef<str>) -> bool {
        let Some(entry) = CACHE
            .read()
            .expect("Cache cannot poison")
            .get(path.as_ref())
            .cloned()
        else {
            return false;
        };

        entry.read().is_some()
    }

    /// Checks if a value exists in the **global** cache, regardless of type, asynchronously.
    pub async fn is_cached_async(path: impl AsRef<str>) -> bool {
        let Some(entry) = CACHE
            .read()
            .expect("Cache cannot poison")
            .get(path.as_ref())
            .cloned()
        else {
            return false;
        };

        entry.read_async().await.is_some()
    }

    /// Checks if a value exists in the **global** cache, is initialized, and is of the correct
    /// type.
    pub fn is_cached_as<T: Any + Send + Sync>(path: impl AsRef<str>) -> bool {
        let Some(entry) = CACHE
            .read()
            .expect("Cache cannot poison")
            .get(path.as_ref())
            .cloned()
        else {
            return false;
        };

        entry.read().as_ref().is_some_and(|e| e.is::<T>())
    }

    /// Checks if a value exists in the **global** cache, is initialized, and is of the correct
    /// type. Waits asynchronously.
    pub async fn is_cached_as_async<T: Any + Send + Sync>(path: impl AsRef<str>) -> bool {
        let Some(entry) = CACHE
            .read()
            .expect("Cache cannot poison")
            .get(path.as_ref())
            .cloned()
        else {
            return false;
        };

        entry
            .read_async()
            .await
            .as_ref()
            .is_some_and(|e| e.is::<T>())
    }

    /// Retrieves a value from the **global** cache, if it exists, without trying to downcast it.
    pub fn get_dyn(path: impl AsRef<str>) -> Option<Arc<dyn Any + Send + Sync>> {
        let entry = {
            let guard = CACHE.read().expect("Cache cannot poison");
            let entry = guard.get(path.as_ref())?.clone();
            drop(guard);
            entry
        };

        entry.read().clone()
    }

    /// Retrieves a value from the **global** cache, if it exists, without trying to downcast it,
    /// asynchronously.
    pub async fn get_dyn_async(path: impl AsRef<str>) -> Option<Arc<dyn Any + Send + Sync>> {
        let entry = {
            let guard = CACHE.read().expect("Cache cannot poison");
            let entry = guard.get(path.as_ref())?.clone();
            drop(guard);
            entry
        };

        entry.read_async().await.clone()
    }

    /// Retrieves a value from the **global** cache, if it exists. Also returns [`None`] if the
    /// cached value is of the wrong type.
    ///
    /// If you wish to insert a default value, use [`get_or`] instead.
    ///
    /// This is not suitable for async.
    ///
    /// [`get_or`]: Self::get_or
    pub fn get<T: Any + Send + Sync>(path: impl AsRef<str>) -> Option<Arc<T>> {
        Self::get_dyn(path).and_then(|v| v.downcast().ok())
    }

    /// Retrieves a value from the **global** cache, if it exists, asynchronously. Also returns
    /// [`None`] if the cached value is of the wrong type.
    ///
    /// If you wish to insert a default value, use [`get_or_async`] instead.
    ///
    /// This is not suitable for async.
    ///
    /// [`get_or_async`]: Self::get_or_async
    pub async fn get_async<T: Any + Send + Sync>(path: impl AsRef<str>) -> Option<Arc<T>> {
        Self::get_dyn_async(path)
            .await
            .and_then(|v| v.downcast().ok())
    }

    /// Retrieves a value from the **global** cache or adds `default` if it does not exist.
    /// Also returns [`None`] if the cached value is of the wrong type.
    ///
    /// This eagerly evaluates `default` even if the value already exists in the cache. It is
    /// generally better to use [`get_or_else`] or one of its variants instead.
    ///
    /// [`get_or_else`]: Self::get_or_else
    pub fn get_or<T: Any + Send + Sync>(path: impl AsRef<str>, default: Arc<T>) -> Option<Arc<T>> {
        if let Some(good) = Self::get_dyn(path.as_ref()) {
            return good.downcast().ok();
        }

        let entry = {
            let mut guard = CACHE.write().expect("Cache cannot poison");
            guard
                .entry(path.as_ref().to_string())
                .or_insert_with(|| Arc::new(FusedRw::new(None)))
                .clone()
        };

        if let Some(good) = entry.read().as_ref() {
            good.clone().downcast().ok()
        } else {
            assert!(
                entry.write().replace(default.clone()).is_none(),
                "Cached value should be None"
            );
            Some(default)
        }
    }

    /// Retrieves a value from the **global** cache **asynchronously** or adds `default` if it does
    /// not exist. Also returns [`None`] if the cached value is of the wrong type.
    ///
    /// This eagerly evaluates `default` even if the value already exists in the cache. It is
    /// generally better to use [`get_or_else_async`] or one of its variants instead.
    ///
    /// [`get_or_else_async`]: Self::get_or_else_async
    pub async fn get_or_async<T: Any + Send + Sync>(
        path: impl AsRef<str>,
        default: Arc<T>,
    ) -> Option<Arc<T>> {
        if let Some(good) = Self::get_dyn_async(path.as_ref()).await {
            return good.downcast().ok();
        }

        let entry = {
            let mut guard = CACHE.write().expect("Cache cannot poison");
            guard
                .entry(path.as_ref().to_string())
                .or_insert_with(|| Arc::new(FusedRw::new(None)))
                .clone()
        };

        if let Some(good) = entry.read_async().await.as_ref() {
            good.clone().downcast().ok()
        } else {
            assert!(
                entry.write_async().await.replace(default.clone()).is_none(),
                "Cached value should be None"
            );
            Some(default)
        }
    }

    /// Retrieves a value from the **global** cache or loads it from a closure if it does not exist.
    /// Also returns [`None`] if the cached value is of the wrong type.
    pub fn get_or_else<T: Any + Send + Sync, F: FnOnce() -> Arc<T>>(
        path: impl AsRef<str>,
        f: F,
    ) -> Option<Arc<T>> {
        if let Some(good) = Self::get_dyn(path.as_ref()) {
            return good.downcast().ok();
        }

        let entry = {
            let mut guard = CACHE.write().expect("Cache cannot poison");
            guard
                .entry(path.as_ref().to_string())
                .or_insert_with(|| Arc::new(FusedRw::new(None)))
                .clone()
        };

        if let Some(good) = entry.read().as_ref() {
            good.clone().downcast().ok()
        } else {
            let loaded = f();
            assert!(
                entry.write().replace(loaded.clone()).is_none(),
                "Cached value should be None"
            );
            Some(loaded)
        }
    }

    /// Retrieves a value from the **global** cache or loads it from an *async* closure if it does not
    /// exist. Also returns [`None`] if the cached value is of the wrong type.
    ///
    /// Unlike the non-async variants, this **not** an atomic operation. Your closure may be run more
    /// than once, but only the first run will write to the cache.
    pub async fn get_or_else_async<T: Any + Send + Sync, F: AsyncFnOnce() -> Arc<T>>(
        path: impl AsRef<str>,
        f: F,
    ) -> Option<Arc<T>> {
        if let Some(good) = Self::get_dyn_async(path.as_ref()).await {
            return good.downcast().ok();
        }

        let entry = {
            let mut guard = CACHE.write().expect("Cache cannot poison");
            guard
                .entry(path.as_ref().to_string())
                .or_insert_with(|| Arc::new(FusedRw::new(None)))
                .clone()
        };

        if let Some(good) = entry.read_async().await.as_ref() {
            good.clone().downcast().ok()
        } else {
            let loaded = f().await;
            assert!(
                entry.write_async().await.replace(loaded.clone()).is_none(),
                "Cached value should be None"
            );
            Some(loaded)
        }
    }

    /// Removes a path from the **global** cache, if it exists.
    ///
    /// Any values that have been acquired from the cache will remain valid until they are dropped.
    pub fn uncache(path: impl AsRef<str>) {
        // Don't care about entries; they'll drop eventually
        let mut guard = CACHE.write().expect("Cache cannot poison");
        guard.remove(path.as_ref());
    }

    /// Removes all paths from the **global** cache.
    ///
    /// This is dangerous, as it can cause other crates to lose their dependent values. Any values
    /// that have been acquired from the cache will remain valid until they are dropped.
    ///
    /// If you want a safer alternative, iterate and use [`uncache`].
    ///
    /// [`uncache`]: Self::uncache
    pub fn clear() {
        let mut guard = CACHE.write().expect("Cache cannot poison");
        guard.clear();
    }

    /// Returns all paths starting with `namespace`, stripping the namespace prefix.
    ///
    /// This is only a snapshot, and future insertions and removals will not be reflected. Some
    /// entries may be in the process of being written, but most functions will wait until they
    /// are ready.
    pub fn namespaced_paths(namespace: impl AsRef<str>) -> HashSet<String> {
        let ns = namespace.as_ref();
        let guard = CACHE.read().expect("Cache cannot poison");
        guard
            .iter()
            .filter_map(|(k, v)| k.strip_prefix(ns).map(|s| (s.to_string(), v)))
            .filter(|(_, v)| v.read().is_some())
            .map(|(k, _)| k)
            .collect()
    }

    /// Returns all paths starting with `namespace` **asynchronously**, stripping the namespace
    /// prefix.
    ///
    /// This is only a snapshot, and future insertions and removals will not be reflected. Some
    /// entries may be in the process of being written, but most functions will wait until they
    /// are ready.
    pub async fn namespaced_paths_async(namespace: impl AsRef<str>) -> HashSet<String> {
        let ns = namespace.as_ref();
        let namespaced: Vec<_> = {
            CACHE
                .read()
                .expect("Cache cannot poison")
                .iter()
                .filter_map(|(k, _)| k.strip_prefix(ns))
                .map(|s| s.to_string())
                .collect()
        };

        let mut hs = HashSet::new();
        for k in namespaced {
            let Some(entry) = CACHE.read().expect("Cache cannot poison").get(&k).cloned() else {
                continue;
            };

            if entry.read_async().await.is_some() {
                hs.insert(k);
            }
        }

        hs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::AtomicUsize;

    #[test]
    fn simple() {
        let string = GlobalCache::get_or_else("abc", || Arc::new("hello".to_string())).unwrap();
        assert_eq!(*string, "hello");
    }

    #[test]
    fn shared() {
        let one = GlobalCache::get_or_else("123", || {
            println!("Running one");
            Arc::new("hello".to_string())
        })
        .unwrap();
        let two = GlobalCache::get_or_else("123", || {
            println!("Running two");
            Arc::new("hello".to_string())
        })
        .unwrap();

        assert!(Arc::ptr_eq(&one, &two));
    }

    #[test]
    fn simultaneous30() {
        let mut handles = Vec::new();
        const N: usize = 30;
        let barrier = Arc::new(std::sync::Barrier::new(N));
        let runs = Arc::new(AtomicUsize::new(0));
        for _ in 0..N {
            let bar_clone = barrier.clone();
            let runs_clone = runs.clone();
            let handle = std::thread::spawn(move || {
                bar_clone.wait();
                let string = GlobalCache::get_or_else("xyz", || {
                    runs_clone.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                    Arc::new("hello".to_string())
                })
                .unwrap();
                assert_eq!(*string, "hello");
            });
            handles.push(handle);
        }
        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(runs.load(std::sync::atomic::Ordering::SeqCst), 1);
    }
}