This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru.go
170 lines (141 loc) · 3.54 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
package lru
import (
"container/list"
"time"
)
var _ Cache = (*LRUCache)(nil)
// NewLRU returns a new instance of the LRU cache with
// a certain size of elements that can be stored in time.
func NewLRU(size int) (Cache, error) {
if size <= 0 {
return nil, ErrNoPositiveSize
}
c := &LRUCache{
size: size,
list: list.New(),
items: make(map[interface{}]*list.Element),
}
return c, nil
}
// Add is adding a key and value with a TTL to the store.
// Setting the TTL to 0 signales that this key will not expire.
func (l *LRUCache) Add(key interface{}, value interface{}, ttl int64) bool {
if e, ok := l.items[key]; ok {
l.list.MoveToFront(e)
e.Value.(*Item).value = value
e.Value.(*Item).ttl = ttl
e.Value.(*Item).timestamp = time.Now().UnixNano()
}
e := newItem(key, value, ttl)
h := l.list.PushFront(e)
l.items[key] = h
rm := l.list.Len() > l.size
if rm {
l.remove()
}
return rm
}
// Get is returning the value of a provided key.
func (l *LRUCache) Get(key interface{}) (value interface{}, ok bool) {
e, ok := l.items[key]
if !ok {
return
}
if e.Value.(*Item).Expired() {
l.removeElement(e)
return nil, false
}
l.list.MoveToFront(e)
e.Value.(*Item).timestamp = time.Now().UnixNano()
return e.Value.(*Item).value, true
}
// Contains is checking if a provided key exists in the cache
func (l *LRUCache) Contains(key interface{}) (ok bool) {
_, ok = l.items[key]
return ok
}
// Fetch is fetching a value for key that does not exists or has expired.
// The fetching is done by a provided function.
func (l *LRUCache) Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error) {
if e, ok := l.Get(key); ok {
return e, ok, nil
}
v, err := call()
if err != nil {
return nil, false, err
}
e := newItem(key, v, ttl)
h := l.list.PushFront(e)
l.items[key] = h
rm := l.list.Len() > l.size
if rm {
l.remove()
}
return e.Value(), rm, nil
}
// GetOldest returns the oldest item of the cache.
func (l *LRUCache) GetOldest() (key interface{}, value interface{}, ok bool) {
e := l.list.Back()
if e != nil {
kv := e.Value.(*Item)
return kv.key, kv.value, true
}
return nil, nil, false
}
// RemoveOldest removes the oldest item in the cache.
func (l *LRUCache) RemoveOldest() (key interface{}, value interface{}, ok bool) {
e := l.list.Back()
if e != nil {
l.removeElement(e)
kv := e.Value.(*Item)
return kv.key, kv.value, true
}
return nil, nil, false
}
// Remove is removing a provided key from the cache.
func (l *LRUCache) Remove(key interface{}) (ok bool) {
if e, ok := l.items[key]; ok {
l.removeElement(e)
return true
}
return false
}
// Expires returns the time.Time when the provided key will expire.
func (l *LRUCache) Expires(key interface{}) (expires time.Time, ok bool) {
if e, ok := l.items[key]; ok {
return e.Value.(*Item).Expires(), true
}
return
}
// Keys returning the keys of the current cache.
func (l *LRUCache) Keys() []interface{} {
keys := make([]interface{}, len(l.items))
i := 0
for e := l.list.Back(); e != nil; e = e.Prev() {
keys[i] = e.Value.(*Item).key
i++
}
return keys
}
// Len returns the length/number of elements that are in the cache.
func (l *LRUCache) Len() int {
return l.list.Len()
}
// Purge is purging the cache.
func (l *LRUCache) Purge() {
for k := range l.items {
delete(l.items, k)
}
l.list.Init()
}
func (l *LRUCache) remove() {
e := l.list.Back()
if e != nil {
l.removeElement(e)
}
}
func (l *LRUCache) removeElement(e *list.Element) {
l.list.Remove(e)
kv := e.Value.(*Item)
delete(l.items, kv.key)
}