mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
feat(cache): Enhance cache file management and cleanup
- Add startup cleanup of stale and temporary files - Modify ClearCache to remove all cache files except config - Implement cleanStaleFiles method to remove orphaned and temporary cache files - Improve file management by tracking and cleaning unused cache entries
This commit is contained in:
parent
b63f38c95d
commit
1f277997d3
70
internal/cache/manager.go
vendored
70
internal/cache/manager.go
vendored
@ -102,6 +102,11 @@ func NewCacheManager(cacheDir string) (*CacheManager, error) {
|
||||
log.Printf("[Cache] Failed to load config: %v, using default values", err)
|
||||
}
|
||||
|
||||
// 启动时清理过期和临时文件
|
||||
if err := cm.cleanStaleFiles(); err != nil {
|
||||
log.Printf("[Cache] Failed to clean stale files: %v", err)
|
||||
}
|
||||
|
||||
// 启动清理协程
|
||||
cm.startCleanup()
|
||||
|
||||
@ -336,21 +341,34 @@ func (cm *CacheManager) SetEnabled(enabled bool) {
|
||||
|
||||
// ClearCache 清空缓存
|
||||
func (cm *CacheManager) ClearCache() error {
|
||||
// 删除所有缓存文件
|
||||
// 清除内存中的缓存项
|
||||
var keysToDelete []CacheKey
|
||||
cm.items.Range(func(key, value interface{}) bool {
|
||||
cacheKey := key.(CacheKey)
|
||||
item := value.(*CacheItem)
|
||||
os.Remove(item.FilePath)
|
||||
keysToDelete = append(keysToDelete, cacheKey)
|
||||
return true
|
||||
})
|
||||
|
||||
// 清除缓存项
|
||||
for _, key := range keysToDelete {
|
||||
cm.items.Delete(key)
|
||||
}
|
||||
|
||||
// 清理缓存目录中的所有文件
|
||||
entries, err := os.ReadDir(cm.cacheDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read cache directory: %v", err)
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.Name() == "config.json" {
|
||||
continue // 保留配置文件
|
||||
}
|
||||
filePath := filepath.Join(cm.cacheDir, entry.Name())
|
||||
if err := os.Remove(filePath); err != nil {
|
||||
log.Printf("[Cache] Failed to remove file %s: %v", filePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
// 重置统计信息
|
||||
cm.hitCount.Store(0)
|
||||
cm.missCount.Store(0)
|
||||
@ -359,6 +377,50 @@ func (cm *CacheManager) ClearCache() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// cleanStaleFiles 清理过期和临时文件
|
||||
func (cm *CacheManager) cleanStaleFiles() error {
|
||||
entries, err := os.ReadDir(cm.cacheDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read cache directory: %v", err)
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.Name() == "config.json" {
|
||||
continue // 保留配置文件
|
||||
}
|
||||
|
||||
filePath := filepath.Join(cm.cacheDir, entry.Name())
|
||||
|
||||
// 清理临时文件
|
||||
if strings.HasPrefix(entry.Name(), "temp-") {
|
||||
if err := os.Remove(filePath); err != nil {
|
||||
log.Printf("[Cache] Failed to remove temp file %s: %v", filePath, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// 检查文件是否仍在缓存记录中
|
||||
fileFound := false
|
||||
cm.items.Range(func(_, value interface{}) bool {
|
||||
item := value.(*CacheItem)
|
||||
if item.FilePath == filePath {
|
||||
fileFound = true
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
// 如果文件不在缓存记录中,删除它
|
||||
if !fileFound {
|
||||
if err := os.Remove(filePath); err != nil {
|
||||
log.Printf("[Cache] Failed to remove stale file %s: %v", filePath, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateTemp 创建临时缓存文件
|
||||
func (cm *CacheManager) CreateTemp(key CacheKey, resp *http.Response) (*os.File, error) {
|
||||
if !cm.enabled.Load() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user