feat: 添加本地缓存功能,减少API调用

- 实现SQLite缓存模块,支持高效查询和存储
- 添加缓存键生成策略(基于原文+语言对的SHA256哈希)
- 集成缓存到Translator类,先查缓存再调用API
- 添加缓存管理命令:cache clear, cache stats, cache cleanup
- 实现组合缓存清理策略(数量限制+时间过期)
- 添加完整的单元测试
- 更新配置文件模板,添加缓存配置
- 更新文档和版本记录

版本: v0.5.1
This commit is contained in:
2026-03-29 21:10:28 +08:00
parent ceed482444
commit b71f76c8b3
15 changed files with 1545 additions and 7 deletions

View File

@@ -26,6 +26,9 @@ type Config struct {
// 内容过滤配置
SkipKeywords []string `yaml:"skip_keywords"` // 不翻译的关键词
// 缓存配置
Cache CacheConfig `yaml:"cache"`
}
// ProviderConfig 厂商配置
@@ -36,6 +39,14 @@ type ProviderConfig struct {
Enabled bool `yaml:"enabled"`
}
// CacheConfig 缓存配置
type CacheConfig struct {
Enabled bool `yaml:"enabled"`
MaxRecords int `yaml:"max_records"`
ExpireDays int `yaml:"expire_days"`
DBPath string `yaml:"db_path"`
}
// ConfigLoader 配置加载器接口
type ConfigLoader interface {
Load(path string) (*Config, error)
@@ -134,6 +145,17 @@ func (c *Config) setDefaults() {
"BUG:", "WARN:", "IMPORTANT:",
}
}
// 设置缓存配置默认值
if c.Cache.MaxRecords <= 0 {
c.Cache.MaxRecords = 10000
}
if c.Cache.ExpireDays <= 0 {
c.Cache.ExpireDays = 30
}
if c.Cache.DBPath == "" {
c.Cache.DBPath = "~/.config/yoyo/cache.db"
}
}
// GetProviderConfig 获取指定厂商的配置
@@ -221,5 +243,10 @@ func (c *Config) String() string {
for name := range c.Prompts {
builder.WriteString(fmt.Sprintf(" %s\n", name))
}
builder.WriteString("Cache:\n")
builder.WriteString(fmt.Sprintf(" enabled: %v\n", c.Cache.Enabled))
builder.WriteString(fmt.Sprintf(" max_records: %d\n", c.Cache.MaxRecords))
builder.WriteString(fmt.Sprintf(" expire_days: %d\n", c.Cache.ExpireDays))
builder.WriteString(fmt.Sprintf(" db_path: %s\n", c.Cache.DBPath))
return builder.String()
}