Add adds the provided key and value to the cache, evicting an old item if necessary.
(key string, value interface{})
| 59 | // Add adds the provided key and value to the cache, evicting |
| 60 | // an old item if necessary. |
| 61 | func (c *Cache) Add(key string, value interface{}) { |
| 62 | if !c.nolock { |
| 63 | c.mu.Lock() |
| 64 | defer c.mu.Unlock() |
| 65 | } |
| 66 | |
| 67 | // Already in cache? |
| 68 | if ee, ok := c.cache[key]; ok { |
| 69 | c.ll.MoveToFront(ee) |
| 70 | ee.Value.(*entry).value = value |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | // Add to cache if not present |
| 75 | ele := c.ll.PushFront(&entry{key, value}) |
| 76 | c.cache[key] = ele |
| 77 | |
| 78 | if c.maxEntries > 0 && c.ll.Len() > c.maxEntries { |
| 79 | c.removeOldest() |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Get fetches the key's value from the cache. |
| 84 | // The ok result will be true if the item was found. |