pub struct TwoLevelCache<TKey, TValue: Clone> { /* private fields */ }Expand description
Provides a lightweight two-level cache for storing key-value pairs, supporting fast retrieval and optional eviction handling.
The cache maintains a primary entry for the most recently added item and a secondary array for additional items, up to the specified capacity. When the cache exceeds its capacity, evicted values are passed to the eviction action. It is optimized for scenarios where a small number of items are frequently accessed.
Implementations§
Source§impl<TKey: PartialEq + Clone + 'static, TValue: Clone> TwoLevelCache<TKey, TValue>
impl<TKey: PartialEq + Clone + 'static, TValue: Clone> TwoLevelCache<TKey, TValue>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a cache with the default secondary capacity (3), no eviction action and the default comparer.
Sourcepub fn with_secondary_size(secondary_size: i32) -> Self
pub fn with_secondary_size(secondary_size: i32) -> Self
Creates a cache with the given secondary capacity.
§Panics
When secondary_size is negative (upstream throws
ArgumentOutOfRangeException).
Sourcepub fn with_options(
secondary_size: i32,
eviction_action: Option<Box<dyn Fn(&TValue) + Send>>,
comparer: Option<Box<dyn Fn(&TKey, &TKey) -> bool + Send>>,
) -> Self
pub fn with_options( secondary_size: i32, eviction_action: Option<Box<dyn Fn(&TValue) + Send>>, comparer: Option<Box<dyn Fn(&TKey, &TKey) -> bool + Send>>, ) -> Self
Creates a cache with the given secondary capacity, eviction action and
key comparer (None compares with ==).
§Panics
When secondary_size is negative (upstream throws
ArgumentOutOfRangeException).
Source§impl<TKey: Clone, TValue: Clone> TwoLevelCache<TKey, TValue>
impl<TKey: Clone, TValue: Clone> TwoLevelCache<TKey, TValue>
Sourcepub fn get_or_add(
&mut self,
key: TKey,
factory: impl FnOnce(&TKey) -> TValue,
) -> TValue
pub fn get_or_add( &mut self, key: TKey, factory: impl FnOnce(&TKey) -> TValue, ) -> TValue
Returns the value stored for key, creating and storing it with
factory when the key is not in the cache.
Sourcepub fn clear_and_dispose(&mut self)
pub fn clear_and_dispose(&mut self)
Removes every entry, passing each value to the eviction action.
Trait Implementations§
Auto Trait Implementations§
impl<TKey, TValue> !RefUnwindSafe for TwoLevelCache<TKey, TValue>
impl<TKey, TValue> !Sync for TwoLevelCache<TKey, TValue>
impl<TKey, TValue> !UnwindSafe for TwoLevelCache<TKey, TValue>
impl<TKey, TValue> Freeze for TwoLevelCache<TKey, TValue>
impl<TKey, TValue> Send for TwoLevelCache<TKey, TValue>
impl<TKey, TValue> Unpin for TwoLevelCache<TKey, TValue>
impl<TKey, TValue> UnsafeUnpin for TwoLevelCache<TKey, TValue>where
Option<(TKey, TValue)>: UnsafeUnpin,
Option<Vec<(TKey, TValue)>>: UnsafeUnpin,
Option<Box<dyn Fn(&TValue) + Send>>: UnsafeUnpin,
Box<dyn Fn(&TKey, &TKey) -> bool + Send>: UnsafeUnpin,
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
Source§impl<T> ClassDefaults for Twhere
T: ?Sized,
impl<T> ClassDefaults for Twhere
T: ?Sized,
Source§fn static_constructor()
fn static_constructor()
affects_render, metadata
overrides). Read more