Class ToolResultCache
Caches deterministic tool results to avoid redundant invocations. Tools must be explicitly registered as cacheable. Cache keys are derived from tool name + arguments hash.
public sealed class ToolResultCache
- Inheritance
-
ToolResultCache
- Inherited Members
Constructors
ToolResultCache(IDistributedCacheService, IEnumerable<string>, TimeSpan?, IDictionary<string, TimeSpan>?, ILogger?)
public ToolResultCache(IDistributedCacheService cache, IEnumerable<string> cacheableTools, TimeSpan? defaultTtl = null, IDictionary<string, TimeSpan>? toolTtls = null, ILogger? logger = null)
Parameters
cacheIDistributedCacheServiceBacking cache service.
cacheableToolsIEnumerable<string>Tool names that are safe to cache (deterministic).
defaultTtlTimeSpan?Default TTL for cached results. Default 5 minutes.
toolTtlsIDictionary<string, TimeSpan>Per-tool TTL overrides.
loggerILoggerOptional logger.
Properties
Hits
Number of cache hits.
public long Hits { get; }
Property Value
Misses
Number of cache misses.
public long Misses { get; }
Property Value
Methods
GetCachedResultAsync(string, string, CancellationToken)
Attempts to get a cached result for a tool invocation.
Returns null if the tool is not cacheable or no cached result exists.
public Task<string?> GetCachedResultAsync(string toolName, string arguments, CancellationToken ct = default)
Parameters
toolNamestringargumentsstringctCancellationToken
Returns
InvalidateToolAsync(string, CancellationToken)
Invalidates all cached results for a specific tool.
public Task InvalidateToolAsync(string toolName, CancellationToken ct = default)
Parameters
toolNamestringctCancellationToken
Returns
IsCacheable(string)
Checks if a tool is registered as cacheable.
public bool IsCacheable(string toolName)
Parameters
toolNamestring
Returns
SetResultAsync(string, string, string, CancellationToken)
Stores a tool result in the cache.
public Task SetResultAsync(string toolName, string arguments, string result, CancellationToken ct = default)
Parameters
toolNamestringargumentsstringresultstringctCancellationToken