pub struct OutputCache {
pub entries: HashMap<CacheKey, CachedOutput>,
pub capacity: usize,
pub policy: EvictionPolicy,
pub hits: u64,
pub misses: u64,
pub lcg_state: u64,
}Expand description
In-memory output deduplication cache.
Fields§
§entries: HashMap<CacheKey, CachedOutput>Live entries.
capacity: usizeMaximum number of entries.
policy: EvictionPolicyEviction policy applied when entries.len() == capacity.
hits: u64Lifetime hit counter.
misses: u64Lifetime miss counter.
lcg_state: u64Internal LCG state used by EvictionPolicy::Random.
Implementations§
Source§impl OutputCache
impl OutputCache
Sourcepub fn new(capacity: usize, policy: EvictionPolicy) -> Self
pub fn new(capacity: usize, policy: EvictionPolicy) -> Self
Create a new cache with the given capacity and eviction policy.
Sourcepub fn get(&mut self, key: &CacheKey, now_ms: u64) -> Option<&str>
pub fn get(&mut self, key: &CacheKey, now_ms: u64) -> Option<&str>
Look up key. Returns None if the entry is absent or expired.
On a hit the entry’s access_count is incremented and the hit stat updated.
Sourcepub fn insert(
&mut self,
key: CacheKey,
response: String,
tokens: u64,
ttl_ms: u64,
now_ms: u64,
)
pub fn insert( &mut self, key: CacheKey, response: String, tokens: u64, ttl_ms: u64, now_ms: u64, )
Insert a new entry, evicting one existing entry first if at capacity.
Sourcepub fn evict_one(&mut self, now_ms: u64)
pub fn evict_one(&mut self, now_ms: u64)
Evict one entry: expired entries take priority; otherwise apply policy.
Sourcepub fn prune_expired(&mut self, now_ms: u64) -> usize
pub fn prune_expired(&mut self, now_ms: u64) -> usize
Remove all expired entries and return how many were removed.
Sourcepub fn hit_rate(&self) -> f64
pub fn hit_rate(&self) -> f64
Ratio of hits to total lookups, or 0.0 if no lookups have been made.
Sourcepub fn stats(&self, now_ms: u64) -> CacheStats
pub fn stats(&self, now_ms: u64) -> CacheStats
Return a statistics snapshot for the current moment now_ms.
Auto Trait Implementations§
impl Freeze for OutputCache
impl RefUnwindSafe for OutputCache
impl Send for OutputCache
impl Sync for OutputCache
impl Unpin for OutputCache
impl UnsafeUnpin for OutputCache
impl UnwindSafe for OutputCache
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request