-
Notifications
You must be signed in to change notification settings - Fork 11
/
lru.go
177 lines (156 loc) · 5.33 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
176
177
package mcache
import (
"github.com/songangweb/mcache/simplelru"
"sync"
)
// LruCache is a thread-safe fixed size LRU cache.
// LruCache 实现一个给定大小的LRU缓存
type LruCache struct {
lru simplelru.LRUCache
lock sync.RWMutex
}
// NewLRU creates an LRU of the given size.
// NewLRU 构造一个给定大小的LRU
func NewLRU(size int) (*LruCache, error) {
return NewLruWithEvict(size, nil)
}
// NewLruWithEvict constructs a fixed size cache with the given eviction
// callback.
// NewLruWithEvict 用于在缓存条目被淘汰时的回调函数
func NewLruWithEvict(size int, onEvicted func(key interface{}, value interface{}, expirationTime int64)) (*LruCache, error) {
lru, err := simplelru.NewLRU(size, simplelru.EvictCallback(onEvicted))
if err != nil {
return nil, err
}
c := &LruCache{
lru: lru,
}
return c, nil
}
// Purge is used to completely clear the cache.
// Purge 清除所有缓存项
func (c *LruCache) Purge() {
c.lock.Lock()
c.lru.Purge()
c.lock.Unlock()
}
// PurgeOverdue is used to completely clear the overdue cache.
// PurgeOverdue 用于清除过期缓存。
func (c *LruCache) PurgeOverdue() {
c.lock.Lock()
c.lru.PurgeOverdue()
c.lock.Unlock()
}
// Add adds a value to the cache. Returns true if an eviction occurred.
// Add 向缓存添加一个值。如果已经存在,则更新信息
func (c *LruCache) Add(key, value interface{}, expirationTime int64) (evicted bool) {
c.lock.Lock()
evicted = c.lru.Add(key, value, expirationTime)
c.lock.Unlock()
return evicted
}
// Get looks up a key's value from the cache.
// Get 从缓存中查找一个键的值。
func (c *LruCache) Get(key interface{}) (value interface{}, expirationTime int64, ok bool) {
c.lock.Lock()
value, expirationTime, ok = c.lru.Get(key)
c.lock.Unlock()
return value, expirationTime, ok
}
// Contains checks if a key is in the cache, without updating the
// recent-ness or deleting it for being stale.
// Contains 检查某个键是否在缓存中,但不更新缓存的状态
func (c *LruCache) Contains(key interface{}) bool {
c.lock.RLock()
containKey := c.lru.Contains(key)
c.lock.RUnlock()
return containKey
}
// Peek returns the key value (or undefined if not found) without updating
// the "recently used"-ness of the key.
// Peek 在不更新的情况下返回键值(如果没有找到则返回false),不更新缓存的状态
func (c *LruCache) Peek(key interface{}) (value interface{}, expirationTime int64, ok bool) {
c.lock.RLock()
value, expirationTime, ok = c.lru.Peek(key)
c.lock.RUnlock()
return value, expirationTime, ok
}
// ContainsOrAdd checks if a key is in the cache without updating the
// recent-ness or deleting it for being stale, and if not, adds the value.
// Returns whether found and whether an eviction occurred.
// ContainsOrAdd 检查键是否在缓存中,而不更新
// 最近或删除它,因为它是陈旧的,如果不是,添加值。
// 返回是否找到和是否发生了驱逐。
func (c *LruCache) ContainsOrAdd(key, value interface{}, expirationTime int64) (ok, evicted bool) {
c.lock.Lock()
defer c.lock.Unlock()
if c.lru.Contains(key) {
return true, false
}
evicted = c.lru.Add(key, value, expirationTime)
return false, evicted
}
// PeekOrAdd checks if a key is in the cache without updating the
// recent-ness or deleting it for being stale, and if not, adds the value.
// Returns whether found and whether an eviction occurred.
// PeekOrAdd 如果一个key在缓存中,那么这个key就不会被更新
// 最近或删除它,因为它是陈旧的,如果不是,添加值。
// 返回是否找到和是否发生了驱逐。
func (c *LruCache) PeekOrAdd(key, value interface{}, expirationTime int64) (previous interface{}, ok, evicted bool) {
c.lock.Lock()
defer c.lock.Unlock()
previous, expirationTime, ok = c.lru.Peek(key)
if ok {
return previous, true, false
}
evicted = c.lru.Add(key, value, expirationTime)
return nil, false, evicted
}
// Remove removes the provided key from the cache.
// Remove 从缓存中移除提供的键。
func (c *LruCache) Remove(key interface{}) (present bool) {
c.lock.Lock()
present = c.lru.Remove(key)
c.lock.Unlock()
return
}
// Resize changes the cache size.
// Resize 调整缓存大小,返回调整前的数量
func (c *LruCache) Resize(size int) (evicted int) {
c.lock.Lock()
evicted = c.lru.Resize(size)
c.lock.Unlock()
return evicted
}
// RemoveOldest removes the oldest item from the cache.
// RemoveOldest 从缓存中移除最老的项
func (c *LruCache) RemoveOldest() (key interface{}, value interface{}, expirationTime int64, ok bool) {
c.lock.Lock()
key, value, expirationTime, ok = c.lru.RemoveOldest()
c.lock.Unlock()
return
}
// GetOldest returns the oldest entry
// GetOldest 从缓存中返回最旧的条目
func (c *LruCache) GetOldest() (key interface{}, value interface{}, expirationTime int64, ok bool) {
c.lock.Lock()
key, value, expirationTime, ok = c.lru.GetOldest()
c.lock.Unlock()
return
}
// Keys returns a slice of the keys in the cache, from oldest to newest.
// Keys 返回缓存中键的切片,从最老到最新
func (c *LruCache) Keys() []interface{} {
c.lock.RLock()
keys := c.lru.Keys()
c.lock.RUnlock()
return keys
}
// Len returns the number of items in the cache.
// Len 获取缓存已存在的缓存条数
func (c *LruCache) Len() int {
c.lock.RLock()
length := c.lru.Len()
c.lock.RUnlock()
return length
}