Debian Codegraph @

simplelru/lru.go

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