mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 02:05:21 +00:00
feat(cache): enhance disk cache management with concurrency control and cleanup optimizations
This commit is contained in:
@@ -127,7 +127,11 @@ func CleanupOldDiskCacheFiles(maxAge time.Duration) error {
|
||||
continue
|
||||
}
|
||||
if now.Sub(info.ModTime()) > maxAge {
|
||||
os.Remove(filepath.Join(dir, entry.Name()))
|
||||
// 注意:后台清理任务删除文件时,由于无法得知原始 base64Size,
|
||||
// 只能按磁盘文件大小扣减。这在目前 base64 存储模式下是准确的。
|
||||
if err := os.Remove(filepath.Join(dir, entry.Name())); err == nil {
|
||||
DecrementDiskFiles(info.Size())
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -113,8 +113,12 @@ func IncrementDiskFiles(size int64) {
|
||||
|
||||
// DecrementDiskFiles 减少磁盘文件计数
|
||||
func DecrementDiskFiles(size int64) {
|
||||
atomic.AddInt64(&diskCacheStats.ActiveDiskFiles, -1)
|
||||
atomic.AddInt64(&diskCacheStats.CurrentDiskUsageBytes, -size)
|
||||
if atomic.AddInt64(&diskCacheStats.ActiveDiskFiles, -1) < 0 {
|
||||
atomic.StoreInt64(&diskCacheStats.ActiveDiskFiles, 0)
|
||||
}
|
||||
if atomic.AddInt64(&diskCacheStats.CurrentDiskUsageBytes, -size) < 0 {
|
||||
atomic.StoreInt64(&diskCacheStats.CurrentDiskUsageBytes, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// IncrementMemoryBuffers 增加内存缓存计数
|
||||
|
||||
Reference in New Issue
Block a user