pub struct ResultCache { /* private fields */ }Expand description
Thread-safe result cache with LRU eviction and TTL
§Performance Characteristics
- Get: O(1) average, O(n) worst case for access order update
- Insert: O(1) average, O(n) worst case for eviction
- Memory: O(max_size * entry_size)
§Thread Safety
Uses Arc<RwLock<_>> for interior mutability:
- Multiple concurrent readers
- Exclusive writer access
- Clone creates a new reference to same cache
Implementations§
Source§impl ResultCache
impl ResultCache
Sourcepub fn new(config: CacheConfig) -> Self
pub fn new(config: CacheConfig) -> Self
Create a new result cache with the given configuration
§Example
use llm_shield_models::cache::{ResultCache, CacheConfig};
use std::time::Duration;
let cache = ResultCache::new(CacheConfig {
max_size: 1000,
ttl: Duration::from_secs(300),
});Sourcepub fn get(&self, key: &str) -> Option<ScanResult>
pub fn get(&self, key: &str) -> Option<ScanResult>
Get a cached result by key
Returns None if:
- Key doesn’t exist
- Entry has expired (and removes it)
Updates LRU access order on cache hit.
Sourcepub fn insert(&self, key: String, result: ScanResult)
pub fn insert(&self, key: String, result: ScanResult)
Insert or update a cache entry
If the cache is at capacity, evicts the least recently used entry. If the key already exists, updates it and refreshes the TTL.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of entries in the cache
Note: This includes expired entries that haven’t been lazily cleaned yet.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get cache statistics
Sourcepub fn reset_stats(&self)
pub fn reset_stats(&self)
Reset cache statistics
This does not affect cached entries.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ResultCache
impl RefUnwindSafe for ResultCache
impl Send for ResultCache
impl Sync for ResultCache
impl Unpin for ResultCache
impl UnwindSafe for ResultCache
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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