MCPcopy Create free account
hub / github.com/astercloud/aster / setToFile

Method setToFile

pkg/tools/cache.go:344–375  ·  view source on GitHub ↗
(key string, value any, ttl time.Duration)

Source from the content-addressed store, hash-verified

342}
343
344func (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
377func (c *ToolCache) deleteFromFile(key string) error {
378 filePath := c.getCacheFilePath(key)

Callers 1

SetMethod · 0.95

Calls 2

getCacheFilePathMethod · 0.95
AddMethod · 0.65

Tested by

no test coverage detected