Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v1.11.3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("cache miss")

Functions

This section is empty.

Types

type CacheConfig

type CacheConfig struct {
	LocalMaxSize    int                // 本地缓存最大条目数
	LocalTTL        time.Duration      // 本地缓存 TTL
	RedisTTL        time.Duration      // Redis 缓存 TTL
	EnableLocal     bool               // 是否启用本地缓存
	EnableRedis     bool               // 是否启用 Redis 缓存
	KeyStrategyType string             // 缓存键策略类型:hash | hierarchical
	CacheableCheck  func(req any) bool // 判断请求是否可缓存
}

CacheConfig 缓存配置

func DefaultCacheConfig

func DefaultCacheConfig() *CacheConfig

DefaultCacheConfig 默认配置

type CacheEntry

type CacheEntry struct {
	Response      any       `json:"response"`
	TokensSaved   int       `json:"tokens_saved"`
	PromptVersion string    `json:"prompt_version,omitempty"`
	ModelVersion  string    `json:"model_version,omitempty"`
	CreatedAt     time.Time `json:"created_at"`
	ExpiresAt     time.Time `json:"expires_at"`
	HitCount      int       `json:"hit_count"`
}

CacheEntry 缓存条目

type CacheStats

type CacheStats struct {
	Hits      int64 `json:"hits"`
	Misses    int64 `json:"misses"`
	Evictions int64 `json:"evictions"`
	Size      int   `json:"size"`
}

CacheStats 跟踪缓存性能.

type CachedToolResult

type CachedToolResult struct {
	Result    json.RawMessage `json:"result"`
	Error     string          `json:"error,omitempty"`
	CachedAt  time.Time       `json:"cached_at"`
	FromCache bool            `json:"from_cache"`
}

CachedToolResult 表示缓存的工具执行结果.

type CachingToolExecutor

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

CachingToolExecutor 将工具执行器用缓存包裹.

func NewCachingToolExecutor

func NewCachingToolExecutor(executor tools.ToolExecutor, cache *ToolResultCache, logger *zap.Logger) *CachingToolExecutor

NewCachingToolExecutor 创建缓存工具执行器.

func (*CachingToolExecutor) Execute

func (e *CachingToolExecutor) Execute(ctx context.Context, calls []types.ToolCall) []tools.ToolResult

Execute 使用缓存执行工具调用.

func (*CachingToolExecutor) ExecuteOne added in v1.0.0

ExecuteOne 使用缓存执行单个工具调用.

type HashKeyStrategy

type HashKeyStrategy struct{}

HashKeyStrategy Hash 缓存键策略 使用全请求 Hash 生成缓存键(原有实现)

func NewHashKeyStrategy

func NewHashKeyStrategy() *HashKeyStrategy

NewHashKeyStrategy 创建 Hash 策略

func (*HashKeyStrategy) GenerateKey

func (s *HashKeyStrategy) GenerateKey(req *llmpkg.ChatRequest) string

GenerateKey 生成 Hash 缓存键

func (*HashKeyStrategy) Name

func (s *HashKeyStrategy) Name() string

Name 返回策略名称

type HierarchicalKeyStrategy

type HierarchicalKeyStrategy struct{}

HierarchicalKeyStrategy 层次化缓存键策略 格式:llm:cache:{tenantID}:{model}:{msgHash} msgHash 只包含系统消息 + 历史消息(不含最后一条用户消息) 这样多轮对话的前 N-1 轮可以共享缓存前缀

func NewHierarchicalKeyStrategy

func NewHierarchicalKeyStrategy() *HierarchicalKeyStrategy

NewHierarchicalKeyStrategy 创建层次化策略

func (*HierarchicalKeyStrategy) GenerateKey

func (s *HierarchicalKeyStrategy) GenerateKey(req *llmpkg.ChatRequest) string

GenerateKey 生成层次化缓存键

func (*HierarchicalKeyStrategy) Name

func (s *HierarchicalKeyStrategy) Name() string

Name 返回策略名称

type KeyStrategy

type KeyStrategy interface {
	// GenerateKey 生成缓存键
	GenerateKey(req *llmpkg.ChatRequest) string

	// Name 返回策略名称(用于日志和调试)
	Name() string
}

KeyStrategy 缓存键生成策略接口

type LRUCache

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

func NewLRUCache

