Get gets a value named key either from the cache or creates it afresh with the create function.
(key string, create CreateFunc)
| 81 | // Get gets a value named key either from the cache or creates it |
| 82 | // afresh with the create function. |
| 83 | func (c *Cache) Get(key string, create CreateFunc) (value any, err error) { |
| 84 | c.mu.Lock() |
| 85 | entry, ok := c.cache[key] |
| 86 | if !ok { |
| 87 | c.mu.Unlock() // Unlock in case Get is called recursively |
| 88 | value, ok, err = create(key) |
| 89 | if err != nil && !ok { |
| 90 | return value, err |
| 91 | } |
| 92 | entry = &cacheEntry{ |
| 93 | value: value, |
| 94 | key: key, |
| 95 | err: err, |
| 96 | } |
| 97 | c.mu.Lock() |
| 98 | if !c.noCache() { |
| 99 | c.cache[key] = entry |
| 100 | } |
| 101 | } |
| 102 | defer c.mu.Unlock() |
| 103 | c.used(entry) |
| 104 | return entry.value, entry.err |
| 105 | } |
| 106 | |
| 107 | func (c *Cache) addPin(key string, count int) { |
| 108 | c.mu.Lock() |