pub struct ModelCache {
pub hits: AtomicU64,
pub misses: AtomicU64,
/* private fields */
}Expand description
Fields§
§hits: AtomicU64Cumulative number of cache hits.
misses: AtomicU64Cumulative number of cache misses.
Implementations§
Source§impl ModelCache
impl ModelCache
Sourcepub fn new(config: ModelCacheConfig) -> Self
pub fn new(config: ModelCacheConfig) -> Self
Create a new, empty cache with the given configuration.
Sourcepub fn get_or_insert<F>(&self, key: &str, loader: F) -> Arc<ModelEntry>where
F: FnOnce() -> ModelEntry,
pub fn get_or_insert<F>(&self, key: &str, loader: F) -> Arc<ModelEntry>where
F: FnOnce() -> ModelEntry,
Return a shared reference to the cached entry for key, or insert a
new one produced by loader if none exists (or if the existing entry
is stale).
The returned Arc allows callers to hold a reference to the entry
while the cache mutex is not held.
Sourcepub fn evict(&self, key: &str) -> bool
pub fn evict(&self, key: &str) -> bool
Remove the entry for key. Returns true if an entry was removed.
Sourcepub fn evict_stale(&self) -> usize
pub fn evict_stale(&self) -> usize
Remove all entries that have been idle longer than the configured TTL. Returns the number of entries removed.
Sourcepub fn hit_rate(&self) -> f32
pub fn hit_rate(&self) -> f32
Cache hit rate as a fraction in [0.0, 1.0].
Returns 0.0 when no lookups have been performed yet.
Sourcepub fn total_memory_bytes(&self) -> usize
pub fn total_memory_bytes(&self) -> usize
Sum of memory_bytes across all cached entries.
Sourcepub fn stats(&self) -> ModelCacheStats
pub fn stats(&self) -> ModelCacheStats
Take a statistics snapshot of the current cache state.
Auto Trait Implementations§
impl !Freeze for ModelCache
impl RefUnwindSafe for ModelCache
impl Send for ModelCache
impl Sync for ModelCache
impl Unpin for ModelCache
impl UnsafeUnpin for ModelCache
impl UnwindSafe for ModelCache
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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