func NewLRUCache(capacity int, ttl time.Duration) *LRUCache

func (*LRUCache) Clear

func (c *LRUCache) Clear()

func (*LRUCache) Delete

func (c *LRUCache) Delete(key string)

func (*LRUCache) Get

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

func (*LRUCache) Set

func (c *LRUCache) Set(key string, entry *CacheEntry)

func (*LRUCache) Stats

func (c *LRUCache) Stats() (size int, capacity int)

Stats 缓存统计

type MultiLevelCache

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

MultiLevelCache 多级缓存实现

func NewMultiLevelCache

func NewMultiLevelCache(rdb *redis.Client, config *CacheConfig, logger *zap.Logger) *MultiLevelCache

NewMultiLevelCache 创建多级缓存

func (*MultiLevelCache) Delete

func (c *MultiLevelCache) Delete(ctx context.Context, key string) error

Delete 删除缓存

func (*MultiLevelCache) GenerateKey

func (c *MultiLevelCache) GenerateKey(req any) string

GenerateKey 生成缓存键(使用策略模式)

func (*MultiLevelCache) Get

func (c *MultiLevelCache) Get(ctx context.Context, key string) (*CacheEntry, error)

Get 获取缓存

func (*MultiLevelCache) InvalidateByVersion

func (c *MultiLevelCache) InvalidateByVersion(ctx context.Context, promptVersion, modelVersion string) error

InvalidateByVersion 按版本失效缓存

func (*MultiLevelCache) IsCacheable

func (c *MultiLevelCache) IsCacheable(req any) bool

IsCacheable 判断请求是否可缓存

func (*MultiLevelCache) Set

func (c *MultiLevelCache) Set(ctx context.Context, key string, entry *CacheEntry) error

Set 设置缓存

func (*MultiLevelCache) Warmup added in v1.6.3

func (c *MultiLevelCache) Warmup(ctx context.Context, maxKeys int) error

Warmup 预热本地缓存:从 Redis 加载访问频率最高的条目到本地 LRU。

type PromptCache

type PromptCache interface {
	Get(ctx context.Context, key string) (*CacheEntry, error)
	Set(ctx context.Context, key string, entry *CacheEntry) error
	Delete(ctx context.Context, key string) error
	GenerateKey(req any) string
}

PromptCache Prompt 缓存接口

type ToolCacheConfig

type ToolCacheConfig struct {
	MaxEntries          int                      `json:"max_entries"`
	DefaultTTL          time.Duration            `json:"default_ttl"`
	EnableSemantic      bool                     `json:"enable_semantic"` // Enable semantic similarity matching
	SimilarityThreshold float64                  `json:"similarity_threshold"`
	ToolTTLOverrides    map[string]time.Duration `json:"tool_ttl_overrides"` // Per-tool TTL
	ExcludedTools       []string                 `json:"excluded_tools"`     // Tools to never cache
}

ToolCacheConfig 配置工具结果缓存.

func DefaultToolCacheConfig

func DefaultToolCacheConfig() ToolCacheConfig

DefaultToolCacheConfig 返回合理的默认值.

type ToolResultCache

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

ToolResultCache 缓存工具执行结果以避免冗余调用.

func NewToolResultCache

func NewToolResultCache(config ToolCacheConfig, logger *zap.Logger) *ToolResultCache

NewToolResultCache 创建新的工具结果缓存.

func (*ToolResultCache) Clear

func (c *ToolResultCache) Clear()

Clear 清除所有缓存条目.

func (*ToolResultCache) Get

func (c *ToolResultCache) Get(toolName string, arguments json.RawMessage) (*CachedToolResult, bool)

Get 获取工具调用的缓存结果.

func (*ToolResultCache) Invalidate

func (c *ToolResultCache) Invalidate(toolName string, arguments json.RawMessage)

Invalidate 删除特定缓存项.

func (*ToolResultCache) InvalidateTool

func (c *ToolResultCache) InvalidateTool(toolName string)

InvalidateTool 删除指定工具的所有缓存项.

func (*ToolResultCache) Set

func (c *ToolResultCache) Set(toolName string, arguments json.RawMessage, result json.RawMessage, err string)

Set 在缓存中设置一个工具结果.

func (*ToolResultCache) Stats

func (c *ToolResultCache) Stats() CacheStats

Stats 返回缓存统计信息.

Jump to

Keyboard shortcuts

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