(key string, value any, ttl time.Duration)
| 342 | } |
| 343 | |
| 344 | func (c *ToolCache) setToFile(key string, value any, ttl time.Duration) error { |
| 345 | // 确保缓存目录存在 |
| 346 | if err := os.MkdirAll(c.config.CacheDir, 0755); err != nil { |
| 347 | return fmt.Errorf("failed to create cache directory: %w", err) |
| 348 | } |
| 349 | |
| 350 | entry := &CacheEntry{ |
| 351 | Key: key, |
| 352 | Value: value, |
| 353 | CreatedAt: time.Now(), |
| 354 | ExpiresAt: time.Now().Add(ttl), |
| 355 | } |
| 356 | |
| 357 | // 序列化 |
| 358 | data, err := json.Marshal(entry) |
| 359 | if err != nil { |
| 360 | return fmt.Errorf("failed to marshal cache entry: %w", err) |
| 361 | } |
| 362 | |
| 363 | // 检查文件大小限制 |
| 364 | if c.config.MaxFileSize > 0 && int64(len(data)) > c.config.MaxFileSize { |
| 365 | return fmt.Errorf("cache entry too large: %d bytes (max: %d)", len(data), c.config.MaxFileSize) |
| 366 | } |
| 367 | |
| 368 | // 写入文件 |
| 369 | filePath := c.getCacheFilePath(key) |
| 370 | if err := os.WriteFile(filePath, data, 0644); err != nil { |
| 371 | return fmt.Errorf("failed to write cache file: %w", err) |
| 372 | } |
| 373 | |
| 374 | return nil |
| 375 | } |
| 376 | |
| 377 | func (c *ToolCache) deleteFromFile(key string) error { |
| 378 | filePath := c.getCacheFilePath(key) |
no test coverage detected