Set stores content in cache with LRU eviction
(uri string, content *CachedContent)
| 225 | |
| 226 | // Set stores content in cache with LRU eviction |
| 227 | func (c *ContentCache) Set(uri string, content *CachedContent) { |
| 228 | c.mu.Lock() |
| 229 | defer c.mu.Unlock() |
| 230 | |
| 231 | // Check if already exists |
| 232 | if _, exists := c.cache[uri]; exists { |
| 233 | // If already exists, just update and move to end |
| 234 | for i, u := range c.order { |
| 235 | if u == uri { |
| 236 | // More efficient removal for common case of updating existing entry |
| 237 | copy(c.order[i:], c.order[i+1:]) |
| 238 | c.order = c.order[:len(c.order)-1] |
| 239 | break |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // Add to end (most recently used) |
| 245 | c.order = append(c.order, uri) |
| 246 | |
| 247 | // Evict oldest if over capacity |
| 248 | if len(c.order) > c.maxEntries { |
| 249 | oldest := c.order[0] |
| 250 | delete(c.cache, oldest) |
| 251 | c.order = c.order[1:] |
| 252 | } |
| 253 | |
| 254 | c.cache[uri] = content |
| 255 | } |
no outgoing calls