Documentation
¶
Index ¶
- Variables
- type Cache
- type CacheConfig
- type CacheEntry
- type CacheKeyGenerator
- type CacheStats
- type InMemoryCache
- func (c *InMemoryCache) Clear(ctx context.Context) error
- func (c *InMemoryCache) Close()
- func (c *InMemoryCache) Delete(ctx context.Context, key string) error
- func (c *InMemoryCache) Get(ctx context.Context, key string) (interface{}, error)
- func (c *InMemoryCache) GetStats() CacheStats
- func (c *InMemoryCache) Has(ctx context.Context, key string) (bool, error)
- func (c *InMemoryCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- type LRUCache
- type MultiTierCache
- func (c *MultiTierCache) Clear(ctx context.Context) error
- func (c *MultiTierCache) Delete(ctx context.Context, key string) error
- func (c *MultiTierCache) Get(ctx context.Context, key string) (interface{}, error)
- func (c *MultiTierCache) GetStats() CacheStats
- func (c *MultiTierCache) Has(ctx context.Context, key string) (bool, error)
- func (c *MultiTierCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- type NoOpCache
- func (c *NoOpCache) Clear(ctx context.Context) error
- func (c *NoOpCache) Delete(ctx context.Context, key string) error
- func (c *NoOpCache) Get(ctx context.Context, key string) (interface{}, error)
- func (c *NoOpCache) GetStats() CacheStats
- func (c *NoOpCache) Has(ctx context.Context, key string) (bool, error)
- func (c *NoOpCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Constants ¶
This section is empty.
Variables ¶
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 根据配置创建缓存
type CacheConfig ¶
type CacheConfig struct {
Enabled bool // 是否启用缓存
Type string // 缓存类型: "memory", "redis", "multi-tier"
MaxSize int // 最大条目数
DefaultTTL time.Duration // 默认 TTL
CleanupInterval time.Duration // 清理间隔
}
CacheConfig 缓存配置
type CacheEntry ¶
type CacheEntry struct {
Key string // 键
Value interface{} // 值
CreateTime time.Time // 创建时间
ExpireTime time.Time // 过期时间
AccessTime time.Time // 最后访问时间
AccessCount int64 // 访问次数
}
CacheEntry 缓存条目
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 提供线程安全的内存缓存 相比 sync.Map,在读多写少且需要遍历的场景下性能更好
func NewInMemoryCache ¶
func NewInMemoryCache(maxSize int, defaultTTL, cleanupInterval time.Duration) *InMemoryCache
NewInMemoryCache 创建内存缓存
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 获取缓存值
type LRUCache ¶
type LRUCache struct {
*InMemoryCache
}
LRUCache LRU (Least Recently Used) 缓存
当缓存满时,驱逐最近最少使用的条目
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 获取第一层的统计信息