-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttl.go
241 lines (202 loc) · 4.79 KB
/
ttl.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package util
import (
"context"
"sync"
"time"
)
type ttlCacheItem struct {
value interface{}
expire time.Time
}
// ttlCache is a cache that assigns TTL(Time-To-Live) for each items.
type ttlCache struct {
sync.RWMutex
ctx context.Context
items map[interface{}]ttlCacheItem
ttl time.Duration
gcInterval time.Duration
}
// NewTTL returns a new TTL cache.
func newTTL(ctx context.Context, gcInterval time.Duration, duration time.Duration) *ttlCache {
c := &ttlCache{
ctx: ctx,
items: make(map[interface{}]ttlCacheItem),
ttl: duration,
gcInterval: gcInterval,
}
go c.doGC()
return c
}
// Put puts an item into cache.
func (c *ttlCache) put(key interface{}, value interface{}) {
c.putWithTTL(key, value, c.ttl)
}
// PutWithTTL puts an item into cache with specified TTL.
func (c *ttlCache) putWithTTL(key interface{}, value interface{}, ttl time.Duration) {
c.Lock()
defer c.Unlock()
c.items[key] = ttlCacheItem{
value: value,
expire: time.Now().Add(ttl),
}
}
// Get retrieves an item from cache.
func (c *ttlCache) get(key interface{}) (interface{}, bool) {
c.RLock()
defer c.RUnlock()
item, ok := c.items[key]
if !ok {
return nil, false
}
if item.expire.Before(time.Now()) {
return nil, false
}
return item.value, true
}
// GetKeys returns all keys that are not expired.
func (c *ttlCache) getKeys() []interface{} {
c.RLock()
defer c.RUnlock()
var keys []interface{}
now := time.Now()
for key, item := range c.items {
if item.expire.After(now) {
keys = append(keys, key)
}
}
return keys
}
// Remove eliminates an item from cache.
func (c *ttlCache) remove(key interface{}) {
c.Lock()
defer c.Unlock()
delete(c.items, key)
}
// pop one key/value that is not expired. If boolean is false, it means that it didn't find the valid one.
func (c *ttlCache) pop() (interface{}, interface{}, bool) {
c.Lock()
defer c.Unlock()
now := time.Now()
for k, item := range c.items {
if item.expire.After(now) {
value := item.value
delete(c.items, k)
return k, value, true
}
}
return nil, nil, false
}
// Len returns current cache size.
func (c *ttlCache) Len() int {
c.RLock()
defer c.RUnlock()
return len(c.items)
}
// Clear removes all items in the ttl cache.
func (c *ttlCache) Clear() {
c.Lock()
defer c.Unlock()
for k := range c.items {
delete(c.items, k)
}
}
func (c *ttlCache) doGC() {
ticker := time.NewTicker(c.gcInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
count := 0
now := time.Now()
c.Lock()
for key := range c.items {
if value, ok := c.items[key]; ok {
if value.expire.Before(now) {
count++
delete(c.items, key)
}
}
}
c.Unlock()
case <-c.ctx.Done():
return
}
}
}
// TTLUint64 is simple TTL saves only uint64s.
type TTLUint64 struct {
*ttlCache
}
// NewIDTTL creates a new TTLUint64 cache.
func NewIDTTL(ctx context.Context, gcInterval, ttl time.Duration) *TTLUint64 {
return &TTLUint64{
ttlCache: newTTL(ctx, gcInterval, ttl),
}
}
// Get return the value by key id
func (c *TTLUint64) Get(id uint64) (interface{}, bool) {
return c.ttlCache.get(id)
}
// Put saves an ID in cache.
func (c *TTLUint64) Put(id uint64, value interface{}) {
c.ttlCache.put(id, value)
}
// GetAllID returns all ids.
func (c *TTLUint64) GetAllID() []uint64 {
keys := c.ttlCache.getKeys()
var ids []uint64
for _, key := range keys {
id, ok := key.(uint64)
if ok {
ids = append(ids, id)
}
}
return ids
}
// Exists checks if an ID exists in cache.
func (c *TTLUint64) Exists(id uint64) bool {
_, ok := c.ttlCache.get(id)
return ok
}
// Remove remove key
func (c *TTLUint64) Remove(key uint64) {
c.ttlCache.remove(key)
}
// PutWithTTL puts an item into cache with specified TTL.
func (c *TTLUint64) PutWithTTL(key uint64, value interface{}, ttl time.Duration) {
c.ttlCache.putWithTTL(key, value, ttl)
}
// TTLString is simple TTL saves key string and value.
type TTLString struct {
*ttlCache
}
// NewStringTTL creates a new TTLString cache.
func NewStringTTL(ctx context.Context, gcInterval, ttl time.Duration) *TTLString {
return &TTLString{
ttlCache: newTTL(ctx, gcInterval, ttl),
}
}
// Put put the string key with the value
func (c *TTLString) Put(key string, value interface{}) {
c.ttlCache.put(key, value)
}
// PutWithTTL puts an item into cache with specified TTL.
func (c *TTLString) PutWithTTL(key string, value interface{}, ttl time.Duration) {
c.ttlCache.putWithTTL(key, value, ttl)
}
// Pop one key/value that is not expired
func (c *TTLString) Pop() (string, interface{}, bool) {
k, v, success := c.ttlCache.pop()
if !success {
return "", nil, false
}
key, ok := k.(string)
if !ok {
return "", nil, false
}
return key, v, true
}
// Get return the value by key id
func (c *TTLString) Get(id string) (interface{}, bool) {
return c.ttlCache.get(id)
}