pub struct Cache<K, V, S = RandomState> { /* private fields */ }
Expand description
Highly performant, thread-safe cache with a focus on simplicity.
It implements the S3-FIFO eviction algorithm as specified in FIFO Queues are All You Need for Cache Eviction. The cache is divided into multiple shards to reduce contention during concurrent access. This crate does not use any unsafe code.
Wrap the cache in a std::sync::Arc
to share it between threads. Both reads and writes only
require shared references to the cache.
Implementations§
Source§impl<K, V> Cache<K, V, RandomState>
impl<K, V> Cache<K, V, RandomState>
Sourcepub fn with_capacity(capacity: usize) -> Cache<K, V, RandomState>
pub fn with_capacity(capacity: usize) -> Cache<K, V, RandomState>
Creates a new cache with at least the specified capacity.
The actual capacity may be slightly higher due to sharding and rounding.
Source§impl<K, V, S> Cache<K, V, S>
impl<K, V, S> Cache<K, V, S>
Source§impl<K, V, S> Cache<K, V, S>
impl<K, V, S> Cache<K, V, S>
Sourcepub fn with_capacity_and_hasher(
capacity: usize,
hash_builder: S,
) -> Cache<K, V, S>
pub fn with_capacity_and_hasher( capacity: usize, hash_builder: S, ) -> Cache<K, V, S>
Creates a new cache with the at least the specified capacity, using hasher
to hash the
keys.
The actual capacity may be slightly higher due to sharding and rounding.
Source§impl<K, V, S> Cache<K, V, S>
impl<K, V, S> Cache<K, V, S>
Sourcepub fn stats(&self) -> Stats
pub fn stats(&self) -> Stats
Returns cache performance statistics and resets the internal counters.
This method provides metrics about cache performance since the last call to stats()
.
After returning the statistics, all internal counters are reset to zero.
§Examples
Basic usage:
use plain_cache::Cache;
let cache = Cache::with_capacity(100);
cache.insert("key1", "value1");
cache.get("key1"); // hit
cache.get("key2"); // miss
let stats = cache.stats();
println!("Hits: {}, Misses: {}", stats.hit_count, stats.miss_count);
Calculating hit rate:
use plain_cache::Cache;
let cache = Cache::with_capacity(100);
cache.insert("key1", "value1");
cache.get("key1"); // hit
cache.get("key2"); // miss
let stats = cache.stats();
let total_requests = stats.hit_count + stats.miss_count;
if total_requests > 0 {
let hit_rate = stats.hit_count as f64 / total_requests as f64;
println!("Hit rate: {:.2}%", hit_rate * 100.0);
}
§Note
The counters are reset after each call to stats()
, so each call returns
statistics for the period since the previous call. This allows for periodic
monitoring of cache performance.