Load loads the cache from disk
()
| 175 | |
| 176 | // Load loads the cache from disk |
| 177 | func (c *ActionCache) Load() error { |
| 178 | actionCacheLog.Printf("Loading action cache from: %s", c.path) |
| 179 | data, err := os.ReadFile(c.path) |
| 180 | if err != nil { |
| 181 | if os.IsNotExist(err) { |
| 182 | // Cache file doesn't exist yet, that's OK |
| 183 | actionCacheLog.Print("Cache file does not exist, starting with empty cache") |
| 184 | return nil |
| 185 | } |
| 186 | actionCacheLog.Printf("Failed to read cache file: %v", err) |
| 187 | return err |
| 188 | } |
| 189 | |
| 190 | if err := json.Unmarshal(data, c); err != nil { |
| 191 | actionCacheLog.Printf("Failed to unmarshal cache data: %v", err) |
| 192 | return err |
| 193 | } |
| 194 | |
| 195 | // Ensure maps are initialized even when absent from the JSON (backward compatibility). |
| 196 | if c.Entries == nil { |
| 197 | c.Entries = make(map[string]ActionCacheEntry) |
| 198 | } |
| 199 | if c.ContainerPins == nil { |
| 200 | c.ContainerPins = make(map[string]ContainerPin) |
| 201 | } |
| 202 | |
| 203 | // Mark cache as clean after successful load (it matches disk state) |
| 204 | c.dirty = false |
| 205 | |
| 206 | actionCacheLog.Printf("Successfully loaded cache with %d entries, %d container pins", len(c.Entries), len(c.ContainerPins)) |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | // Save saves the cache to disk with sorted entries |
| 211 | // If the cache is empty, the file is not created or is deleted if it exists |