lru.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2013 Google Inc.
  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 lru implements an LRU cache.
  14. package lru
  15. import "container/list"
  16. // Cache is an LRU cache. It is not safe for concurrent access.
  17. type Cache struct {
  18. // MaxEntries is the maximum number of cache entries before
  19. // an item is evicted. Zero means no limit.
  20. MaxEntries int
  21. // OnEvicted optionally specificies a callback function to be
  22. // executed when an entry is purged from the cache.
  23. OnEvicted func(key Key, value interface{})
  24. ll *list.List
  25. cache map[interface{}]*list.Element
  26. }
  27. // A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators
  28. type Key interface{}
  29. type entry struct {
  30. key Key
  31. value interface{}
  32. }
  33. // New creates a new Cache.
  34. // If maxEntries is zero, the cache has no limit and it's assumed
  35. // that eviction is done by the caller.
  36. func New(maxEntries int) *Cache {
  37. return &Cache{
  38. MaxEntries: maxEntries,
  39. ll: list.New(),
  40. cache: make(map[interface{}]*list.Element),
  41. }
  42. }
  43. // Add adds a value to the cache.
  44. func (c *Cache) Add(key Key, value interface{}) {
  45. if c.cache == nil {
  46. c.cache = make(map[interface{}]*list.Element)
  47. c.ll = list.New()
  48. }
  49. if ee, ok := c.cache[key]; ok {
  50. c.ll.MoveToFront(ee)
  51. ee.Value.(*entry).value = value
  52. return
  53. }
  54. ele := c.ll.PushFront(&entry{key, value})
  55. c.cache[key] = ele
  56. if c.MaxEntries != 0 && c.ll.Len() > c.MaxEntries {
  57. c.RemoveOldest()
  58. }
  59. }
  60. // Get looks up a key's value from the cache.
  61. func (c *Cache) Get(key Key) (value interface{}, ok bool) {
  62. if c.cache == nil {
  63. return
  64. }
  65. if ele, hit := c.cache[key]; hit {
  66. c.ll.MoveToFront(ele)
  67. return ele.Value.(*entry).value, true
  68. }
  69. return
  70. }
  71. // Remove removes the provided key from the cache.
  72. func (c *Cache) Remove(key Key) {
  73. if c.cache == nil {
  74. return
  75. }
  76. if ele, hit := c.cache[key]; hit {
  77. c.removeElement(ele)
  78. }
  79. }
  80. // RemoveOldest removes the oldest item from the cache.
  81. func (c *Cache) RemoveOldest() {
  82. if c.cache == nil {
  83. return
  84. }
  85. ele := c.ll.Back()
  86. if ele != nil {
  87. c.removeElement(ele)
  88. }
  89. }
  90. func (c *Cache) removeElement(e *list.Element) {
  91. c.ll.Remove(e)
  92. kv := e.Value.(*entry)
  93. delete(c.cache, kv.key)
  94. if c.OnEvicted != nil {
  95. c.OnEvicted(kv.key, kv.value)
  96. }
  97. }
  98. // Len returns the number of items in the cache.
  99. func (c *Cache) Len() int {
  100. if c.cache == nil {
  101. return 0
  102. }
  103. return c.ll.Len()
  104. }