...
1
16 package lru
17
18 import (
19 "sync"
20
21 groupcache "k8s.io/utils/internal/third_party/forked/golang/golang-lru"
22 )
23
24 type Key = groupcache.Key
25 type EvictionFunc = func(key Key, value interface{})
26
27
28 type Cache struct {
29 cache *groupcache.Cache
30 lock sync.RWMutex
31 }
32
33
34 func New(size int) *Cache {
35 return &Cache{
36 cache: groupcache.New(size),
37 }
38 }
39
40
41 func NewWithEvictionFunc(size int, f EvictionFunc) *Cache {
42 c := New(size)
43 c.cache.OnEvicted = f
44 return c
45 }
46
47
48 func (c *Cache) Add(key Key, value interface{}) {
49 c.lock.Lock()
50 defer c.lock.Unlock()
51 c.cache.Add(key, value)
52 }
53
54
55 func (c *Cache) Get(key Key) (value interface{}, ok bool) {
56 c.lock.Lock()
57 defer c.lock.Unlock()
58 return c.cache.Get(key)
59 }
60
61
62 func (c *Cache) Remove(key Key) {
63 c.lock.Lock()
64 defer c.lock.Unlock()
65 c.cache.Remove(key)
66 }
67
68
69 func (c *Cache) RemoveOldest() {
70 c.lock.Lock()
71 defer c.lock.Unlock()
72 c.cache.RemoveOldest()
73 }
74
75
76 func (c *Cache) Len() int {
77 c.lock.RLock()
78 defer c.lock.RUnlock()
79 return c.cache.Len()
80 }
81
82
83 func (c *Cache) Clear() {
84 c.lock.Lock()
85 defer c.lock.Unlock()
86 c.cache.Clear()
87 }
88
View as plain text