-
Notifications
You must be signed in to change notification settings - Fork 8
/
ttlcache.go
116 lines (94 loc) · 2.24 KB
/
ttlcache.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
package ttlcache
import (
"sync"
"time"
)
const defaultCapacity = 16 // Just to avoid extra allocations in most of the cases.
// Cache represents key-value storage.
type Cache struct {
done chan struct{}
mu sync.RWMutex
items map[uint64]item
}
type item struct {
deadline int64 // Unix nano
value interface{}
}
// New creates key-value storage.
// resolution – configures cleanup manager.
// Cleanup operation locks storage so think twice before setting it to small value.
func New(resolution time.Duration) *Cache {
c := &Cache{
done: make(chan struct{}),
items: make(map[uint64]item, defaultCapacity),
}
go cleaner(c, resolution)
return c
}
// Get returns stored record.
// The first returned variable is a stored value.
// The second one is an existence flag like in the map.
func (c *Cache) Get(key uint64) (interface{}, bool) {
c.mu.RLock()
cacheItem, ok := c.items[key]
c.mu.RUnlock()
if !ok {
return nil, false
}
return cacheItem.value, true
}
// Set adds value to the cache with given ttl.
// ttl value should be a multiple of the resolution time value.
func (c *Cache) Set(key uint64, value interface{}, ttl time.Duration) {
cacheItem := item{
deadline: time.Now().UnixNano() + int64(ttl),
value: value,
}
c.mu.Lock()
c.items[key] = cacheItem
c.mu.Unlock()
}
// Delete removes record from storage.
func (c *Cache) Delete(key uint64) {
c.mu.Lock()
delete(c.items, key)
c.mu.Unlock()
}
// Clear removes all items from storage and leaves the cleanup manager running.
func (c *Cache) Clear() {
c.mu.Lock()
c.items = make(map[uint64]item, defaultCapacity)
c.mu.Unlock()
}
// Close stops cleanup manager and removes records from storage.
func (c *Cache) Close() error {
close(c.done)
c.mu.Lock()
c.items = nil
c.mu.Unlock()
return nil
}
// cleanup removes outdated items from the storage.
// It triggers stop the world for the cache.
func (c *Cache) cleanup() {
now := time.Now().UnixNano()
c.mu.Lock()
for key, item := range c.items {
if item.deadline < now {
delete(c.items, key)
}
}
c.mu.Unlock()
}
func cleaner(c *Cache, resolution time.Duration) {
ticker := time.NewTicker(resolution)
for {
select {
case <-ticker.C:
c.cleanup()
case <-c.done:
ticker.Stop()
return
}
}
}