cache

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss     = errors.New("cache miss")
	ErrCacheInvalid  = errors.New("invalid cache entry")
	ErrCacheDisabled = errors.New("cache is disabled")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get 获取缓存值
	Get(ctx context.Context, key string) (interface{}, error)

	// Set 设置缓存值
	Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

	// Delete 删除缓存值
	Delete(ctx context.Context, key string) error

	// Clear 清空所有缓存
	Clear(ctx context.Context) error

	// Has 检查键是否存在
	Has(ctx context.Context, key string) (bool, error)

	// GetStats 获取缓存统计信息
	GetStats() CacheStats
}

Cache 定义缓存接口

借鉴 LangChain 的缓存设计,用于缓存 LLM 调用结果 减少 API 调用次数,降低成本和延迟

func NewCacheFromConfig

func NewCacheFromConfig(config CacheConfig) Cache

NewCacheFromConfig 根据配置创建缓存

已简化为使用 SimpleCache,删除过度设计的 LRU/MultiTier 等实现

type CacheConfig

type CacheConfig struct {
	Enabled         bool          // 是否启用缓存
	Type            string        // 缓存类型: "memory", "redis", "multi-tier"
	MaxSize         int           // 最大条目数
	DefaultTTL      time.Duration // 默认 TTL
	CleanupInterval time.Duration // 清理间隔
}

CacheConfig 缓存配置

func DefaultCacheConfig

func DefaultCacheConfig() CacheConfig

DefaultCacheConfig 返回默认配置

type CacheEntry

type CacheEntry struct {
	Key         string      // 键
	Value       interface{} // 值
	CreateTime  time.Time   // 创建时间
	ExpireTime  time.Time   // 过期时间
	AccessTime  time.Time   // 最后访问时间
	AccessCount int64       // 访问次数
}

CacheEntry 缓存条目

func (*CacheEntry) IsExpired

func (e *CacheEntry) IsExpired() bool

IsExpired 检查是否过期

type CacheKeyGenerator

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

CacheKeyGenerator 缓存键生成器

func NewCacheKeyGenerator

func NewCacheKeyGenerator(prefix string) *CacheKeyGenerator

NewCacheKeyGenerator 创建键生成器

func (*CacheKeyGenerator) GenerateKey

func (g *CacheKeyGenerator) GenerateKey(prompt string, params map[string]interface{}) string

GenerateKey 生成缓存键

根据提示和参数生成唯一的缓存键

func (*CacheKeyGenerator) GenerateKeySimple

func (g *CacheKeyGenerator) GenerateKeySimple(parts ...string) string

GenerateKeySimple 生成简单的缓存键

Optimized to use strings.Builder for efficient string concatenation and avoid multiple allocations in the loop.

type CacheStats

type CacheStats struct {
	Hits      int64   // 命中次数
	Misses    int64   // 未命中次数
	Sets      int64   // 设置次数
	Deletes   int64   // 删除次数
	Evictions int64   // 驱逐次数
	Size      int64   // 当前大小
	MaxSize   int64   // 最大大小
	HitRate   float64 // 命中率
}

CacheStats 缓存统计信息

type InMemoryCache

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

InMemoryCache 内存缓存实现

使用 sync.RWMutex + map 提供线程安全的内存缓存

func NewInMemoryCache

func NewInMemoryCache(maxSize int, defaultTTL, cleanupInterval time.Duration) *InMemoryCache

NewInMemoryCache 创建内存缓存

func (*InMemoryCache) Clear

func (c *InMemoryCache) Clear(ctx context.Context) error

Clear 清空所有缓存

func (*InMemoryCache) Close

func (c *InMemoryCache) Close()

Close 关闭缓存

func (*InMemoryCache) Delete

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

Delete 删除缓存值

func (*InMemoryCache) Get

func (c *InMemoryCache) Get(ctx context.Context, key string) (interface{}, error)

Get 获取缓存值

func (*InMemoryCache) GetStats

