evictor
Provides several cache implementations with different eviction policies:
- LRU (Least Recently Used) - Evicts the item that was accessed longest ago
- MRU (Most Recently Used) - Evicts the item that was accessed most recently
- LFU (Least Frequently Used) - Evicts the item that has been accessed least frequently
- FIFO (First In, First Out) - Evicts the item that was inserted earliest, regardless of access patterns
- LIFO (Last In, First Out) - Evicts the item that was inserted most recently, regardless of access patterns
- Random - Evicts a randomly selected item when the cache is full
All caches are generic over key and value types, with a configurable capacity.
Usage
LRU (Least Recently Used) Cache
Evicts the item that was accessed longest ago:
use NonZeroUsize;
use Lru;
let mut cache = new;
cache.insert;
cache.insert;
cache.insert;
// Access entry (marks as recently used)
cache.get;
// Insert when full evicts least recently used
cache.insert;
assert!; // 2 was evicted (least recently used)
assert!; // 1 was recently accessed, so kept
MRU (Most Recently Used) Cache
Evicts the item that was accessed most recently:
use NonZeroUsize;
use Mru;
let mut cache = new;
cache.insert;
cache.insert;
cache.insert;
// Access entry (marks as recently used)
cache.get;
// Insert when full evicts most recently used
cache.insert;
assert!; // 1 was evicted (most recently used)
assert!; // 2 was not recently accessed, so kept
LFU (Least Frequently Used) Cache
Evicts the item that has been accessed least frequently:
use NonZeroUsize;
use Lfu;
let mut cache = new;
cache.insert;
cache.insert;
cache.insert;
// Access entries different numbers of times
cache.get; // frequency: 1
cache.get; // frequency: 2
cache.get; // frequency: 1
// Key 3 has frequency: 0 (never accessed after insertion)
// Insert when full evicts least frequently used
cache.insert;
assert!; // 3 was evicted (frequency 0)
assert!; // 1 has highest frequency (2)
assert!; // 2 has frequency 1
FIFO (First In, First Out) Cache
Evicts the item that was inserted earliest, regardless of access patterns:
use NonZeroUsize;
use FIFO;
let mut cache = FIFO::new;
cache.insert;
cache.insert;
cache.insert;
// Access entry (doesn't affect eviction order in FIFO)
cache.get;
// Insert when full evicts first inserted
cache.insert;
assert!; // 1 was evicted (first inserted)
assert!; // 2 was inserted second, so kept
assert!; // 3 was inserted third, so kept
LIFO (Last In, First Out) Cache
Evicts the item that was inserted most recently, regardless of access patterns:
use NonZeroUsize;
use LIFO;
let mut cache = LIFO::new;
cache.insert;
cache.insert;
cache.insert;
// Access entry (doesn't affect eviction order in LIFO)
cache.get;
// Insert when full evicts most recently inserted
cache.insert;
assert!; // 3 was evicted (most recently inserted)
assert!; // 1 was inserted first, so kept
assert!; // 2 was inserted second, so kept
Random Cache
Evicts a randomly selected item when the cache is full. Useful as a baseline for comparison with other policies or when no particular access pattern can be predicted:
use NonZeroUsize;
use Random;
let mut cache = new;
cache.insert;
cache.insert;
cache.insert;
// Access entry (doesn't affect eviction order in Random policy)
cache.get;
// Insert when full evicts a random item
cache.insert;
// One of the original entries (1, 2, or 3) was randomly evicted
assert_eq!;
assert!; // 4 was just inserted
Creating from Iterator
All cache types can be created from iterators. This will set the capacity to the number of items in the iterator:
use ;
let items = vec!;
// Works with any cache type
let lru_cache: = items.clone.into_iter.collect;
let mru_cache: = items.clone.into_iter.collect;
let lfu_cache: = items.clone.into_iter.collect;
let fifo_cache: = items.clone.into_iter.collect;
let lifo_cache: = items.clone.into_iter.collect;
let random_cache: = items.into_iter.collect;
Common Operations
All cache types support the same operations with identical APIs:
use NonZeroUsize;
use Lru; // Could also use Mru, Lfu, FIFO, LIFO, or Random
let mut cache = new;
// Insert and access
cache.insert;
cache.insert;
let value = cache.get; // Returns Some(&String) and updates cache order
// Non-mutating operations (don't affect eviction order)
let value = cache.peek; // Returns Some(&String) without updating order
let exists = cache.contains_key; // Returns bool
if let Some = cache.tail
// Other operations
cache.remove; // Remove specific key
if let Some = cache.pop
cache.clear; // Remove all entries
Features
Default Features
ahash- Fast hashing using theahashcrate (enabled by default)rand- Enables the Random cache policy using therandcrate.
License
This project is licensed under the either the APACHE or MIT License at your option. See the LICENSE-APACHE and LICENSE-MIT files for details.