memory_cache/
macros.rs

1/// Macro for defining functions whose result needs to be cached.
2///
3/// # Example
4/// ```
5/// use once_cell::sync::Lazy;
6/// use std::sync::Mutex;
7/// use memory_cache::{MemoryCache, cached};
8///
9/// cached! {
10///     fn factorial(x: u128) -> u128 = {
11///         if x <= 1 {
12///             1
13///         } else {
14///             x * factorial(x - 1)
15///         }
16///     }
17/// }
18///
19/// assert_eq!(factorial(21), 51090942171709440000);
20/// ```
21#[macro_export]
22macro_rules! cached {
23    (fn $name:ident ($($arg:ident: $arg_type:ty), *) -> $ret:ty = $body:expr) => {
24        #[allow(unused_parens)]
25        fn $name($($arg: $arg_type), *) -> $ret {
26            // Static instance of `MemoryCache<A, B>`.
27            static CACHE: Lazy<Mutex<MemoryCache<($($arg_type),*), $ret>>> =
28                Lazy::new(|| Mutex::new(MemoryCache::new()));
29
30            // Create key.
31            let key = ($($arg.clone()), *);
32
33            // Acquires a mutex for check cached value.
34            let cache = CACHE.lock().unwrap();
35
36            match cache.get(&key) {
37                Some(value) => value.clone(),
38                None => {
39                    // Dispose mutex before executing body expression,
40                    // to avoid deadlock during a recursive call.
41                    drop(cache);
42
43                    // Execute the body expression.
44                    let value = (||$body)();
45
46                    // Acquires a mutex for add/update cache.
47                    let mut cache = CACHE.lock().unwrap();
48                    cache.insert(key, value, None);
49                    value.clone()
50                }
51            }
52        }
53    };
54}