-
Notifications
You must be signed in to change notification settings - Fork 0
/
excache.go
183 lines (143 loc) · 3.36 KB
/
excache.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
178
179
180
181
182
183
package excache
import (
"container/heap"
"fmt"
"sync"
"time"
)
// Cache represents safe for concurrent use passive expiring cache.
// Passively expires old records.
// Uses heap.Interface under the hood.
type Cache struct {
m *sync.Mutex
capacity int
expireQueue expireQueue
cache map[string]*record
}
// New returns an initialized cache instance
func New(capacity int) (*Cache, error) {
if capacity <= 0 {
return nil, fmt.Errorf("capacity can't be negative")
}
return &Cache{
m: &sync.Mutex{},
capacity: capacity,
expireQueue: make(expireQueue, 0, capacity),
cache: make(map[string]*record, capacity),
}, nil
}
// Get returns (value, true) or (nil, false) for a given key.
// Resets TTL.
func (c *Cache) Get(key string) (interface{}, bool) {
c.m.Lock()
defer c.m.Unlock()
c.expire()
e, ok := c.cache[key]
if !ok {
return nil, false
}
c.expireQueue.update(e, e.value, e.ttl, time.Now().Add(e.ttl).UnixNano())
return e.value, true
}
// Put inserts new record into the cache
func (c *Cache) Put(key string, value interface{}, ttl time.Duration) {
c.m.Lock()
defer c.m.Unlock()
c.expire()
if len(c.cache) >= c.capacity {
if c.expireQueue[0].key != key {
r := c.expireQueue.Pop().(*record)
delete(c.cache, r.key)
}
}
r, ok := c.cache[key]
if !ok {
r = &record{
key: key,
value: value,
ttl: ttl,
expireTimeStamp: time.Now().Add(ttl).UnixNano(),
}
heap.Push(&c.expireQueue, r)
c.cache[key] = r
return
}
c.expireQueue.update(r, value, ttl, time.Now().Add(ttl).UnixNano())
}
// Delete removes the record associated with the specified key from the cache
func (c *Cache) Delete(key string) {
c.m.Lock()
defer c.m.Unlock()
c.expire()
r, ok := c.cache[key]
if !ok {
return
}
heap.Remove(&c.expireQueue, r.index)
delete(c.cache, key)
}
// Clear removes all saved records
func (c *Cache) Clear() {
c.m.Lock()
defer c.m.Unlock()
c.expireQueue = make(expireQueue, 0, c.capacity)
c.cache = make(map[string]*record, c.capacity)
}
// Len returns the number of records in the cache
func (c *Cache) Len() int {
c.m.Lock()
defer c.m.Unlock()
c.expire()
return len(c.cache)
}
// Expire removes old records
func (c *Cache) Expire() {
c.m.Lock()
defer c.m.Unlock()
c.expire()
}
func (c *Cache) expire() {
now := time.Now().UnixNano()
for c.expireQueue.Len() > 0 && now >= c.expireQueue[0].expireTimeStamp {
r := c.expireQueue.Pop().(*record)
delete(c.cache, r.key)
}
}
type record struct {
key string
value interface{}
ttl time.Duration
expireTimeStamp int64
index int
}
type expireQueue []*record
func (q expireQueue) Len() int { return len(q) }
func (q expireQueue) Less(i, j int) bool {
return q[i].expireTimeStamp > q[j].expireTimeStamp
}
func (q expireQueue) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
q[i].index = i
q[j].index = j
}
func (q *expireQueue) Push(x interface{}) {
n := len(*q)
item := x.(*record)
item.index = n
*q = append(*q, item)
}
func (q *expireQueue) Pop() interface{} {
old := *q
n := len(old)
item := old[n-1]
old[n-1] = nil
item.index = -1
*q = old[0 : n-1]
return item
}
func (q *expireQueue) update(oldRecord *record, value interface{}, ttl time.Duration, expireTimeStamp int64) {
oldRecord.value = value
oldRecord.ttl = ttl
oldRecord.expireTimeStamp = expireTimeStamp
heap.Fix(q, oldRecord.index)
}