pub struct Cache { /* private fields */ }Expand description
A thread-safe Least Recently Used (LRU) cache.
This struct wraps an LruCache in a Mutex to allow concurrent access
from multiple threads. It maps canonicalised JSON strings to their
corresponding database IDs. It also tracks hit and miss statistics.
Implementations§
Source§impl Cache
impl Cache
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a new Cache with the specified capacity.
§Arguments
capacity- The maximum number of items the cache can hold. Minimum capacity is 1.
Sourcepub fn put(&self, key: String, value: i32)
pub fn put(&self, key: String, value: i32)
Inserts a key-value pair into the cache.
§Arguments
key- The canonicalised JSON string key.value- The database ID associated with the key.
If the cache mutex is poisoned, the operation is silently skipped.
Sourcepub fn hit_rate(&self) -> f64
pub fn hit_rate(&self) -> f64
Returns the cache hit rate as a percentage.
§Returns
The hit rate as a float between 0.0 and 100.0. Returns 0.0 if no cache operations have occurred.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the current number of items in the cache.
§Returns
The number of items currently stored in the cache. Returns 0 if the cache mutex is poisoned.
Auto Trait Implementations§
impl !Freeze for Cache
impl RefUnwindSafe for Cache
impl Send for Cache
impl Sync for Cache
impl Unpin for Cache
impl UnwindSafe for Cache
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