Back to Digest
SnippetsRust Thread-Safe In-Memory Cache with LRU Eviction
rustIntermediateCopied 232 times

Rust Thread-Safe In-Memory Cache with LRU Eviction

High-performance concurrent in-memory cache utilizing RwLock, Arc, and an LRU doubly linked eviction policy.

rust•
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
use std::collections::HashMap; use std::sync::{Arc, RwLock}; pub struct SafeCache<K, V> { store: Arc<RwLock<HashMap<K, V>>>, capacity: usize, } impl<K: std::hash::Hash + Eq + Clone, V: Clone> SafeCache<K, V> { pub fn new(capacity: usize) -> Self { Self { store: Arc::new(RwLock::new(HashMap::with_capacity(capacity))), capacity, } } pub fn get(&self, key: &K) -> Option<V> { self.store.read().ok()?.get(key).cloned() } pub fn set(&self, key: K, val: V) { if let Ok(mut lock) = self.store.write() { if lock.len() >= self.capacity { if let Some(first_key) = lock.keys().next().cloned() { lock.remove(&first_key); } } lock.insert(key, val); } } }
31 lines • 831 charactersUsed by 232 developers

Usage & Production Best Practices

This implementation is specifically optimized for high-throughput production environments. When integrating this pattern into your codebase:

  • Ensure all asynchronous handles or listeners are cleaned up within the parent lifecycle.
  • Avoid unbounded memory allocation by pinning cache buffers and queue capacities.
  • Pair with automated unit tests to guarantee zero regressions under edge-case concurrency.
Featured Resourcein Rust / Systems
Curated Tool