lru.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
  2. // All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license that can be
  5. // found in the LICENSE file.
  6. package cache
  7. import (
  8. "sync"
  9. "unsafe"
  10. )
  11. type lruNode struct {
  12. n *Node
  13. h *Handle
  14. ban bool
  15. next, prev *lruNode
  16. }
  17. func (n *lruNode) insert(at *lruNode) {
  18. x := at.next
  19. at.next = n
  20. n.prev = at
  21. n.next = x
  22. x.prev = n
  23. }
  24. func (n *lruNode) remove() {
  25. if n.prev != nil {
  26. n.prev.next = n.next
  27. n.next.prev = n.prev
  28. n.prev = nil
  29. n.next = nil
  30. } else {
  31. panic("BUG: removing removed node")
  32. }
  33. }
  34. type lru struct {
  35. mu sync.Mutex
  36. capacity int
  37. used int
  38. recent lruNode
  39. }
  40. func (r *lru) reset() {
  41. r.recent.next = &r.recent
  42. r.recent.prev = &r.recent
  43. r.used = 0
  44. }
  45. func (r *lru) Capacity() int {
  46. r.mu.Lock()
  47. defer r.mu.Unlock()
  48. return r.capacity
  49. }
  50. func (r *lru) SetCapacity(capacity int) {
  51. var evicted []*lruNode
  52. r.mu.Lock()
  53. r.capacity = capacity
  54. for r.used > r.capacity {
  55. rn := r.recent.prev
  56. if rn == nil {
  57. panic("BUG: invalid LRU used or capacity counter")
  58. }
  59. rn.remove()
  60. rn.n.CacheData = nil
  61. r.used -= rn.n.Size()
  62. evicted = append(evicted, rn)
  63. }
  64. r.mu.Unlock()
  65. for _, rn := range evicted {
  66. rn.h.Release()
  67. }
  68. }
  69. func (r *lru) Promote(n *Node) {
  70. var evicted []*lruNode
  71. r.mu.Lock()
  72. if n.CacheData == nil {
  73. if n.Size() <= r.capacity {
  74. rn := &lruNode{n: n, h: n.GetHandle()}
  75. rn.insert(&r.recent)
  76. n.CacheData = unsafe.Pointer(rn)
  77. r.used += n.Size()
  78. for r.used > r.capacity {
  79. rn := r.recent.prev
  80. if rn == nil {
  81. panic("BUG: invalid LRU used or capacity counter")
  82. }
  83. rn.remove()
  84. rn.n.CacheData = nil
  85. r.used -= rn.n.Size()
  86. evicted = append(evicted, rn)
  87. }
  88. }
  89. } else {
  90. rn := (*lruNode)(n.CacheData)
  91. if !rn.ban {
  92. rn.remove()
  93. rn.insert(&r.recent)
  94. }
  95. }
  96. r.mu.Unlock()
  97. for _, rn := range evicted {
  98. rn.h.Release()
  99. }
  100. }
  101. func (r *lru) Ban(n *Node) {
  102. r.mu.Lock()
  103. if n.CacheData == nil {
  104. n.CacheData = unsafe.Pointer(&lruNode{n: n, ban: true})
  105. } else {
  106. rn := (*lruNode)(n.CacheData)
  107. if !rn.ban {
  108. rn.remove()
  109. rn.ban = true
  110. r.used -= rn.n.Size()
  111. r.mu.Unlock()
  112. rn.h.Release()
  113. rn.h = nil
  114. return
  115. }
  116. }
  117. r.mu.Unlock()
  118. }
  119. func (r *lru) Evict(n *Node) {
  120. r.mu.Lock()
  121. rn := (*lruNode)(n.CacheData)
  122. if rn == nil || rn.ban {
  123. r.mu.Unlock()
  124. return
  125. }
  126. n.CacheData = nil
  127. r.mu.Unlock()
  128. rn.h.Release()
  129. }
  130. func (r *lru) EvictNS(ns uint64) {
  131. var evicted []*lruNode
  132. r.mu.Lock()
  133. for e := r.recent.prev; e != &r.recent; {
  134. rn := e
  135. e = e.prev
  136. if rn.n.NS() == ns {
  137. rn.remove()
  138. rn.n.CacheData = nil
  139. r.used -= rn.n.Size()
  140. evicted = append(evicted, rn)
  141. }
  142. }
  143. r.mu.Unlock()
  144. for _, rn := range evicted {
  145. rn.h.Release()
  146. }
  147. }
  148. func (r *lru) EvictAll() {
  149. r.mu.Lock()
  150. back := r.recent.prev
  151. for rn := back; rn != &r.recent; rn = rn.prev {
  152. rn.n.CacheData = nil
  153. }
  154. r.reset()
  155. r.mu.Unlock()
  156. for rn := back; rn != &r.recent; rn = rn.prev {
  157. rn.h.Release()
  158. }
  159. }
  160. func (r *lru) Close() error {
  161. return nil
  162. }
  163. // NewLRU create a new LRU-cache.
  164. func NewLRU(capacity int) Cacher {
  165. r := &lru{capacity: capacity}
  166. r.reset()
  167. return r
  168. }