lruexpirecache.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "container/list"
  16. "sync"
  17. "time"
  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. lock sync.Mutex
  32. maxSize int
  33. evictionList list.List
  34. entries map[interface{}]*list.Element
  35. }
  36. // NewLRUExpireCache creates an expiring cache with the given size
  37. func NewLRUExpireCache(maxSize int) *LRUExpireCache {
  38. return NewLRUExpireCacheWithClock(maxSize, realClock{})
  39. }
  40. // NewLRUExpireCacheWithClock creates an expiring cache with the given size, using the specified clock to obtain the current time.
  41. func NewLRUExpireCacheWithClock(maxSize int, clock Clock) *LRUExpireCache {
  42. if maxSize <= 0 {
  43. panic("maxSize must be > 0")
  44. }
  45. return &LRUExpireCache{
  46. clock: clock,
  47. maxSize: maxSize,
  48. entries: map[interface{}]*list.Element{},
  49. }
  50. }
  51. type cacheEntry struct {
  52. key interface{}
  53. value interface{}
  54. expireTime time.Time
  55. }
  56. // Add adds the value to the cache at key with the specified maximum duration.
  57. func (c *LRUExpireCache) Add(key interface{}, value interface{}, ttl time.Duration) {
  58. c.lock.Lock()
  59. defer c.lock.Unlock()
  60. // Key already exists
  61. oldElement, ok := c.entries[key]
  62. if ok {
  63. c.evictionList.MoveToFront(oldElement)
  64. oldElement.Value.(*cacheEntry).value = value
  65. oldElement.Value.(*cacheEntry).expireTime = c.clock.Now().Add(ttl)
  66. return
  67. }
  68. // Make space if necessary
  69. if c.evictionList.Len() >= c.maxSize {
  70. toEvict := c.evictionList.Back()
  71. c.evictionList.Remove(toEvict)
  72. delete(c.entries, toEvict.Value.(*cacheEntry).key)
  73. }
  74. // Add new entry
  75. entry := &cacheEntry{
  76. key: key,
  77. value: value,
  78. expireTime: c.clock.Now().Add(ttl),
  79. }
  80. element := c.evictionList.PushFront(entry)
  81. c.entries[key] = element
  82. }
  83. // Get returns the value at the specified key from the cache if it exists and is not
  84. // expired, or returns false.
  85. func (c *LRUExpireCache) Get(key interface{}) (interface{}, bool) {
  86. c.lock.Lock()
  87. defer c.lock.Unlock()
  88. element, ok := c.entries[key]
  89. if !ok {
  90. return nil, false
  91. }
  92. if c.clock.Now().After(element.Value.(*cacheEntry).expireTime) {
  93. c.evictionList.Remove(element)
  94. delete(c.entries, key)
  95. return nil, false
  96. }
  97. c.evictionList.MoveToFront(element)
  98. return element.Value.(*cacheEntry).value, true
  99. }
  100. // Remove removes the specified key from the cache if it exists
  101. func (c *LRUExpireCache) Remove(key interface{}) {
  102. c.lock.Lock()
  103. defer c.lock.Unlock()
  104. element, ok := c.entries[key]
  105. if !ok {
  106. return
  107. }
  108. c.evictionList.Remove(element)
  109. delete(c.entries, key)
  110. }
  111. // Keys returns all unexpired keys in the cache.
  112. //
  113. // Keep in mind that subsequent calls to Get() for any of the returned keys
  114. // might return "not found".
  115. //
  116. // Keys are returned ordered from least recently used to most recently used.
  117. func (c *LRUExpireCache) Keys() []interface{} {
  118. c.lock.Lock()
  119. defer c.lock.Unlock()
  120. now := c.clock.Now()
  121. val := make([]interface{}, 0, c.evictionList.Len())
  122. for element := c.evictionList.Back(); element != nil; element = element.Prev() {
  123. // Only return unexpired keys
  124. if !now.After(element.Value.(*cacheEntry).expireTime) {
  125. val = append(val, element.Value.(*cacheEntry).key)
  126. }
  127. }
  128. return val
  129. }