| 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 |
"time" |
| 22 |
) |
| 23 |
|
| 24 |
// TTLCache is a ttl-based cache with reference counters. |
| 25 |
// Each elements is deleted as soon as expiering the configured ttl. |
| 26 |
type TTLCache struct { |
| 27 |
m map[string]*refCounterWithTimer |
| 28 |
mu sync.Mutex |
| 29 |
ttl time.Duration |
| 30 |
|
| 31 |
// OnEvicted optionally specifies a callback function to be |
| 32 |
// executed when an entry is purged from the cache. |
| 33 |
OnEvicted func(key string, value interface{}) |
| 34 |
} |
| 35 |
|
| 36 |
// NewTTLCache creates a new ttl-based cache. |
| 37 |
func NewTTLCache(ttl time.Duration) *TTLCache { |
| 38 |
return &TTLCache{ |
| 39 |
m: make(map[string]*refCounterWithTimer), |
| 40 |
ttl: ttl, |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Get retrieves the specified object from the cache and increments the reference counter of the |
| 45 |
// target content. Client must call `done` callback to decrease the reference count when the value |
| 46 |
// will no longer be used. |
| 47 |
func (c *TTLCache) Get(key string) (value interface{}, done func(), ok bool) { |
| 48 |
c.mu.Lock() |
| 49 |
defer c.mu.Unlock() |
| 50 |
rc, ok := c.m[key] |
| 51 |
if !ok { |
| 52 |
return nil, nil, false |
| 53 |
} |
| 54 |
rc.inc() |
| 55 |
return rc.v, c.decreaseOnceFunc(rc), true |
| 56 |
} |
| 57 |
|
| 58 |
// Add adds object to the cache and returns the cached contents with incrementing the reference count. |
| 59 |
// If the specified content already exists in the cache, this sets `added` to false and returns |
| 60 |
// "already cached" content (i.e. doesn't replace the content with the new one). Client must call |
| 61 |
// `done` callback to decrease the counter when the value will no longer be used. |
| 62 |
func (c *TTLCache) Add(key string, value interface{}) (cachedValue interface{}, done func(), added bool) { |
| 63 |
c.mu.Lock() |
| 64 |
defer c.mu.Unlock() |
| 65 |
if rc, ok := c.m[key]; ok { |
| 66 |
rc.inc() |
| 67 |
return rc.v, c.decreaseOnceFunc(rc), false |
| 68 |
} |
| 69 |
rc := &refCounterWithTimer{ |
| 70 |
refCounter: &refCounter{ |
| 71 |
key: key, |
| 72 |
v: value, |
| 73 |
onEvicted: c.OnEvicted, |
| 74 |
}, |
| 75 |
} |
| 76 |
rc.initialize() // Keep this object having at least 1 ref count (will be decreased in OnEviction) |
| 77 |
rc.inc() // The client references this object (will be decreased on "done") |
| 78 |
rc.t = time.AfterFunc(c.ttl, func() { |
| 79 |
c.mu.Lock() |
| 80 |
defer c.mu.Unlock() |
| 81 |
c.evictLocked(key) |
| 82 |
}) |
| 83 |
c.m[key] = rc |
| 84 |
return rc.v, c.decreaseOnceFunc(rc), true |
| 85 |
} |
| 86 |
|
| 87 |
// Remove removes the specified contents from the cache. OnEvicted callback will be called when |
| 88 |
// nobody refers to the removed content. |
| 89 |
func (c *TTLCache) Remove(key string) { |
| 90 |
c.mu.Lock() |
| 91 |
defer c.mu.Unlock() |
| 92 |
c.evictLocked(key) |
| 93 |
} |
| 94 |
|
| 95 |
func (c *TTLCache) evictLocked(key string) { |
| 96 |
if rc, ok := c.m[key]; ok { |
| 97 |
delete(c.m, key) |
| 98 |
rc.t.Stop() // stop timer to prevent GC to this content anymore |
| 99 |
rc.finalize() |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func (c *TTLCache) decreaseOnceFunc(rc *refCounterWithTimer) func() { |
| 104 |
var once sync.Once |
| 105 |
return func() { |
| 106 |
c.mu.Lock() |
| 107 |
defer c.mu.Unlock() |
| 108 |
once.Do(func() { rc.dec() }) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
type refCounterWithTimer struct { |
| 113 |
*refCounter |
| 114 |
t *time.Timer |
| 115 |
} |