Skip to main content

ToolCache

Struct ToolCache 

Source
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>

Source

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.

Source

pub fn with_capacity(self, max_size: usize) -> Self

Set the maximum number of entries; 0 disables capacity-based eviction.

Source

pub fn with_ttl(self, ttl: Duration) -> Self

Set the default TTL for new entries. Per-call TTLs override this value.

Source

pub fn with_clock<F>(self, clock: F) -> Self
where F: Fn() -> Instant + Send + Sync + 'static,

Replace the cache clock. Default is Instant::now. Intended for tests.

Source

pub fn len(&self) -> usize

Number of cached entries currently held.

Source

pub fn is_empty(&self) -> bool

true if the cache currently holds no entries.

Source

pub fn hits(&self) -> u64

Number of get / get_or_set calls that found a live entry.

Source

pub fn misses(&self) -> u64

Number of get / get_or_set calls that did not find a live entry.

Source

pub fn evictions(&self) -> u64

Number of entries dropped because the cache exceeded its capacity.

Source

pub fn expirations(&self) -> u64

Number of entries dropped because their TTL elapsed.

Source

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.

Source

pub fn clear(&mut self)

Drop all entries and reset statistics.

Source

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.

Source

pub fn set(&mut self, tool_name: &str, args: &Value, value: V)

Insert or replace a cached value using the cache’s default TTL.

Source

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.

Source

pub fn get_or_set<F>(&mut self, tool_name: &str, args: &Value, compute: F) -> &V
where F: FnOnce() -> V,

Return the cached value if present, else call compute and cache it.

Source

pub fn get_or_set_with_ttl<F>( &mut self, tool_name: &str, args: &Value, compute: F, ttl: Duration, ) -> &V
where F: FnOnce() -> V,

Same as ToolCache::get_or_set but with a per-call TTL override.

Source

pub fn invalidate(&mut self, tool_name: &str, args: &Value) -> bool

Drop a single entry. Returns true if it was present.

Trait Implementations§

Source§

impl<V> Default for ToolCache<V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<V> Freeze for ToolCache<V>

§

impl<V> !RefUnwindSafe for ToolCache<V>

§

impl<V> Send for ToolCache<V>
where V: Send,

§

impl<V> Sync for ToolCache<V>
where V: Sync,

§

impl<V> Unpin for ToolCache<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for ToolCache<V>

§

impl<V> !UnwindSafe for ToolCache<V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.