pub struct DefaultCache<K: CacheKey, V: CacheValue> { /* private fields */ }Expand description
In-memory Cache with an LRU eviction policy, byte-based memory limit,
and optional per-entry TTL.
Entries are evicted in least-recently-used order whenever an insert would
push memory_used above memory_limit. Inserts whose own size exceeds the
limit are rejected (and any prior entry under the same key is removed).
When a TTL is configured, the expiration is stamped onto each entry at
insertion time and checked lazily on access. Entries with size 0 are rejected.
Implementations§
Source§impl<K: CacheKey, V: CacheValue> DefaultCache<K, V>
impl<K: CacheKey, V: CacheValue> DefaultCache<K, V>
Sourcepub fn new(memory_limit: usize) -> Self
pub fn new(memory_limit: usize) -> Self
Create a cache with the given memory budget in bytes and no TTL.
Sourcepub fn new_with_ttl(memory_limit: usize, ttl: Option<Duration>) -> Self
pub fn new_with_ttl(memory_limit: usize, ttl: Option<Duration>) -> Self
Create a cache with the given memory budget in bytes and an optional TTL applied to every newly inserted entry.
Sourcepub fn with_time_provider(self, provider: Arc<dyn TimeProvider>) -> Self
pub fn with_time_provider(self, provider: Arc<dyn TimeProvider>) -> Self
Override the time source used to stamp and check TTLs.
Sourcepub fn memory_used(&self) -> usize
pub fn memory_used(&self) -> usize
Number of bytes currently accounted for by live entries.
Trait Implementations§
Source§impl<K: CacheKey, V: CacheValue> Cache<K, V> for DefaultCache<K, V>
impl<K: CacheKey, V: CacheValue> Cache<K, V> for DefaultCache<K, V>
Source§fn remove(&self, k: &K) -> Option<V>
fn remove(&self, k: &K) -> Option<V>
Remove an entry from the cache, returning the value if it existed.
Source§fn contains_key(&self, k: &K) -> bool
fn contains_key(&self, k: &K) -> bool
Check if the cache contains a specific key.
Source§fn cache_limit(&self) -> usize
fn cache_limit(&self) -> usize
Current memory budget, in bytes.
Source§fn update_cache_limit(&self, limit: usize)
fn update_cache_limit(&self, limit: usize)
Change the memory budget in bytes.
Source§fn cache_ttl(&self) -> Option<Duration>
fn cache_ttl(&self) -> Option<Duration>
Time-to-live applied to newly inserted entries, or
None if entries
never expire on their own.Source§fn update_cache_ttl(&self, ttl: Option<Duration>)
fn update_cache_ttl(&self, ttl: Option<Duration>)
Change the TTL applied to subsequent inserts.
Source§fn drop_table_entries(&self, table_ref: &TableReference) -> Result<()>
fn drop_table_entries(&self, table_ref: &TableReference) -> Result<()>
Invalidate every entry associated with
table_ref.Source§fn list_entries(&self) -> HashMap<K, CacheEntryInfo<V>>
fn list_entries(&self) -> HashMap<K, CacheEntryInfo<V>>
Snapshot of all current entries with per-entry metadata (size, hits,
expiration) for diagnostics and observability.
Auto Trait Implementations§
impl<K, V> !Freeze for DefaultCache<K, V>
impl<K, V> !RefUnwindSafe for DefaultCache<K, V>
impl<K, V> !UnwindSafe for DefaultCache<K, V>
impl<K, V> Send for DefaultCache<K, V>
impl<K, V> Sync for DefaultCache<K, V>
impl<K, V> Unpin for DefaultCache<K, V>
impl<K, V> UnsafeUnpin for DefaultCache<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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more