Clear removes all items from the cache.
()
| 289 | |
| 290 | // Clear removes all items from the cache. |
| 291 | func (c *FileCache) Clear() error { |
| 292 | c.mutex.Lock() |
| 293 | defer c.mutex.Unlock() |
| 294 | |
| 295 | // Clear in-memory cache and LRU list |
| 296 | c.inMemory = make(map[string]*list.Element) |
| 297 | c.lruList = list.New() |
| 298 | |
| 299 | // If persisted, remove all cache files |
| 300 | if c.persisted { |
| 301 | files, err := os.ReadDir(c.dir) |
| 302 | if err != nil { |
| 303 | return fmt.Errorf("failed to read cache directory: %w", err) |
| 304 | } |
| 305 | |
| 306 | for _, file := range files { |
| 307 | if file.IsDir() || filepath.Ext(file.Name()) != ".json" { |
| 308 | continue |
| 309 | } |
| 310 | |
| 311 | if err := os.Remove(filepath.Join(c.dir, file.Name())); err != nil { |
| 312 | return fmt.Errorf("failed to remove cache file %s: %w", file.Name(), err) |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return nil |
| 318 | } |
| 319 | |
| 320 | // Close implements the Cache.Close method for FileCache |
| 321 | // This is a no-op for FileCache since it doesn't maintain any resources that need explicit closing. |