pub struct DedupCache { /* private fields */ }Expand description
Request deduplication cache.
Stores responses keyed by RequestKey. When the cache is full, the
oldest entry (by insertion order, tracked via a monotonic sequence counter)
is evicted to make room for the new one.
§Thread safety
DedupCache is not Sync by design; wrap it in a Mutex or
RwLock when sharing across threads.
Implementations§
Source§impl DedupCache
impl DedupCache
Sourcepub fn new(capacity: usize, default_ttl: Duration) -> Self
pub fn new(capacity: usize, default_ttl: Duration) -> Self
Create a new cache with the given capacity and default_ttl.
Sourcepub fn with_capacity(n: usize) -> Self
pub fn with_capacity(n: usize) -> Self
Shorthand constructor with a 60-second default TTL.
Sourcepub fn get(&mut self, key: &RequestKey) -> Option<&str>
pub fn get(&mut self, key: &RequestKey) -> Option<&str>
Look up a cached response.
Returns None if the key is not present or if the cached entry has
expired. On a valid hit the hit_count of the entry is incremented
and the response string slice is returned.
Sourcepub fn insert(&mut self, key: RequestKey, response: String)
pub fn insert(&mut self, key: RequestKey, response: String)
Insert a response with the DedupCache::default_ttl.
If the cache is at capacity, the entry with the smallest insertion sequence number (i.e. the oldest entry) is evicted first.
Sourcepub fn insert_with_ttl(
&mut self,
key: RequestKey,
response: String,
ttl: Duration,
)
pub fn insert_with_ttl( &mut self, key: RequestKey, response: String, ttl: Duration, )
Insert a response with a custom ttl.
Evicts the oldest entry when the cache is at capacity before inserting.
Sourcepub fn evict_expired(&mut self) -> usize
pub fn evict_expired(&mut self) -> usize
Remove all expired entries.
Returns the number of entries removed.
Sourcepub fn stats(&self) -> &DedupStats
pub fn stats(&self) -> &DedupStats
Reference to the current statistics snapshot.
Auto Trait Implementations§
impl Freeze for DedupCache
impl RefUnwindSafe for DedupCache
impl Send for DedupCache
impl Sync for DedupCache
impl Unpin for DedupCache
impl UnsafeUnpin for DedupCache
impl UnwindSafe for DedupCache
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
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>
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>
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