pub struct Cache<K, V, S = RandomState> { /* private fields */ }Expand description
A concurrent, fixed-size, set-associative cache.
This cache maps keys to values using a fixed number of buckets. Each key hashes to exactly one bucket, and collisions are resolved by eviction (the new value replaces the old one).
§Thread Safety
The cache is safe to share across threads (Send + Sync). All operations use atomic
instructions and never block, making it suitable for high-contention scenarios.
§Limitations
- No
Dropsupport: Key and value types must not implementDrop. UseCopytypes, primitives, or&'staticreferences. - Eviction on collision: When two keys hash to the same bucket, the older entry is lost.
- No iteration or removal: Individual entries cannot be enumerated or explicitly removed.
§Type Parameters
K: The key type. Must implementHash+Eqand must not implementDrop.V: The value type. Must implementCloneand must not implementDrop.S: The hash builder type. Must implementBuildHasher. Defaults toRandomStateorrapidhashif therapidhashfeature is enabled.
§Example
use fixed_cache::Cache;
let cache: Cache<u64, u64> = Cache::new(256, Default::default());
// Insert a value
cache.insert(42, 100);
assert_eq!(cache.get(&42), Some(100));
// Get or compute a value
let value = cache.get_or_insert_with(123, |&k| k * 2);
assert_eq!(value, 246);Implementations§
Source§impl<K, V, S> Cache<K, V, S>
impl<K, V, S> Cache<K, V, S>
Sourcepub fn new(num: usize, build_hasher: S) -> Self
pub fn new(num: usize, build_hasher: S) -> Self
Create a new cache with the specified number of entries and hasher.
Dynamically allocates memory for the cache entries.
§Panics
Panics if num is not a power of two.
Sourcepub const fn new_static(
entries: &'static [Bucket<(K, V)>],
build_hasher: S,
) -> Self
pub const fn new_static( entries: &'static [Bucket<(K, V)>], build_hasher: S, ) -> Self
Creates a new cache with the specified entries and hasher.
§Panics
Panics if entries.len() is not a power of two.
Source§impl<K, V, S> Cache<K, V, S>
impl<K, V, S> Cache<K, V, S>
Sourcepub fn get<Q: ?Sized + Hash + Equivalent<K>>(&self, key: &Q) -> Option<V>
pub fn get<Q: ?Sized + Hash + Equivalent<K>>(&self, key: &Q) -> Option<V>
Get an entry from the cache.
Sourcepub fn get_or_insert_with<F>(&self, key: K, f: F) -> V
pub fn get_or_insert_with<F>(&self, key: K, f: F) -> V
Gets a value from the cache, or inserts one computed by f if not present.
If the key is found in the cache, returns a clone of the cached value.
Otherwise, calls f to compute the value, attempts to insert it, and returns it.
Sourcepub fn get_or_insert_with_ref<'a, Q, F, Cvt>(
&self,
key: &'a Q,
f: F,
cvt: Cvt,
) -> V
pub fn get_or_insert_with_ref<'a, Q, F, Cvt>( &self, key: &'a Q, f: F, cvt: Cvt, ) -> V
Gets a value from the cache, or inserts one computed by f if not present.
If the key is found in the cache, returns a clone of the cached value.
Otherwise, calls f to compute the value, attempts to insert it, and returns it.
This is the same as get_or_insert_with, but takes a reference to the key, and a function
to get the key reference to an owned key.