lru.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 occured.
  44. func (c *LRU) Add(key, value interface{}) 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. return ent.Value.(*entry).value, true
  67. }
  68. return
  69. }
  70. // Check if a key is in the cache, without updating the recent-ness
  71. // or deleting it for being stale.
  72. func (c *LRU) Contains(key interface{}) (ok bool) {
  73. _, ok = c.items[key]
  74. return ok
  75. }
  76. // Returns the key value (or undefined if not found) without updating
  77. // the "recently used"-ness of the key.
  78. func (c *LRU) Peek(key interface{}) (value interface{}, ok bool) {
  79. if ent, ok := c.items[key]; ok {
  80. return ent.Value.(*entry).value, true
  81. }
  82. return nil, ok
  83. }
  84. // Remove removes the provided key from the cache, returning if the
  85. // key was contained.
  86. func (c *LRU) Remove(key interface{}) bool {
  87. if ent, ok := c.items[key]; ok {
  88. c.removeElement(ent)
  89. return true
  90. }
  91. return false
  92. }
  93. // RemoveOldest removes the oldest item from the cache.
  94. func (c *LRU) RemoveOldest() (interface{}, interface{}, bool) {
  95. ent := c.evictList.Back()
  96. if ent != nil {
  97. c.removeElement(ent)
  98. kv := ent.Value.(*entry)
  99. return kv.key, kv.value, true
  100. }
  101. return nil, nil, false
  102. }
  103. // GetOldest returns the oldest entry
  104. func (c *LRU) GetOldest() (interface{}, interface{}, bool) {
  105. ent := c.evictList.Back()
  106. if ent != nil {
  107. kv := ent.Value.(*entry)
  108. return kv.key, kv.value, true
  109. }
  110. return nil, nil, false
  111. }
  112. // Keys returns a slice of the keys in the cache, from oldest to newest.
  113. func (c *LRU) Keys() []interface{} {
  114. keys := make([]interface{}, len(c.items))
  115. i := 0
  116. for ent := c.evictList.Back(); ent != nil; ent = ent.Prev() {
  117. keys[i] = ent.Value.(*entry).key
  118. i++
  119. }
  120. return keys
  121. }
  122. // Len returns the number of items in the cache.
  123. func (c *LRU) Len() int {
  124. return c.evictList.Len()
  125. }
  126. // removeOldest removes the oldest item from the cache.
  127. func (c *LRU) removeOldest() {
  128. ent := c.evictList.Back()
  129. if ent != nil {
  130. c.removeElement(ent)
  131. }
  132. }
  133. // removeElement is used to remove a given list element from the cache
  134. func (c *LRU) removeElement(e *list.Element) {
  135. c.evictList.Remove(e)
  136. kv := e.Value.(*entry)
  137. delete(c.items, kv.key)
  138. if c.onEvict != nil {
  139. c.onEvict(kv.key, kv.value)
  140. }
  141. }