lru.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package simplelru
  2. import (
  3. "container/list"
  4. "errors"
  5. )
  6. // EvictCallback is used to get a callback when a cache entry is evicted
  7. type EvictCallback func(key interface{}, value interface{})
  8. // LRU implements a non-thread safe fixed size LRU cache
  9. type LRU struct {
  10. size int
  11. evictList *list.List
  12. items map[interface{}]*list.Element
  13. onEvict EvictCallback
  14. }
  15. // entry is used to hold a value in the evictList
  16. type entry struct {
  17. key interface{}
  18. value interface{}
  19. }
  20. // NewLRU constructs an LRU of the given size
  21. func NewLRU(size int, onEvict EvictCallback) (*LRU, error) {
  22. if size <= 0 {
  23. return nil, errors.New("Must provide a positive size")
  24. }
  25. c := &LRU{
  26. size: size,
  27. evictList: list.New(),
  28. items: make(map[interface{}]*list.Element),
  29. onEvict: onEvict,
  30. }
  31. return c, nil
  32. }
  33. // Purge is used to completely clear the cache.
  34. func (c *LRU) Purge() {
  35. for k, v := range c.items {
  36. if c.onEvict != nil {
  37. c.onEvict(k, v.Value.(*entry).value)
  38. }
  39. delete(c.items, k)
  40. }
  41. c.evictList.Init()
  42. }
  43. // Add adds a value to the cache. Returns true if an eviction occurred.
  44. func (c *LRU) Add(key, value interface{}) (evicted bool) {
  45. // Check for existing item
  46. if ent, ok := c.items[key]; ok {
  47. c.evictList.MoveToFront(ent)
  48. ent.Value.(*entry).value = value
  49. return false
  50. }
  51. // Add new item
  52. ent := &entry{key, value}
  53. entry := c.evictList.PushFront(ent)
  54. c.items[key] = entry
  55. evict := c.evictList.Len() > c.size
  56. // Verify size not exceeded
  57. if evict {
  58. c.removeOldest()
  59. }
  60. return evict
  61. }
  62. // Get looks up a key's value from the cache.
  63. func (c *LRU) Get(key interface{}) (value interface{}, ok bool) {
  64. if ent, ok := c.items[key]; ok {
  65. c.evictList.MoveToFront(ent)
  66. if ent.Value.(*entry) == nil {
  67. return nil, false
  68. }
  69. return ent.Value.(*entry).value, true
  70. }
  71. return
  72. }
  73. // Contains checks if a key is in the cache, without updating the recent-ness
  74. // or deleting it for being stale.
  75. func (c *LRU) Contains(key interface{}) (ok bool) {
  76. _, ok = c.items[key]
  77. return ok
  78. }
  79. // Peek returns the key value (or undefined if not found) without updating
  80. // the "recently used"-ness of the key.
  81. func (c *LRU) Peek(key interface{}) (value interface{}, ok bool) {
  82. var ent *list.Element
  83. if ent, ok = c.items[key]; ok {
  84. return ent.Value.(*entry).value, true
  85. }
  86. return nil, ok
  87. }
  88. // Remove removes the provided key from the cache, returning if the
  89. // key was contained.
  90. func (c *LRU) Remove(key interface{}) (present bool) {
  91. if ent, ok := c.items[key]; ok {
  92. c.removeElement(ent)
  93. return true
  94. }
  95. return false
  96. }
  97. // RemoveOldest removes the oldest item from the cache.
  98. func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool) {
  99. ent := c.evictList.Back()
  100. if ent != nil {
  101. c.removeElement(ent)
  102. kv := ent.Value.(*entry)
  103. return kv.key, kv.value, true
  104. }
  105. return nil, nil, false
  106. }
  107. // GetOldest returns the oldest entry
  108. func (c *LRU) GetOldest() (key interface{}, value interface{}, ok bool) {
  109. ent := c.evictList.Back()
  110. if ent != nil {
  111. kv := ent.Value.(*entry)
  112. return kv.key, kv.value, true
  113. }
  114. return nil, nil, false
  115. }
  116. // Keys returns a slice of the keys in the cache, from oldest to newest.
  117. func (c *LRU) Keys() []interface{} {
  118. keys := make([]interface{}, len(c.items))
  119. i := 0
  120. for ent := c.evictList.Back(); ent != nil; ent = ent.Prev() {
  121. keys[i] = ent.Value.(*entry).key
  122. i++
  123. }
  124. return keys
  125. }
  126. // Len returns the number of items in the cache.
  127. func (c *LRU) Len() int {
  128. return c.evictList.Len()
  129. }
  130. // Resize changes the cache size.
  131. func (c *LRU) Resize(size int) (evicted int) {
  132. diff := c.Len() - size
  133. if diff < 0 {
  134. diff = 0
  135. }
  136. for i := 0; i < diff; i++ {
  137. c.removeOldest()
  138. }
  139. c.size = size
  140. return diff
  141. }
  142. // removeOldest removes the oldest item from the cache.
  143. func (c *LRU) removeOldest() {
  144. ent := c.evictList.Back()
  145. if ent != nil {
  146. c.removeElement(ent)
  147. }
  148. }
  149. // removeElement is used to remove a given list element from the cache
  150. func (c *LRU) removeElement(e *list.Element) {
  151. c.evictList.Remove(e)
  152. kv := e.Value.(*entry)
  153. delete(c.items, kv.key)
  154. if c.onEvict != nil {
  155. c.onEvict(kv.key, kv.value)
  156. }
  157. }