No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

162 líneas
3.7 KiB

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