func (c *InMemoryCache) GetStats() CacheStats

GetStats 获取统计信息

func (*InMemoryCache) Has

func (c *InMemoryCache) Has(ctx context.Context, key string) (bool, error)

Has 检查键是否存在

func (*InMemoryCache) Set

func (c *InMemoryCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set 设置缓存值

type LRUCache

type LRUCache struct {
	*InMemoryCache
}

LRUCache LRU (Least Recently Used) 缓存

当缓存满时,驱逐最近最少使用的条目

func NewLRUCache

func NewLRUCache(maxSize int, defaultTTL, cleanupInterval time.Duration) *LRUCache

NewLRUCache 创建 LRU 缓存

type MultiTierCache

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

MultiTierCache 多级缓存

支持多个缓存层,如 L1 内存 + L2 Redis

func NewMultiTierCache

func NewMultiTierCache(tiers ...Cache) *MultiTierCache

NewMultiTierCache 创建多级缓存

func (*MultiTierCache) Clear

func (c *MultiTierCache) Clear(ctx context.Context) error

Clear 清空所有层级

func (*MultiTierCache) Delete

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

Delete 从所有层级删除

func (*MultiTierCache) Get

func (c *MultiTierCache) Get(ctx context.Context, key string) (interface{}, error)

Get 从各级缓存获取

func (*MultiTierCache) GetStats

func (c *MultiTierCache) GetStats() CacheStats

GetStats 获取第一层的统计信息

func (*MultiTierCache) Has

func (c *MultiTierCache) Has(ctx context.Context, key string) (bool, error)

Has 检查键是否存在于任何层级

func (*MultiTierCache) Set

func (c *MultiTierCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set 设置到所有层级

type NoOpCache

type NoOpCache struct{}

NoOpCache 无操作缓存

用于禁用缓存的场景

func NewNoOpCache

func NewNoOpCache() *NoOpCache

NewNoOpCache 创建无操作缓存

func (*NoOpCache) Clear

func (c *NoOpCache) Clear(ctx context.Context) error

func (*NoOpCache) Delete

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

func (*NoOpCache) Get

func (c *NoOpCache) Get(ctx context.Context, key string) (interface{}, error)

func (*NoOpCache) GetStats

func (c *NoOpCache) GetStats() CacheStats

func (*NoOpCache) Has

func (c *NoOpCache) Has(ctx context.Context, key string) (bool, error)

func (*NoOpCache) Set

func (c *NoOpCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

type SimpleCache added in v0.5.0

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

SimpleCache 简化的缓存实现

使用 sync.Map + TTL 提供线程安全的缓存,删除所有过度设计的特性: - 删除 LRU (实际场景不需要) - 删除多层缓存 (单进程应用不需要) - 删除分片 (sync.Map内部已优化) - 删除自动调优 (过度设计) - 删除依赖管理 (过度复杂)

func NewSimpleCache added in v0.5.0

func NewSimpleCache(ttl time.Duration) *SimpleCache

NewSimpleCache 创建简化缓存

func (*SimpleCache) Clear added in v0.5.0

func (c *SimpleCache) Clear(ctx context.Context) error

Clear 清空所有缓存

func (*SimpleCache) Close added in v0.5.0

func (c *SimpleCache) Close()

Close 关闭缓存

func (*SimpleCache) Delete added in v0.5.0

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

Delete 删除缓存值

func (*SimpleCache) Get added in v0.5.0

func (c *SimpleCache) Get(ctx context.Context, key string) (interface{}, error)

Get 获取缓存值

func (*SimpleCache) GetStats added in v0.5.0

func (c *SimpleCache) GetStats() CacheStats

GetStats 获取统计信息

func (*SimpleCache) Has added in v0.5.0

func (c *SimpleCache) Has(ctx context.Context, key string) (bool, error)

Has 检查键是否存在

func (*SimpleCache) Set added in v0.5.0

func (c *SimpleCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set 设置缓存值

Jump to

Keyboard shortcuts

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