| 1 |
/* |
| 2 |
Copyright 2021 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
package lru |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"sync" |
| 21 |
|
| 22 |
groupcache "k8s.io/utils/internal/third_party/forked/golang/golang-lru" |
| 23 |
) |
| 24 |
|
| 25 |
type Key = groupcache.Key |
| 26 |
type EvictionFunc = func(key Key, value any) |
| 27 |
|
| 28 |
// Cache is a thread-safe fixed size LRU cache. |
| 29 |
type Cache struct { |
| 30 |
cache *groupcache.Cache |
| 31 |
lock sync.RWMutex |
| 32 |
} |
| 33 |
|
| 34 |
// New creates an LRU of the given size. |
| 35 |
func New(size int) *Cache { |
| 36 |
return &Cache{ |
| 37 |
cache: groupcache.New(size), |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// NewWithEvictionFunc creates an LRU of the given size with the given eviction func. |
| 42 |
func NewWithEvictionFunc(size int, f EvictionFunc) *Cache { |
| 43 |
c := New(size) |
| 44 |
c.cache.OnEvicted = f |
| 45 |
return c |
| 46 |
} |
| 47 |
|
| 48 |
// SetEvictionFunc updates the eviction func |
| 49 |
func (c *Cache) SetEvictionFunc(f EvictionFunc) error { |
| 50 |
c.lock.Lock() |
| 51 |
defer c.lock.Unlock() |
| 52 |
if c.cache.OnEvicted != nil { |
| 53 |
return fmt.Errorf("lru cache eviction function is already set") |
| 54 |
} |
| 55 |
c.cache.OnEvicted = f |
| 56 |
return nil |
| 57 |
} |
| 58 |
|
| 59 |
// Add adds a value to the cache. |
| 60 |
func (c *Cache) Add(key Key, value any) { |
| 61 |
c.lock.Lock() |
| 62 |
defer c.lock.Unlock() |
| 63 |
c.cache.Add(key, value) |
| 64 |
} |
| 65 |
|
| 66 |
// Get looks up a key's value from the cache. |
| 67 |
func (c *Cache) Get(key Key) (value any, ok bool) { |
| 68 |
c.lock.Lock() |
| 69 |
defer c.lock.Unlock() |
| 70 |
return c.cache.Get(key) |
| 71 |
} |
| 72 |
|
| 73 |
// Remove removes the provided key from the cache. |
| 74 |
func (c *Cache) Remove(key Key) { |
| 75 |
c.lock.Lock() |
| 76 |
defer c.lock.Unlock() |
| 77 |
c.cache.Remove(key) |
| 78 |
} |
| 79 |
|
| 80 |
// RemoveOldest removes the oldest item from the cache. |
| 81 |
func (c *Cache) RemoveOldest() { |
| 82 |
c.lock.Lock() |
| 83 |
defer c.lock.Unlock() |
| 84 |
c.cache.RemoveOldest() |
| 85 |
} |
| 86 |
|
| 87 |
// Len returns the number of items in the cache. |
| 88 |
func (c *Cache) Len() int { |
| 89 |
c.lock.RLock() |
| 90 |
defer c.lock.RUnlock() |
| 91 |
return c.cache.Len() |
| 92 |
} |
| 93 |
|
| 94 |
// Clear purges all stored items from the cache. |
| 95 |
func (c *Cache) Clear() { |
| 96 |
c.lock.Lock() |
| 97 |
defer c.lock.Unlock() |
| 98 |
c.cache.Clear() |
| 99 |
} |