1 //! Least recently used (LRU) cache
3 //! Implements a cache with least recently used cache replacement policy.
4 //! A HashMap is used for fast access by a given key and a doubly linked list
5 //! is used to keep track of the cache access order.
7 use std
::collections
::{HashMap, hash_map::Entry}
;
8 use std
::marker
::PhantomData
;
10 /// Interface for getting values on cache misses.
11 pub trait Cacher
<K
, V
> {
12 /// Fetch a value for key on cache miss.
14 /// Whenever a cache miss occurs, the fetch method provides a corresponding value.
15 /// If no value can be obtained for the given key, None is returned, the cache is
16 /// not updated in that case.
17 fn fetch(&mut self, key
: K
) -> Result
<Option
<V
>, anyhow
::Error
>;
20 /// Node of the doubly linked list storing key and value
21 struct CacheNode
<K
, V
> {
22 // We need to additionally store the key to be able to remove it
23 // from the HashMap when removing the tail.
26 prev
: *mut CacheNode
<K
, V
>,
27 next
: *mut CacheNode
<K
, V
>,
28 // Dropcheck marker. See the phantom-data section in the rustonomicon.
29 _marker
: PhantomData
<Box
<CacheNode
<K
, V
>>>,
32 impl<K
, V
> CacheNode
<K
, V
> {
33 fn new(key
: K
, value
: V
) -> Self {
37 prev
: std
::ptr
::null_mut(),
38 next
: std
::ptr
::null_mut(),
44 /// LRU cache instance.
48 /// # use self::proxmox_backup::tools::lru_cache::{Cacher, LruCache};
49 /// # fn main() -> Result<(), anyhow::Error> {
50 /// struct LruCacher {};
52 /// impl Cacher<u64, u64> for LruCacher {
53 /// fn fetch(&mut self, key: u64) -> Result<Option<u64>, anyhow::Error> {
58 /// let mut cache = LruCache::new(3);
60 /// assert_eq!(cache.get_mut(1), None);
61 /// assert_eq!(cache.len(), 0);
63 /// cache.insert(1, 1);
64 /// cache.insert(2, 2);
65 /// cache.insert(3, 3);
66 /// cache.insert(4, 4);
67 /// assert_eq!(cache.len(), 3);
69 /// assert_eq!(cache.get_mut(1), None);
70 /// assert_eq!(cache.get_mut(2), Some(&mut 2));
71 /// assert_eq!(cache.get_mut(3), Some(&mut 3));
72 /// assert_eq!(cache.get_mut(4), Some(&mut 4));
77 /// assert_eq!(cache.len(), 0);
78 /// assert_eq!(cache.get_mut(2), None);
79 /// // access will fill in missing cache entry by fetching from LruCacher
80 /// assert_eq!(cache.access(2, &mut LruCacher {}).unwrap(), Some(&mut 2));
82 /// cache.insert(1, 1);
83 /// assert_eq!(cache.get_mut(1), Some(&mut 1));
86 /// assert_eq!(cache.len(), 0);
87 /// assert_eq!(cache.get_mut(1), None);
91 pub struct LruCache
<K
, V
> {
92 /// Quick access to individual nodes via the node pointer.
93 map
: HashMap
<K
, *mut CacheNode
<K
, V
>>,
94 /// Actual nodes stored in a linked list.
95 list
: LinkedList
<K
, V
>,
96 /// Max nodes the cache can hold, temporarily exceeded by 1 due to
97 /// implementation details.
99 // Dropcheck marker. See the phantom-data section in the rustonomicon.
100 _marker
: PhantomData
<Box
<CacheNode
<K
, V
>>>,
103 // trivial: if our contents are Send, the whole cache is Send
104 unsafe impl<K
: Send
, V
: Send
> Send
for LruCache
<K
, V
> {}
106 impl<K
: std
::cmp
::Eq
+ std
::hash
::Hash
+ Copy
, V
> LruCache
<K
, V
> {
107 /// Create LRU cache instance which holds up to `capacity` nodes at once.
108 pub fn new(capacity
: usize) -> Self {
109 let capacity
= capacity
.max(1);
111 map
: HashMap
::with_capacity(capacity
),
112 list
: LinkedList
::new(),
114 _marker
: PhantomData
,
118 /// Clear all the entries from the cache.
119 pub fn clear(&mut self) {
120 // This frees only the HashMap with the node pointers.
122 // This frees the actual nodes and resets the list head and tail.
126 /// Insert or update an entry identified by `key` with the given `value`.
127 /// This entry is placed as the most recently used node at the head.
128 pub fn insert(&mut self, key
: K
, value
: V
) {
129 match self.map
.entry(key
) {
130 Entry
::Occupied(mut o
) => {
131 // Node present, update value
132 let node_ptr
= *o
.get_mut();
133 self.list
.bring_to_front(node_ptr
);
134 let mut node
= unsafe { Box::from_raw(node_ptr) }
;
136 let _node_ptr
= Box
::into_raw(node
);
138 Entry
::Vacant(v
) => {
139 // Node not present, insert a new one
140 // Unfortunately we need a copy of the key here, therefore it has
141 // to impl the copy trait
142 let node
= Box
::new(CacheNode
::new(key
, value
));
143 let node_ptr
= Box
::into_raw(node
);
144 self.list
.push_front(node_ptr
);
146 // If we have more elements than capacity,
147 // delete the lists tail node (= oldest node).
148 // This needs to be executed after the insert in order to
149 // avoid borrow conflict. This means there are temporarily
150 // self.capacity + 1 cache nodes.
151 if self.map
.len() > self.capacity
{
158 /// Remove the given `key` and its `value` from the cache.
159 pub fn remove(&mut self, key
: K
) -> Option
<V
> {
160 // Remove node pointer from the HashMap and get ownership of the node
161 let node_ptr
= self.map
.remove(&key
)?
;
162 let node
= self.list
.remove(node_ptr
);
166 /// Remove the least recently used node from the cache.
167 fn pop_tail(&mut self) {
168 if let Some(old_tail
) = self.list
.pop_tail() {
169 // Remove HashMap entry for old tail
170 self.map
.remove(&old_tail
.key
);
174 /// Get a mutable reference to the value identified by `key`.
175 /// This will update the cache entry to be the most recently used entry.
176 /// On cache misses, None is returned.
177 pub fn get_mut(&mut self, key
: K
) -> Option
<&mut V
> {
178 let node_ptr
= self.map
.get(&key
)?
;
179 self.list
.bring_to_front(*node_ptr
);
180 Some(unsafe { &mut (*self.list.head).value }
)
183 /// Number of entries in the cache.
184 pub fn len(&self) -> usize {
188 /// Returns `true` when the cache is empty
189 pub fn is_empty(&self) -> bool
{
193 /// Get a mutable reference to the value identified by `key`.
194 /// This will update the cache entry to be the most recently used entry.
195 /// On cache misses, the cachers fetch method is called to get a corresponding
197 /// If fetch returns a value, it is inserted as the most recently used entry
199 pub fn access
<'a
>(&'a
mut self, key
: K
, cacher
: &mut dyn Cacher
<K
, V
>) -> Result
<Option
<&'a
mut V
>, anyhow
::Error
> {
200 match self.map
.entry(key
) {
201 Entry
::Occupied(mut o
) => {
202 // Cache hit, birng node to front of list
203 let node_ptr
= *o
.get_mut();
204 self.list
.bring_to_front(node_ptr
);
206 Entry
::Vacant(v
) => {
207 // Cache miss, try to fetch from cacher and insert at the front
208 match cacher
.fetch(key
)?
{
209 None
=> return Ok(None
),
211 // Unfortunately we need a copy of the key here, therefore it has
212 // to impl the copy trait
213 let node
= Box
::new(CacheNode
::new(key
, value
));
214 let node_ptr
= Box
::into_raw(node
);
215 self.list
.push_front(node_ptr
);
217 // If we have more elements than capacity,
218 // delete the lists tail node (= oldest node).
219 // This needs to be executed after the insert in order to
220 // avoid borrow conflict. This means there are temporarily
221 // self.capacity + 1 cache nodes.
222 if self.map
.len() > self.capacity
{
230 Ok(Some(unsafe { &mut (*self.list.head).value }
))
234 /// Linked list holding the nodes of the LruCache.
236 /// This struct actually holds the CacheNodes via the raw linked list pointers
237 /// and allows to define the access sequence of these via the list sequence.
238 /// The LinkedList of the standard library unfortunately does not implement
239 /// an efficient way to bring list entries to the front, therefore we need our own.
240 struct LinkedList
<K
, V
> {
241 head
: *mut CacheNode
<K
, V
>,
242 tail
: *mut CacheNode
<K
, V
>,
245 impl<K
, V
> LinkedList
<K
, V
> {
246 /// Create a new empty linked list.
249 head
: std
::ptr
::null_mut(),
250 tail
: std
::ptr
::null_mut(),
254 /// Bring the CacheNode referenced by `node_ptr` to the front of the linked list.
255 fn bring_to_front(&mut self, node_ptr
: *mut CacheNode
<K
, V
>) {
256 if node_ptr
== self.head
{
257 // node is already head, just return
261 let mut node
= unsafe { Box::from_raw(node_ptr) }
;
262 // Update the prev node to point to next (or null if current node is tail)
263 unsafe { (*node.prev).next = node.next }
;
265 // Update the next node or otherwise the tail
266 if !node
.next
.is_null() {
267 unsafe { (*node.next).prev = node.prev }
;
269 // No next node means this was the tail
270 self.tail
= node
.prev
;
273 node
.prev
= std
::ptr
::null_mut();
274 node
.next
= self.head
;
275 // update the head and release ownership of the node again
276 let node_ptr
= Box
::into_raw(node
);
277 // Update current head
278 unsafe { (*self.head).prev = node_ptr }
;
279 // Update to new head
280 self.head
= node_ptr
;
283 /// Insert a new node at the front of the linked list.
284 fn push_front(&mut self, node_ptr
: *mut CacheNode
<K
, V
>) {
285 let mut node
= unsafe { Box::from_raw(node_ptr) }
;
287 // Old head gets new heads next
288 node
.next
= self.head
;
289 // Release ownership of node, rest can be handled with just the pointer.
290 let node_ptr
= Box
::into_raw(node
);
292 // Update the prev for the old head
293 if !self.head
.is_null() {
294 unsafe { (*self.head).prev = node_ptr }
;
297 // Update the head to the new node pointer
298 self.head
= node_ptr
;
300 // If there was no old tail, this node will be the new tail too
301 if self.tail
.is_null() {
302 self.tail
= node_ptr
;
306 /// Remove the node referenced by `node_ptr` from the linked list and return it.
307 fn remove(&mut self, node_ptr
: *mut CacheNode
<K
, V
>) -> Box
<CacheNode
<K
, V
>> {
308 let node
= unsafe { Box::from_raw(node_ptr) }
;
310 // Update the previous node or otherwise the head
311 if !node
.prev
.is_null() {
312 unsafe { (*node.prev).next = node.next }
;
314 // No previous node means this was the head
315 self.head
= node
.next
;
318 // Update the next node or otherwise the tail
319 if !node
.next
.is_null() {
320 unsafe { (*node.next).prev = node.prev }
;
322 // No next node means this was the tail
323 self.tail
= node
.prev
;
328 /// Remove the tail node from the linked list and return it.
329 fn pop_tail(&mut self) -> Option
<Box
<CacheNode
<K
, V
>>> {
330 if self.tail
.is_null() {
334 let old_tail
= unsafe { Box::from_raw(self.tail) }
;
335 self.tail
= old_tail
.prev
;
336 // Update next node for new tail
337 if !self.tail
.is_null() {
338 unsafe { (*self.tail).next = std::ptr::null_mut() }
;
343 /// Clear the linked list and free all the nodes.
344 fn clear(&mut self) {
345 let mut next
= self.head
;
346 while !next
.is_null() {
347 // Taking ownership of node and drop it at the end of the block.
348 let current
= unsafe { Box::from_raw(next) }
;
351 // Reset head and tail pointers
352 self.head
= std
::ptr
::null_mut();
353 self.tail
= std
::ptr
::null_mut();
358 fn test_linked_list() {
359 let mut list
= LinkedList
::new();
361 let node
= Box
::new(CacheNode
::new(idx
, idx
+ 1));
362 // Get pointer, release ownership.
363 let node_ptr
= Box
::into_raw(node
);
364 list
.push_front(node_ptr
);
366 assert_eq
!(unsafe { (*list.head).key }
, 2);
367 assert_eq
!(unsafe { (*list.head).value }
, 3);
368 assert_eq
!(unsafe { (*list.tail).key }
, 0);
369 assert_eq
!(unsafe { (*list.tail).value }
, 1);
371 list
.bring_to_front(list
.tail
);
372 assert_eq
!(unsafe { (*list.head).key }
, 0);
373 assert_eq
!(unsafe { (*list.head).value }
, 1);
374 assert_eq
!(unsafe { (*list.tail).key }
, 1);
375 assert_eq
!(unsafe { (*list.tail).value }
, 2);
377 list
.bring_to_front(list
.tail
);
378 assert_eq
!(unsafe { (*list.head).key }
, 1);
379 assert_eq
!(unsafe { (*list.head).value }
, 2);
380 assert_eq
!(unsafe { (*list.tail).key }
, 2);
381 assert_eq
!(unsafe { (*list.tail).value }
, 3);
383 let tail
= list
.pop_tail().unwrap();
384 assert_eq
!(tail
.key
, 2);
385 assert_eq
!(tail
.value
, 3);
386 assert_eq
!(unsafe { (*list.head).key }
, 1);
387 assert_eq
!(unsafe { (*list.head).value }
, 2);
388 assert_eq
!(unsafe { (*list.tail).key }
, 0);
389 assert_eq
!(unsafe { (*list.tail).value }
, 1);
392 assert
!(list
.head
.is_null());
393 assert
!(list
.tail
.is_null());