pub struct ToolCache<V> { /* private fields */ }Expand description
LRU cache for tool results, keyed by (tool_name, args).
V is the cached value type. Use ToolCache::new, optionally chaining
ToolCache::with_capacity and ToolCache::with_ttl to configure the
cache:
use std::time::Duration;
use tool_result_cache::ToolCache;
let _cache: ToolCache<String> = ToolCache::new()
.with_capacity(256)
.with_ttl(Duration::from_secs(60));Implementations§
Source§impl<V> ToolCache<V>
impl<V> ToolCache<V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new cache with a default capacity of 1024 and no TTL.
Capacity-based eviction only kicks in once the cache holds more than
max_size entries. Use ToolCache::with_capacity or
ToolCache::with_ttl to override.
Sourcepub fn with_capacity(self, max_size: usize) -> Self
pub fn with_capacity(self, max_size: usize) -> Self
Set the maximum number of entries; 0 disables capacity-based eviction.
Sourcepub fn with_ttl(self, ttl: Duration) -> Self
pub fn with_ttl(self, ttl: Duration) -> Self
Set the default TTL for new entries. Per-call TTLs override this value.
Sourcepub fn with_clock<F>(self, clock: F) -> Self
pub fn with_clock<F>(self, clock: F) -> Self
Replace the cache clock. Default is Instant::now. Intended for tests.
Sourcepub fn evictions(&self) -> u64
pub fn evictions(&self) -> u64
Number of entries dropped because the cache exceeded its capacity.
Sourcepub fn expirations(&self) -> u64
pub fn expirations(&self) -> u64
Number of entries dropped because their TTL elapsed.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Snapshot of all counters at this point in time. The snapshot is a copy and will not change as the cache continues to be used.
Sourcepub fn get(&mut self, tool_name: &str, args: &Value) -> Option<&V>
pub fn get(&mut self, tool_name: &str, args: &Value) -> Option<&V>
Look up a cached value by tool name + args. Updates hit/miss stats.
Sourcepub fn set(&mut self, tool_name: &str, args: &Value, value: V)
pub fn set(&mut self, tool_name: &str, args: &Value, value: V)
Insert or replace a cached value using the cache’s default TTL.
Sourcepub fn set_with_ttl(
&mut self,
tool_name: &str,
args: &Value,
value: V,
ttl: Duration,
)
pub fn set_with_ttl( &mut self, tool_name: &str, args: &Value, value: V, ttl: Duration, )
Insert or replace a cached value with a per-call TTL override.
Sourcepub fn get_or_set<F>(&mut self, tool_name: &str, args: &Value, compute: F) -> &Vwhere
F: FnOnce() -> V,
pub fn get_or_set<F>(&mut self, tool_name: &str, args: &Value, compute: F) -> &Vwhere
F: FnOnce() -> V,
Return the cached value if present, else call compute and cache it.
Sourcepub fn get_or_set_with_ttl<F>(
&mut self,
tool_name: &str,
args: &Value,
compute: F,
ttl: Duration,
) -> &Vwhere
F: FnOnce() -> V,
pub fn get_or_set_with_ttl<F>(
&mut self,
tool_name: &str,
args: &Value,
compute: F,
ttl: Duration,
) -> &Vwhere
F: FnOnce() -> V,
Same as ToolCache::get_or_set but with a per-call TTL override.
Sourcepub fn invalidate(&mut self, tool_name: &str, args: &Value) -> bool
pub fn invalidate(&mut self, tool_name: &str, args: &Value) -> bool
Drop a single entry. Returns true if it was present.