LoadCache reads a cache file. A missing or corrupt file yields an empty (usable) cache — never an error. Stats must never fail on a bad cache.
(path string)
| 20 | // LoadCache reads a cache file. A missing or corrupt file yields an empty |
| 21 | // (usable) cache — never an error. Stats must never fail on a bad cache. |
| 22 | func LoadCache(path string) *Cache { |
| 23 | c := &Cache{Entries: map[string]cacheEntry{}} |
| 24 | data, err := os.ReadFile(path) |
| 25 | if err != nil { |
| 26 | return c |
| 27 | } |
| 28 | var loaded Cache |
| 29 | if err := json.Unmarshal(data, &loaded); err != nil || loaded.Entries == nil { |
| 30 | return c |
| 31 | } |
| 32 | return &loaded |
| 33 | } |
| 34 | |
| 35 | // Save writes the cache as JSON. |
| 36 | func (c *Cache) Save(path string) error { |
no outgoing calls