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
use std::borrow::Borrow;
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
use std::fmt;
use std::sync::{Arc, Mutex, MutexGuard};
use std::sync::atomic::{AtomicUsize, Ordering};
use lru_cache::LruCache;
pub struct ThreadSafeCache<K, V, S = RandomState>
where K: Eq + Hash, S: BuildHasher
{
inner: Mutex<LruCache<K, Arc<V>, S>>,
hits: AtomicUsize,
misses: AtomicUsize,
}
impl<K: Eq + Hash, V> ThreadSafeCache<K, V> {
#[inline]
pub fn new(capacity: usize) -> Self {
Self::with_hasher(capacity, RandomState::new())
}
}
impl<K, V, S> ThreadSafeCache<K, V, S>
where K: Eq + Hash, S: BuildHasher
{
pub fn with_hasher(capacity: usize, hasher: S) -> Self {
ThreadSafeCache{
inner: Mutex::new(LruCache::with_hasher(capacity, hasher)),
hits: AtomicUsize::new(0),
misses: AtomicUsize::new(0),
}
}
#[doc(hidden)]
fn lock(&self) -> MutexGuard<LruCache<K, Arc<V>, S>> {
self.inner.lock().expect("ThreadSafeCache mutex poisoned")
}
}
#[allow(dead_code)]
impl<K: Eq + Hash, V> ThreadSafeCache<K, V> {
pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: ?Sized + Eq + Hash
{
let y = self.lock().contains_key(key);
if !y { self.miss(); }
y
}
pub fn get<Q>(&self, key: &Q) -> Option<Arc<V>>
where K: Borrow<Q>, Q: ?Sized + Eq + Hash
{
match self.lock().get_mut(key) {
Some(v) => { self.hit(); Some(v.clone()) }
None => { self.miss(); None }
}
}
pub fn put(&self, k: K, v: V) -> Arc<V> {
let value = Arc::new(v);
self.lock().insert(k, value.clone()).unwrap_or_else(|| value)
}
pub fn insert(&self, k: K, v: V) -> Option<Arc<V>> {
self.lock().insert(k, Arc::new(v))
}
pub fn remove<Q>(&self, key: &Q) -> Option<Arc<V>>
where K: Borrow<Q>, Q: ?Sized + Eq + Hash
{
match self.lock().remove(key) {
r @ Some(_) => { self.hit(); r }
r @ None => { self.miss(); r }
}
}
pub fn capacity(&self) -> usize {
self.lock().capacity()
}
pub fn set_capacity(&self, capacity: usize) {
self.lock().set_capacity(capacity);
}
pub fn remove_lru(&self) -> Option<(K, Arc<V>)> {
self.lock().remove_lru()
}
pub fn len(&self) -> usize {
self.lock().len()
}
pub fn is_empty(&self) -> bool {
self.lock().is_empty()
}
pub fn clear(&self) {
self.lock().clear()
}
}
impl<K: Eq + Hash, V> ThreadSafeCache<K, V> {
fn hit(&self) -> usize {
let inc = 1;
self.hits.fetch_add(inc, Ordering::Relaxed) + inc
}
fn miss(&self) -> usize {
let inc = 1;
self.misses.fetch_add(inc, Ordering::Relaxed) + inc
}
}
impl<K :Eq + Hash, V> ThreadSafeCache<K, V> {
pub fn hits(&self) -> usize {
self.hits.load(Ordering::Relaxed)
}
pub fn misses(&self) -> usize {
self.misses.load(Ordering::Relaxed)
}
}
impl<K: Eq + Hash, V> fmt::Debug for ThreadSafeCache<K, V> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut ds = fmt.debug_struct("ThreadSafeCache");
if let Ok(inner) = self.inner.try_lock() {
ds.field("capacity", &inner.capacity());
ds.field("len", &inner.len());
}
ds.finish()
}
}