cache

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cache provides tool result caching for LLM agent tool execution.

It defines the ToolCache interface, an in-memory LRU backend (LRUCache), a CachedTool decorator wrapping tools.Tool, and helpers for deterministic cache key generation and cache policy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheCountersFromTool

func CacheCountersFromTool(t tools.Tool) (hits, misses int64, ok bool)

CacheCountersFromTool extracts cache counters from a Tool wrapped by WrapTool. Returns (0, 0, false) if the tool is not cached.

func CacheKey

func CacheKey(toolName string, args map[string]any) string

CacheKey generates a deterministic cache key for a tool call. Format: toolName:SHA256(canonicalJSON(args))

func WrapTool

func WrapTool(inner tools.Tool, cache ToolCache, ttl time.Duration, policy CachePolicy) tools.Tool

WrapTool wraps a tool with caching.

Types

type CachePolicy

type CachePolicy func(args map[string]any, result string, err error) bool

CachePolicy determines whether a tool result should be cached. Return false to prevent caching (e.g., error results, non-deterministic tools).

type LRUCache

type LRUCache struct {
	// contains filtered or unexported fields
}

LRUCache is an in-memory LRU cache with per-entry TTL.

func NewLRUCache

func NewLRUCache(maxSize int) *LRUCache

NewLRUCache creates a new LRU cache with the given max size. If maxSize <= 0, defaults to 1000.

func (*LRUCache) Clear

func (c *LRUCache) Clear()

Clear removes all entries from the cache.

func (*LRUCache) Delete

func (c *LRUCache) Delete(key string)

Delete removes a single entry from the cache.

func (*LRUCache) Get

func (c *LRUCache) Get(key string) (any, bool)

Get retrieves a value from the cache by key. Returns (value, true) on hit, (nil, false) on miss or expired entry.

func (*LRUCache) Set

func (c *LRUCache) Set(key string, value any, ttl time.Duration)

Set stores a value in the cache with an optional TTL. If ttl <= 0, the entry never expires (evicted only by LRU).

type ToolCache

type ToolCache interface {
	Get(key string) (value any, ok bool)
	Set(key string, value any, ttl time.Duration)
	Delete(key string)
	Clear()
}

ToolCache is the interface for cache backends.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL