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
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::RwLock;
pub static DEFAULT_CACHE_CAPACITY: usize = 10_000;
pub(super) struct Cache<K, V>
where
K: Eq + Hash + Clone,
V: Clone,
{
map: RwLock<HashMap<K, V>>,
pub capacity: usize,
}
impl<K, V> Default for Cache<K, V>
where
K: Eq + Hash + Clone,
V: Clone,
{
fn default() -> Self {
Self::new(DEFAULT_CACHE_CAPACITY)
}
}
impl<K, V> Cache<K, V>
where
K: Eq + Hash + Clone,
V: Clone,
{
pub(super) fn new(capacity: usize) -> Self {
let map = RwLock::new(HashMap::with_capacity(capacity));
Cache { map, capacity }
}
pub(super) fn fresh(&self) -> Self {
Self::new(self.capacity)
}
pub(super) fn clear(&self) {
self.map.write().unwrap().clear();
}
pub(super) fn get_values<I>(&self, keys_iter: I) -> Option<Vec<Option<V>>>
where
I: Iterator<Item = K>,
{
if let Ok(ref mut cache) = self.map.try_read() {
Some(keys_iter.map(|k| cache.get(&k).cloned()).collect())
} else {
None
}
}
pub(super) fn set_values<I, J>(&self, keys_iter: I, values_iter: J)
where
I: Iterator<Item = K>,
J: Iterator<Item = Option<V>>,
{
if let Ok(ref mut cache) = self.map.try_read() {
if cache.len() >= self.capacity {
return;
}
} else {
return;
}
if let Ok(ref mut cache) = self.map.try_write() {
for (key, value) in keys_iter.zip(values_iter).filter(|(_, v)| v.is_some()) {
if cache.len() >= self.capacity {
break;
}
cache.insert(key, value.unwrap());
}
}
}
}