lru.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2015 To gocql authors
  3. Copyright 2013 Google Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. // Package lru implements an LRU cache.
  15. package lru
  16. import "container/list"
  17. // Cache is an LRU cache. It is not safe for concurrent access.
  18. //
  19. // This cache has been forked from github.com/golang/groupcache/lru, but
  20. // specialized with string keys to avoid the allocations caused by wrapping them
  21. // in interface{}.
  22. type Cache struct {
  23. // MaxEntries is the maximum number of cache entries before
  24. // an item is evicted. Zero means no limit.
  25. MaxEntries int
  26. // OnEvicted optionally specifies a callback function to be
  27. // executed when an entry is purged from the cache.
  28. OnEvicted func(key string, value interface{})
  29. ll *list.List
  30. cache map[string]*list.Element
  31. }
  32. type entry struct {
  33. key string
  34. value interface{}
  35. }
  36. // New creates a new Cache.
  37. // If maxEntries is zero, the cache has no limit and it's assumed
  38. // that eviction is done by the caller.
  39. func New(maxEntries int) *Cache {
  40. return &Cache{
  41. MaxEntries: maxEntries,
  42. ll: list.New(),
  43. cache: make(map[string]*list.Element),
  44. }
  45. }
  46. // Add adds a value to the cache.
  47. func (c *Cache) Add(key string, value interface{}) {
  48. if c.cache == nil {
  49. c.cache = make(map[string]*list.Element)
  50. c.ll = list.New()
  51. }
  52. if ee, ok := c.cache[key]; ok {
  53. c.ll.MoveToFront(ee)
  54. ee.Value.(*entry).value = value
  55. return
  56. }
  57. ele := c.ll.PushFront(&entry{key, value})
  58. c.cache[key] = ele
  59. if c.MaxEntries != 0 && c.ll.Len() > c.MaxEntries {
  60. c.RemoveOldest()
  61. }
  62. }
  63. // Get looks up a key's value from the cache.
  64. func (c *Cache) Get(key string) (value interface{}, ok bool) {
  65. if c.cache == nil {
  66. return
  67. }
  68. if ele, hit := c.cache[key]; hit {
  69. c.ll.MoveToFront(ele)
  70. return ele.Value.(*entry).value, true
  71. }
  72. return
  73. }
  74. // Remove removes the provided key from the cache.
  75. func (c *Cache) Remove(key string) bool {
  76. if c.cache == nil {
  77. return false
  78. }
  79. if ele, hit := c.cache[key]; hit {
  80. c.removeElement(ele)
  81. return true
  82. }
  83. return false
  84. }
  85. // RemoveOldest removes the oldest item from the cache.
  86. func (c *Cache) RemoveOldest() {
  87. if c.cache == nil {
  88. return
  89. }
  90. ele := c.ll.Back()
  91. if ele != nil {
  92. c.removeElement(ele)
  93. }
  94. }
  95. func (c *Cache) removeElement(e *list.Element) {
  96. c.ll.Remove(e)
  97. kv := e.Value.(*entry)
  98. delete(c.cache, kv.key)
  99. if c.OnEvicted != nil {
  100. c.OnEvicted(kv.key, kv.value)
  101. }
  102. }
  103. // Len returns the number of items in the cache.
  104. func (c *Cache) Len() int {
  105. if c.cache == nil {
  106. return 0
  107. }
  108. return c.ll.Len()
  109. }