| 1 |
/* |
| 2 |
Copyright The containerd 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 |
|
| 17 |
package cacheutil |
| 18 |
|
| 19 |
import ( |
| 20 |
"sync" |
| 21 |
|
| 22 |
"github.com/golang/groupcache/lru" |
| 23 |
) |
| 24 |
|
| 25 |
// LRUCache is "groupcache/lru"-like cache. The difference is that "groupcache/lru" immediately |
| 26 |
// finalizes theevicted contents using OnEvicted callback but our version strictly tracks the |
| 27 |
// reference counts of contents and calls OnEvicted when nobody refers to the evicted contents. |
| 28 |
type LRUCache struct { |
| 29 |
cache *lru.Cache |
| 30 |
mu sync.Mutex |
| 31 |
|
| 32 |
// OnEvicted optionally specifies a callback function to be |
| 33 |
// executed when an entry is purged from the cache. |
| 34 |
OnEvicted func(key string, value interface{}) |
| 35 |
} |
| 36 |
|
| 37 |
// NewLRUCache creates new lru cache. |
| 38 |
func NewLRUCache(maxEntries int) *LRUCache { |
| 39 |
inner := lru.New(maxEntries) |
| 40 |
inner.OnEvicted = func(key lru.Key, value interface{}) { |
| 41 |
// Decrease the ref count incremented in Add(). |
| 42 |
// When nobody refers to this value, this value will be finalized via refCounter. |
| 43 |
value.(*refCounter).finalize() |
| 44 |
} |
| 45 |
return &LRUCache{ |
| 46 |
cache: inner, |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Get retrieves the specified object from the cache and increments the reference counter of the |
| 51 |
// target content. Client must call `done` callback to decrease the reference count when the value |
| 52 |
// will no longer be used. |
| 53 |
func (c *LRUCache) Get(key string) (value interface{}, done func(), ok bool) { |
| 54 |
c.mu.Lock() |
| 55 |
defer c.mu.Unlock() |
| 56 |
o, ok := c.cache.Get(key) |
| 57 |
if !ok { |
| 58 |
return nil, nil, false |
| 59 |
} |
| 60 |
rc := o.(*refCounter) |
| 61 |
rc.inc() |
| 62 |
return rc.v, c.decreaseOnceFunc(rc), true |
| 63 |
} |
| 64 |
|
| 65 |
// Add adds object to the cache and returns the cached contents with incrementing the reference count. |
| 66 |
// If the specified content already exists in the cache, this sets `added` to false and returns |
| 67 |
// "already cached" content (i.e. doesn't replace the content with the new one). Client must call |
| 68 |
// `done` callback to decrease the counter when the value will no longer be used. |
| 69 |
func (c *LRUCache) Add(key string, value interface{}) (cachedValue interface{}, done func(), added bool) { |
| 70 |
c.mu.Lock() |
| 71 |
defer c.mu.Unlock() |
| 72 |
if o, ok := c.cache.Get(key); ok { |
| 73 |
rc := o.(*refCounter) |
| 74 |
rc.inc() |
| 75 |
return rc.v, c.decreaseOnceFunc(rc), false |
| 76 |
} |
| 77 |
rc := &refCounter{ |
| 78 |
key: key, |
| 79 |
v: value, |
| 80 |
onEvicted: c.OnEvicted, |
| 81 |
} |
| 82 |
rc.initialize() // Keep this object having at least 1 ref count (will be decreased in OnEviction) |
| 83 |
rc.inc() // The client references this object (will be decreased on "done") |
| 84 |
c.cache.Add(key, rc) |
| 85 |
return rc.v, c.decreaseOnceFunc(rc), true |
| 86 |
} |
| 87 |
|
| 88 |
// Remove removes the specified contents from the cache. OnEvicted callback will be called when |
| 89 |
// nobody refers to the removed content. |
| 90 |
func (c *LRUCache) Remove(key string) { |
| 91 |
c.mu.Lock() |
| 92 |
defer c.mu.Unlock() |
| 93 |
c.cache.Remove(key) |
| 94 |
} |
| 95 |
|
| 96 |
func (c *LRUCache) decreaseOnceFunc(rc *refCounter) func() { |
| 97 |
var once sync.Once |
| 98 |
return func() { |
| 99 |
c.mu.Lock() |
| 100 |
defer c.mu.Unlock() |
| 101 |
once.Do(func() { rc.dec() }) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
type refCounter struct { |
| 106 |
onEvicted func(key string, value interface{}) |
| 107 |
|
| 108 |
key string |
| 109 |
v interface{} |
| 110 |
refCounts int64 |
| 111 |
|
| 112 |
mu sync.Mutex |
| 113 |
|
| 114 |
initializeOnce sync.Once |
| 115 |
finalizeOnce sync.Once |
| 116 |
} |
| 117 |
|
| 118 |
func (r *refCounter) inc() { |
| 119 |
r.mu.Lock() |
| 120 |
defer r.mu.Unlock() |
| 121 |
r.refCounts++ |
| 122 |
} |
| 123 |
|
| 124 |
func (r *refCounter) dec() { |
| 125 |
r.mu.Lock() |
| 126 |
defer r.mu.Unlock() |
| 127 |
r.refCounts-- |
| 128 |
if r.refCounts <= 0 && r.onEvicted != nil { |
| 129 |
// nobody will refer this object |
| 130 |
r.onEvicted(r.key, r.v) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func (r *refCounter) initialize() { |
| 135 |
r.initializeOnce.Do(func() { r.inc() }) |
| 136 |
} |
| 137 |
|
| 138 |
func (r *refCounter) finalize() { |
| 139 |
r.finalizeOnce.Do(func() { r.dec() }) |
| 140 |
} |