Table of Contents

Class ToolResultCache

Namespace
JD.AI.Core.Caching
Assembly
JD.AI.Core.dll

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

cache IDistributedCacheService

Backing cache service.

cacheableTools IEnumerable<string>

Tool names that are safe to cache (deterministic).

defaultTtl TimeSpan?

Default TTL for cached results. Default 5 minutes.

toolTtls IDictionary<string, TimeSpan>

Per-tool TTL overrides.

logger ILogger

Optional logger.

Properties

Hits

Number of cache hits.

public long Hits { get; }

Property Value

long

Misses

Number of cache misses.

public long Misses { get; }

Property Value

long

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

toolName string
arguments string
ct CancellationToken

Returns

Task<string>

InvalidateToolAsync(string, CancellationToken)

Invalidates all cached results for a specific tool.

public Task InvalidateToolAsync(string toolName, CancellationToken ct = default)

Parameters

toolName string
ct CancellationToken

Returns

Task

IsCacheable(string)

Checks if a tool is registered as cacheable.

public bool IsCacheable(string toolName)

Parameters

toolName string

Returns

bool

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

toolName string
arguments string
result string
ct CancellationToken

Returns

Task