1/* 2Copyright 2016 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package cache 18 19import ( 20 "sync" 21 "time" 22 23 "github.com/hashicorp/golang-lru" 24) 25 26// Clock defines an interface for obtaining the current time 27type Clock interface { 28 Now() time.Time 29} 30 31// realClock implements the Clock interface by calling time.Now() 32type realClock struct{} 33 34func (realClock) Now() time.Time { return time.Now() } 35 36// LRUExpireCache is a cache that ensures the mostly recently accessed keys are returned with 37// a ttl beyond which keys are forcibly expired. 38type LRUExpireCache struct { 39 // clock is used to obtain the current time 40 clock Clock 41 42 cache *lru.Cache 43 lock sync.Mutex 44} 45 46// NewLRUExpireCache creates an expiring cache with the given size 47func NewLRUExpireCache(maxSize int) *LRUExpireCache { 48 return NewLRUExpireCacheWithClock(maxSize, realClock{}) 49} 50 51// NewLRUExpireCacheWithClock creates an expiring cache with the given size, using the specified clock to obtain the current time. 52func NewLRUExpireCacheWithClock(maxSize int, clock Clock) *LRUExpireCache { 53 cache, err := lru.New(maxSize) 54 if err != nil { 55 // if called with an invalid size 56 panic(err) 57 } 58 return &LRUExpireCache{clock: clock, cache: cache} 59} 60 61type cacheEntry struct { 62 value interface{} 63 expireTime time.Time 64} 65 66// Add adds the value to the cache at key with the specified maximum duration. 67func (c *LRUExpireCache) Add(key interface{}, value interface{}, ttl time.Duration) { 68 c.lock.Lock() 69 defer c.lock.Unlock() 70 c.cache.Add(key, &cacheEntry{value, c.clock.Now().Add(ttl)}) 71} 72 73// Get returns the value at the specified key from the cache if it exists and is not 74// expired, or returns false. 75func (c *LRUExpireCache) Get(key interface{}) (interface{}, bool) { 76 c.lock.Lock() 77 defer c.lock.Unlock() 78 e, ok := c.cache.Get(key) 79 if !ok { 80 return nil, false 81 } 82 if c.clock.Now().After(e.(*cacheEntry).expireTime) { 83 c.cache.Remove(key) 84 return nil, false 85 } 86 return e.(*cacheEntry).value, true 87} 88 89// Remove removes the specified key from the cache if it exists 90func (c *LRUExpireCache) Remove(key interface{}) { 91 c.lock.Lock() 92 defer c.lock.Unlock() 93 c.cache.Remove(key) 94} 95 96// Keys returns all the keys in the cache, even if they are expired. Subsequent calls to 97// get may return not found. It returns all keys from oldest to newest. 98func (c *LRUExpireCache) Keys() []interface{} { 99 c.lock.Lock() 100 defer c.lock.Unlock() 101 return c.cache.Keys() 102} 103