pub struct ResponseCache { /* private fields */ }Expand description
LRU cache for RAG responses with per-entry TTL.
Internally uses:
- A
HashMap<u64, CacheEntry>for O(1) lookup by hash. - A
VecDeque<u64>that records insertion/access order for LRU eviction.
Implementations§
Source§impl ResponseCache
impl ResponseCache
Sourcepub fn new(capacity: usize, default_ttl_ms: u64) -> Self
pub fn new(capacity: usize, default_ttl_ms: u64) -> Self
Create a new cache with the given capacity (maximum number of entries)
and default_ttl_ms (entry lifetime in milliseconds).
Sourcepub fn get(&mut self, query: &str, now_ms: u64) -> Option<String>
pub fn get(&mut self, query: &str, now_ms: u64) -> Option<String>
Look up a cached response for query at the given timestamp.
- Returns
Some(response)if an unexpired entry exists and updatesaccess_countand the hit counter. - Returns
None(and increments the miss counter) otherwise.
Sourcepub fn put(&mut self, query: &str, response: String, now_ms: u64)
pub fn put(&mut self, query: &str, response: String, now_ms: u64)
Insert or replace a cache entry for query.
If the cache is at capacity, the least-recently-used entry is evicted first.
Sourcepub fn invalidate(&mut self, query: &str) -> bool
pub fn invalidate(&mut self, query: &str) -> bool
Remove the entry for query (if any).
Returns true if an entry was actually removed.
Sourcepub fn invalidate_expired(&mut self, now_ms: u64) -> usize
pub fn invalidate_expired(&mut self, now_ms: u64) -> usize
Remove all entries whose TTL has elapsed relative to now_ms.
Returns the number of entries that were purged.
Sourcepub fn stats(&self) -> &CacheStats
pub fn stats(&self) -> &CacheStats
Return a reference to the current statistics.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Current number of stored entries (including potentially expired ones).
Sourcepub fn default_ttl_ms(&self) -> u64
pub fn default_ttl_ms(&self) -> u64
The default TTL applied to new entries.
Auto Trait Implementations§
impl Freeze for ResponseCache
impl RefUnwindSafe for ResponseCache
impl Send for ResponseCache
impl Sync for ResponseCache
impl Unpin for ResponseCache
impl UnsafeUnpin for ResponseCache
impl UnwindSafe for ResponseCache
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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 moreimpl<T> MaybeSend for Twhere
T: Send,
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.