lruexpirecache.go 2.9 KB

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