Skip to main content

DefaultCache

Struct DefaultCache 

Source
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>

Source

pub fn new(memory_limit: usize) -> Self

Create a cache with the given memory budget in bytes and no TTL.

Source

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.

Source

pub fn with_name(self, name: impl Into<String>) -> Self

Override the cache name.

Source

pub fn with_time_provider(self, provider: Arc<dyn TimeProvider>) -> Self

Override the time source used to stamp and check TTLs.

Source

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>

Source§

fn get(&self, key: &K) -> Option<V>

Get a cached entry if it exists.
Source§

fn put(&self, key: &K, value: V) -> Option<V>

Store a value in the cache. Read more
Source§

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

Check if the cache contains a specific key.
Source§

fn len(&self) -> usize

Fetch the total number of cache entries.
Source§

fn clear(&self)

Remove all entries from the cache.
Source§

fn name(&self) -> String

Return the cache name.
Source§

fn cache_limit(&self) -> usize

Current memory budget, in bytes.
Source§

fn update_cache_limit(&self, limit: usize)

Change the memory budget in bytes.
Source§

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>)

Change the TTL applied to subsequent inserts.
Source§

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>>

Snapshot of all current entries with per-entry metadata (size, hits, expiration) for diagnostics and observability.
Source§

fn is_empty(&self) -> bool

Check if the cache collection is empty.

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>
where K: Unpin, V: Unpin,

§

impl<K, V> UnsafeUnpin for DefaultCache<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V