pub struct BoundedLinearCache<K, V> { /* private fields */ }Expand description
BoundedLinearCache is a bounded cache with approximate key matching support.
The cache enforces a maximum capacity, and when the capacity is exceeded, the least recently used (LRU) element is evicted.
§Approximate Key Matching
Keys must implement the ApproxComparable trait, which allows approximate equality comparisons based on the provided tolerance.
This enables the cache to retrieve values even when the queried key is not an exact match but is “close enough.”
§Example Usage
use proximipy::caching::bounded::bounded_linear_cache::BoundedLinearCache;
use proximipy::caching::approximate_cache::ApproximateCache;
let mut cache = BoundedLinearCache::new(3, 2.0);
cache.insert(10 as i16, "Value 1");
cache.insert(20, "Value 2");
cache.insert(30, "Value 3");
assert_eq!(cache.find(&11), Some("Value 1"));
assert_eq!(cache.len(), 3);
cache.insert(40, "Value 4"); // Evicts the least recently used (Key(20))
assert!(cache.find(&20).is_none());§Type Parameters
K: The type of the keys, which must implementApproxComparable,Eq,Hash, andClone.V: The type of the values, which must implementClone.
§Methods
new(max_capacity: usize, tolerance: f32) -> Self: Creates a newBoundedLinearCachewith the specified maximum capacity and tolerance.find(&mut self, key: &K) -> Option<V>: Attempts to find a value matching the given key approximately. Promotes the found key to the head of the list.insert(&mut self, key: K, value: V): Inserts a key-value pair into the cache. Evicts the least recently used item if the cache is full.len(&self) -> usize: Returns the current size of the cache.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<K, V> Freeze for BoundedLinearCache<K, V>
impl<K, V> !RefUnwindSafe for BoundedLinearCache<K, V>
impl<K, V> !Send for BoundedLinearCache<K, V>
impl<K, V> !Sync for BoundedLinearCache<K, V>
impl<K, V> Unpin for BoundedLinearCache<K, V>where
K: Unpin,
impl<K, V> !UnwindSafe for BoundedLinearCache<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more