In-Memory Caches
Recipe | Crates | Categories |
---|---|---|
Use a Least Recently Used (LRU) Cache | ||
cached | ||
moka |
FIXME
Consider the following Rust crates for your in-memory caching needs: lru
⮳, moka
⮳, and cached
⮳.
Use a Least Recently Used (LRU) Cache
lru
⮳ provides a fast Least Recently Used (LRU) cache for Rust. LRU is a common data structure used to limit the size of a cache by discarding the Least Recently Used (accessed) items when the cache reaches its capacity.
use std::num::NonZeroUsize; use lru::LruCache; fn main() { // Create an LRU cache with a capacity of 3 let mut cache = LruCache::new(NonZeroUsize::new(3).unwrap()); // Insert some key-value pairs into the cache cache.put(1, "one"); cache.put(2, "two"); cache.put(3, "three"); // Access some entries println!("a) value for key 1: {:?}", cache.get(&1)); // Should print "Value for key 1: Some("one")" assert_eq!(*cache.get(&2).unwrap(), "two"); assert_eq!(cache.get(&3), Some(&"three")); assert!(cache.get(&4).is_none()); // Insert another entry, causing the least recently used entry to be evicted cache.put(4, "four"); // The cache now contains keys 2, 3, and 4 println!("b) value for key 1: {:?}", cache.get(&1)); // Should print "Value for key 1: None" println!("b) value for key 3: {:?}", cache.get(&3)); // Should print "Value for key 3: Some("three")" println!("b) value for key 4: {:?}", cache.get(&4)); // Should print "Value for key 4: Some("four")" // Insert another entry, causing another eviction cache.put(5, "five"); // The cache now contains keys 3, 4, and 5 println!("Value for key 2: {:?}", cache.get(&2)); // Should print "Value for key 2: None" println!("Value for key 4: {:?}", cache.get(&4)); // Should print "Value for key 4: Some("four")" println!("Value for key 5: {:?}", cache.get(&5)); // Should print "Value for key 5: Some("five")" { // Returns a mutable reference to the value of the key in the cache or // `None``, if it is not present in the cache. // Moves the key to the head of the LRU list, if it exists. let v = cache.get_mut(&5).unwrap(); *v = "new value"; assert_eq!(cache.get_mut(&5), Some(&mut "new value")); assert_eq!(cache.get_mut(&6), None); } }
moka
moka
⮳ is a fast and concurrent cache library inspired by Java Caffeine.
// COMING SOON
cached
cached
⮳ provides generic cache implementations and simplified function memoization.
// // COMING SOON
Related Topics
- Database.
- Database Implementations.
- NoSQL.
- Key-Value Stores.