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
use core::*;
use std::sync::{Arc, RwLock};
use lru_cache::LRUCache;
pub trait WithCache
where
Self: Sized,
{
fn with_cache(&self, cache_size: usize) -> Self;
}
impl<M: Send + Sync + Clone + 'static> WithCache for Chain<M> {
fn with_cache(&self, cache_size: usize) -> Self {
self.mod_metric(|next| {
let cache: RwLock<LRUCache<String, M>> =
RwLock::new(LRUCache::with_capacity(cache_size));
Arc::new(move |kind, name, rate| {
let mut cache = cache.write().expect("Locking metric cache");
let name_str = String::from(name);
if let Some(value) = cache.get(&name_str) {
return value.clone();
}
let new_value = (next)(kind, name, rate).clone();
cache.insert(name_str, new_value.clone());
new_value
})
})
}
}
#[deprecated(since = "0.5.0", note = "Use `with_cache` instead.")]
pub fn cache<M, IC>(cache_size: usize, chain: IC) -> Chain<M>
where
M: Clone + Send + Sync + 'static,
IC: Into<Chain<M>>,
{
let chain = chain.into();
chain.with_cache(cache_size)
}