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
/*! A macro for defining functions that wrap a static-ref cache object. # Options: 1.) Use the default unbounded cache ```rust,ignore cached!{CACHE_NAME >> func_name(arg1: arg1_type, arg2: arg2_type) -> return_type = { <regular function body> }} ``` 2.) Use an explicitly specified cache-type, but let the macro instantiate it. The cache-type is expected to have a `new` method that takes no arguments. ```rust,ignore cached!{CACHE_NAME: SpecificCacheType >> func_name(arg1: arg1_type, arg2: arg2_type) -> return_type = { <regular function body> }} ``` 3.) Use an explicitly specified cache-type and provide the instantiated cache struct. This allows using caches that require args in their constructor or have a constructor method other than a simple `new`. ```rust,ignore cached!{CACHE_NAME: MyCache = MyCache::with_capacity(arg); >> func_name(arg1: arg1_type, arg2: arg2_type) -> return_type = { <regular function body> }} ``` Custom cache types must implement `cached::Cached` */ use std::hash::Hash; use std::cmp::Eq; pub mod macros; pub mod stores; pub use stores::*; /// Blank marker function to help enforce the `cached::Cached` trait on any /// explicitly specified cache types pub fn enforce_cached_impl<K: Hash + Eq, V, T: Cached<K, V>>(_: &::std::sync::MutexGuard<T>) {} pub trait Cached<K, V> { fn cache_get(&mut self, k: &K) -> Option<&V>; fn cache_set(&mut self, k: K, v: V); fn cache_size(&self) -> usize; fn cache_hits(&self) -> Option<u32> { None } fn cache_misses(&self) -> Option<u32> { None } fn cache_capacity(&self) -> Option<usize> { None } fn cache_lifespan(&self) -> Option<u64> { None } }