-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlru.go
175 lines (145 loc) · 3.51 KB
/
lru.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package turbo
import (
"container/list"
"sync"
)
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package lru implements an LRU c.
// c is an LRU c. It is not safe for concurrent access.
type cache struct {
sync.RWMutex
// MaxEntries is the maximum number of c entries before
// an item is evicted. Zero means no limit.
MaxEntries int
// OnEvicted optionally specificies a callback function to be
// executed when an entry is purged from the c.
OnEvicted func(key Key, value interface{})
ll *list.List
cache map[interface{}]*list.Element
}
// A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators
type Key interface{}
type entry struct {
key Key
value interface{}
}
// New creates a new c.
// If maxEntries is zero, the c has no limit and it's assumed
// that eviction is done by the caller.
func New(maxEntries int) *cache {
return &cache{
MaxEntries: maxEntries,
ll: list.New(),
cache: make(map[interface{}]*list.Element),
}
}
// Add adds a value to the c.
func (c *cache) Add(key Key, value interface{}) {
c.Lock()
defer c.Unlock()
if c.cache == nil {
c.cache = make(map[interface{}]*list.Element)
c.ll = list.New()
}
if ee, ok := c.cache[key]; ok {
c.ll.MoveToFront(ee)
ee.Value.(*entry).value = value
return
}
ele := c.ll.PushFront(&entry{key, value})
c.cache[key] = ele
if c.MaxEntries != 0 && c.ll.Len() > c.MaxEntries {
c.removeOldest()
}
}
// Get looks up a key's value from the c.
func (c *cache) Get(key Key) (value interface{}, ok bool) {
c.Lock()
defer c.Unlock()
if c.cache == nil {
return
}
if ele, hit := c.cache[key]; hit {
c.ll.MoveToFront(ele)
return ele.Value.(*entry).value, true
}
return
}
// Remove removes the provided key from the c.
func (c *cache) Remove(key Key) interface{} {
c.Lock()
defer c.Unlock()
if c.cache == nil {
return nil
}
if ele, hit := c.cache[key]; hit {
return c.removeElement(ele)
}
return nil
}
// RemoveOldest removes the oldest item from the c.
func (c *cache) removeOldest() interface{} {
if c.cache == nil {
return nil
}
ele := c.ll.Back()
if nil != ele {
return c.removeElement(ele)
}
return nil
}
func (c *cache) removeElement(e *list.Element) interface{} {
c.ll.Remove(e)
kv := e.Value.(*entry)
delete(c.cache, kv.key)
if c.OnEvicted != nil {
c.OnEvicted(kv.key, kv.value)
}
return kv.value
}
// Len returns the number of items in the c.
func (c *cache) Len() int {
c.RLock()
defer c.RUnlock()
if c.cache == nil {
return 0
}
return c.ll.Len()
}
// Clear purges all stored items from the c.
func (c *cache) Clear() {
c.Lock()
defer c.Unlock()
if c.OnEvicted != nil {
for _, e := range c.cache {
kv := e.Value.(*entry)
c.OnEvicted(kv.key, kv.value)
}
}
c.ll = nil
c.cache = nil
}
//iterator
func (c *cache) Iterator(do func(k, v interface{}) error) {
c.RLock()
defer c.RUnlock()
for _, e := range c.cache {
kv := e.Value.(*entry)
err := do(kv.key, kv.value)
//err break finish iterator
if nil != err {
break
}
}
}