| Crates.io | fixed-cache |
| lib.rs | fixed-cache |
| version | 0.1.7 |
| created_at | 2025-12-17 22:45:34.782044+00 |
| updated_at | 2026-01-23 16:59:11.812075+00 |
| description | A minimalistic, lock-free, fixed-size cache |
| homepage | |
| repository | https://github.com/DaniPopes/fixed-cache |
| max_upload_size | |
| id | 1991245 |
| size | 71,849 |
A minimalistic, lock-free, fixed-size cache for Rust.
This crate provides a concurrent set-associative cache with a fixed number of entries. It is designed for high-performance scenarios where you need fast, thread-safe caching with predictable memory usage and minimal overhead.
rapidhash for faster hashing)no_std compatible (with alloc)static_cache! macroAdd to your Cargo.toml:
[dependencies]
fixed-cache = "0.1"
use fixed_cache::Cache;
// Create a cache with 1024 entries
let cache: Cache<u64, u64> = Cache::new(1024, Default::default());
// Insert and retrieve values
cache.insert(42, 100);
assert_eq!(cache.get(&42), Some(100));
// Remove an entry
assert_eq!(cache.remove(&42), Some(100));
assert_eq!(cache.get(&42), None);
// Use get_or_insert_with for lazy initialization
let value = cache.get_or_insert_with(123, |&k| k * 2);
assert_eq!(value, 246);
For global caches that need to be initialized at compile time:
use fixed_cache::{Cache, static_cache};
// Requires a const-compatible hasher (e.g., rapidhash with the `rapidhash` feature)
type MyBuildHasher = todo!();
static CACHE: Cache<u64, u64, MyBuildHasher> = static_cache!(u64, u64, 1024, MyBuildHasher::new());
fn lookup(key: u64) -> u64 {
CACHE.get_or_insert_with(key, |&k| k * 2) // your computation here
}
The cache uses a set-associative design where each key maps to exactly one bucket based on its hash. When a collision occurs (two keys hash to the same bucket), the new value evicts the old one. This means:
This design is ideal for memoization caches where:
rapidhash - Use rapidhash for faster hashing (recommended)stats - Enable statistics tracking (hits, misses, collisions) via custom handlersnightly - Enable nightly-only optimizationsLicensed under either of Apache License, Version 2.0 or MIT license at your option.