文件缓存操作
(key string)
| 316 | // 文件缓存操作 |
| 317 | |
| 318 | func (c *ToolCache) getFromFile(key string) (any, bool) { |
| 319 | filePath := c.getCacheFilePath(key) |
| 320 | |
| 321 | // 读取文件 |
| 322 | data, err := os.ReadFile(filePath) |
| 323 | if err != nil { |
| 324 | return nil, false |
| 325 | } |
| 326 | |
| 327 | // 反序列化 |
| 328 | var entry CacheEntry |
| 329 | if err := json.Unmarshal(data, &entry); err != nil { |
| 330 | c.stats.Errors++ |
| 331 | return nil, false |
| 332 | } |
| 333 | |
| 334 | // 检查是否过期 |
| 335 | if entry.IsExpired() { |
| 336 | // 删除过期文件 |
| 337 | _ = os.Remove(filePath) |
| 338 | return nil, false |
| 339 | } |
| 340 | |
| 341 | return entry.Value, true |
| 342 | } |
| 343 | |
| 344 | func (c *ToolCache) setToFile(key string, value any, ttl time.Duration) error { |
| 345 | // 确保缓存目录存在 |