| 426 | } |
| 427 | |
| 428 | func (c *ToolCache) cleanupFiles() { |
| 429 | if _, err := os.Stat(c.config.CacheDir); os.IsNotExist(err) { |
| 430 | return |
| 431 | } |
| 432 | |
| 433 | entries, err := os.ReadDir(c.config.CacheDir) |
| 434 | if err != nil { |
| 435 | c.stats.Errors++ |
| 436 | return |
| 437 | } |
| 438 | |
| 439 | for _, entry := range entries { |
| 440 | if entry.IsDir() { |
| 441 | continue |
| 442 | } |
| 443 | |
| 444 | filePath := filepath.Join(c.config.CacheDir, entry.Name()) |
| 445 | |
| 446 | // 读取并检查是否过期 |
| 447 | data, err := os.ReadFile(filePath) |
| 448 | if err != nil { |
| 449 | continue |
| 450 | } |
| 451 | |
| 452 | var cacheEntry CacheEntry |
| 453 | if err := json.Unmarshal(data, &cacheEntry); err != nil { |
| 454 | continue |
| 455 | } |
| 456 | |
| 457 | if cacheEntry.IsExpired() { |
| 458 | _ = os.Remove(filePath) |
| 459 | c.stats.Evictions++ |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // CachedTool 带缓存的工具包装器 |
| 465 | type CachedTool struct { |