|
| 1 | +package lazycache |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "github.com/hashicorp/golang-lru/simplelru" |
| 7 | +) |
| 8 | + |
| 9 | +var _ = Entry(&delayedEntry{}) |
| 10 | + |
| 11 | +// New creates a new LazyCache. |
| 12 | +func New(options CacheOptions) *LazyCache { |
| 13 | + lru, err := simplelru.NewLRU(int(options.MaxEntries), nil) |
| 14 | + if err != nil { |
| 15 | + panic(err) |
| 16 | + } |
| 17 | + c := &LazyCache{ |
| 18 | + lru: lru, |
| 19 | + } |
| 20 | + return c |
| 21 | +} |
| 22 | + |
| 23 | +type CacheOptions struct { |
| 24 | + // MaxEntries is the maximum number of entries that the cache should hold. |
| 25 | + // Note that this can also be adjusted after the cache is created with Resize. |
| 26 | + MaxEntries int |
| 27 | +} |
| 28 | + |
| 29 | +// Entry is the result of a cache lookup. |
| 30 | +// Any Err value is the error that was returned by the cache prime function. This error value is cached. TODO(bep) consider this. |
| 31 | +type Entry interface { |
| 32 | + Value() any |
| 33 | + Err() error |
| 34 | +} |
| 35 | + |
| 36 | +type LazyCache struct { |
| 37 | + lru *simplelru.LRU |
| 38 | + mu sync.RWMutex |
| 39 | +} |
| 40 | + |
| 41 | +// Contains returns true if the given key is in the cache. |
| 42 | +func (c *LazyCache) Contains(key any) bool { |
| 43 | + c.mu.RLock() |
| 44 | + b := c.lru.Contains(key) |
| 45 | + c.mu.RUnlock() |
| 46 | + return b |
| 47 | +} |
| 48 | + |
| 49 | +// Delete deletes the item with given key from the cache, returning if the |
| 50 | +// key was contained. |
| 51 | +func (c *LazyCache) Delete(key any) bool { |
| 52 | + c.mu.Lock() |
| 53 | + defer c.mu.Unlock() |
| 54 | + return c.lru.Remove(key) |
| 55 | +} |
| 56 | + |
| 57 | +// DeleteFunc deletes all entries for which the given function returns true. |
| 58 | +func (c *LazyCache) DeleteFunc(matches func(key any, item Entry) bool) int { |
| 59 | + c.mu.RLock() |
| 60 | + keys := c.lru.Keys() |
| 61 | + |
| 62 | + var keysToDelete []any |
| 63 | + for _, key := range keys { |
| 64 | + v, _ := c.lru.Peek(key) |
| 65 | + if matches(key, v.(Entry)) { |
| 66 | + keysToDelete = append(keysToDelete, key) |
| 67 | + } |
| 68 | + } |
| 69 | + c.mu.RUnlock() |
| 70 | + |
| 71 | + c.mu.Lock() |
| 72 | + defer c.mu.Unlock() |
| 73 | + var deleteCount int |
| 74 | + for _, key := range keysToDelete { |
| 75 | + if c.lru.Remove(key) { |
| 76 | + deleteCount++ |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return deleteCount |
| 81 | +} |
| 82 | + |
| 83 | +// Keys returns a slice of the keys in the cache, oldest first. |
| 84 | +func (c *LazyCache) Keys() []any { |
| 85 | + c.mu.RLock() |
| 86 | + defer c.mu.RUnlock() |
| 87 | + return c.lru.Keys() |
| 88 | +} |
| 89 | + |
| 90 | +// Len returns the number of items in the cache. |
| 91 | +func (c *LazyCache) Len() int { |
| 92 | + c.mu.RLock() |
| 93 | + defer c.mu.RUnlock() |
| 94 | + return c.lru.Len() |
| 95 | +} |
| 96 | + |
| 97 | +// Get returns the value associated with key. |
| 98 | +func (c *LazyCache) Get(key any) Entry { |
| 99 | + c.mu.Lock() |
| 100 | + v, ok := c.lru.Get(key) |
| 101 | + c.mu.Unlock() |
| 102 | + if !ok { |
| 103 | + return entry{} |
| 104 | + } |
| 105 | + return v.(Entry) |
| 106 | +} |
| 107 | + |
| 108 | +// GetOrCreate returns the value associated with key, or creates it if it doesn't. |
| 109 | +// Note that create, the cache prime function, is called once and then not called again for a given key |
| 110 | +// unless the cache entry is evicted; it does not block other goroutines from calling GetOrCreate, |
| 111 | +// it is not called with the cache lock held. |
| 112 | +func (c *LazyCache) GetOrCreate(key any, create func(key any) (any, error)) Entry { |
| 113 | + c.mu.Lock() |
| 114 | + v, ok := c.lru.Get(key) |
| 115 | + if ok { |
| 116 | + c.mu.Unlock() |
| 117 | + return v.(Entry) |
| 118 | + } |
| 119 | + |
| 120 | + var e = &delayedEntry{ |
| 121 | + done: make(chan struct{}), |
| 122 | + } |
| 123 | + // Add the *delayedEntry early and release the lock. |
| 124 | + // Calllers coming in getting the same cache entry will block on the done channel. |
| 125 | + c.lru.Add(key, e) |
| 126 | + c.mu.Unlock() |
| 127 | + |
| 128 | + // Create the value with the lock released. |
| 129 | + v, err := create(key) |
| 130 | + |
| 131 | + // e is a pointer, and these values will be available to other callers getting this cache entry, |
| 132 | + // once the done channel is closed. |
| 133 | + e.err = err |
| 134 | + e.value = v |
| 135 | + close(e.done) |
| 136 | + |
| 137 | + return e |
| 138 | +} |
| 139 | + |
| 140 | +// Resize changes the cache size and returns the number of entries evicted. |
| 141 | +func (c *LazyCache) Resize(size int) (evicted int) { |
| 142 | + c.mu.Lock() |
| 143 | + evicted = c.lru.Resize(size) |
| 144 | + c.mu.Unlock() |
| 145 | + return evicted |
| 146 | +} |
| 147 | + |
| 148 | +// Set associates value with key. |
| 149 | +func (c *LazyCache) Set(key, value any) { |
| 150 | + c.mu.Lock() |
| 151 | + if _, ok := value.(Entry); !ok { |
| 152 | + value = entry{ |
| 153 | + value: value, |
| 154 | + } |
| 155 | + } |
| 156 | + c.lru.Add(key, value) |
| 157 | + c.mu.Unlock() |
| 158 | +} |
| 159 | + |
| 160 | +// delayedEntry holds a cache value or error that is not available until the done channel is closed. |
| 161 | +type delayedEntry struct { |
| 162 | + done chan struct{} |
| 163 | + value any |
| 164 | + err error |
| 165 | +} |
| 166 | + |
| 167 | +func (r *delayedEntry) Value() any { |
| 168 | + <-r.done |
| 169 | + return r.value |
| 170 | +} |
| 171 | + |
| 172 | +func (r *delayedEntry) Err() error { |
| 173 | + <-r.done |
| 174 | + return r.err |
| 175 | +} |
| 176 | + |
| 177 | +type entry struct { |
| 178 | + value any |
| 179 | + err error |
| 180 | +} |
| 181 | + |
| 182 | +func (r entry) Value() any { |
| 183 | + return r.value |
| 184 | +} |
| 185 | + |
| 186 | +func (r entry) Err() error { |
| 187 | + return r.err |
| 188 | +} |
0 commit comments