expiring.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Copyright 2019 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/heap"
  16. "sync"
  17. "time"
  18. utilclock "k8s.io/apimachinery/pkg/util/clock"
  19. )
  20. // NewExpiring returns an initialized expiring cache.
  21. func NewExpiring() *Expiring {
  22. return NewExpiringWithClock(utilclock.RealClock{})
  23. }
  24. // NewExpiringWithClock is like NewExpiring but allows passing in a custom
  25. // clock for testing.
  26. func NewExpiringWithClock(clock utilclock.Clock) *Expiring {
  27. return &Expiring{
  28. clock: clock,
  29. cache: make(map[interface{}]entry),
  30. }
  31. }
  32. // Expiring is a map whose entries expire after a per-entry timeout.
  33. type Expiring struct {
  34. clock utilclock.Clock
  35. // mu protects the below fields
  36. mu sync.RWMutex
  37. // cache is the internal map that backs the cache.
  38. cache map[interface{}]entry
  39. // generation is used as a cheap resource version for cache entries. Cleanups
  40. // are scheduled with a key and generation. When the cleanup runs, it first
  41. // compares its generation with the current generation of the entry. It
  42. // deletes the entry iff the generation matches. This prevents cleanups
  43. // scheduled for earlier versions of an entry from deleting later versions of
  44. // an entry when Set() is called multiple times with the same key.
  45. //
  46. // The integer value of the generation of an entry is meaningless.
  47. generation uint64
  48. heap expiringHeap
  49. }
  50. type entry struct {
  51. val interface{}
  52. expiry time.Time
  53. generation uint64
  54. }
  55. // Get looks up an entry in the cache.
  56. func (c *Expiring) Get(key interface{}) (val interface{}, ok bool) {
  57. c.mu.RLock()
  58. defer c.mu.RUnlock()
  59. e, ok := c.cache[key]
  60. if !ok || !c.clock.Now().Before(e.expiry) {
  61. return nil, false
  62. }
  63. return e.val, true
  64. }
  65. // Set sets a key/value/expiry entry in the map, overwriting any previous entry
  66. // with the same key. The entry expires at the given expiry time, but its TTL
  67. // may be lengthened or shortened by additional calls to Set(). Garbage
  68. // collection of expired entries occurs during calls to Set(), however calls to
  69. // Get() will not return expired entries that have not yet been garbage
  70. // collected.
  71. func (c *Expiring) Set(key interface{}, val interface{}, ttl time.Duration) {
  72. now := c.clock.Now()
  73. expiry := now.Add(ttl)
  74. c.mu.Lock()
  75. defer c.mu.Unlock()
  76. c.generation++
  77. c.cache[key] = entry{
  78. val: val,
  79. expiry: expiry,
  80. generation: c.generation,
  81. }
  82. // Run GC inline before pushing the new entry.
  83. c.gc(now)
  84. heap.Push(&c.heap, &expiringHeapEntry{
  85. key: key,
  86. expiry: expiry,
  87. generation: c.generation,
  88. })
  89. }
  90. // Delete deletes an entry in the map.
  91. func (c *Expiring) Delete(key interface{}) {
  92. c.mu.Lock()
  93. defer c.mu.Unlock()
  94. c.del(key, 0)
  95. }
  96. // del deletes the entry for the given key. The generation argument is the
  97. // generation of the entry that should be deleted. If the generation has been
  98. // changed (e.g. if a set has occurred on an existing element but the old
  99. // cleanup still runs), this is a noop. If the generation argument is 0, the
  100. // entry's generation is ignored and the entry is deleted.
  101. //
  102. // del must be called under the write lock.
  103. func (c *Expiring) del(key interface{}, generation uint64) {
  104. e, ok := c.cache[key]
  105. if !ok {
  106. return
  107. }
  108. if generation != 0 && generation != e.generation {
  109. return
  110. }
  111. delete(c.cache, key)
  112. }
  113. // Len returns the number of items in the cache.
  114. func (c *Expiring) Len() int {
  115. c.mu.RLock()
  116. defer c.mu.RUnlock()
  117. return len(c.cache)
  118. }
  119. func (c *Expiring) gc(now time.Time) {
  120. for {
  121. // Return from gc if the heap is empty or the next element is not yet
  122. // expired.
  123. //
  124. // heap[0] is a peek at the next element in the heap, which is not obvious
  125. // from looking at the (*expiringHeap).Pop() implementation below.
  126. // heap.Pop() swaps the first entry with the last entry of the heap, then
  127. // calls (*expiringHeap).Pop() which returns the last element.
  128. if len(c.heap) == 0 || now.Before(c.heap[0].expiry) {
  129. return
  130. }
  131. cleanup := heap.Pop(&c.heap).(*expiringHeapEntry)
  132. c.del(cleanup.key, cleanup.generation)
  133. }
  134. }
  135. type expiringHeapEntry struct {
  136. key interface{}
  137. expiry time.Time
  138. generation uint64
  139. }
  140. // expiringHeap is a min-heap ordered by expiration time of its entries. The
  141. // expiring cache uses this as a priority queue to efficiently organize entries
  142. // which will be garbage collected once they expire.
  143. type expiringHeap []*expiringHeapEntry
  144. var _ heap.Interface = &expiringHeap{}
  145. func (cq expiringHeap) Len() int {
  146. return len(cq)
  147. }
  148. func (cq expiringHeap) Less(i, j int) bool {
  149. return cq[i].expiry.Before(cq[j].expiry)
  150. }
  151. func (cq expiringHeap) Swap(i, j int) {
  152. cq[i], cq[j] = cq[j], cq[i]
  153. }
  154. func (cq *expiringHeap) Push(c interface{}) {
  155. *cq = append(*cq, c.(*expiringHeapEntry))
  156. }
  157. func (cq *expiringHeap) Pop() interface{} {
  158. c := (*cq)[cq.Len()-1]
  159. *cq = (*cq)[:cq.Len()-1]
  160. return c
  161. }