-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
170 lines (148 loc) · 3.87 KB
/
cache.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 ttlmap
import (
"sync"
"time"
)
// A "thread" safe map of type string:Interface{}
// To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.
type CacheMap struct {
items []*CacheMapShared
options cacheOptions
}
// A "thread" safe string to anything map
type CacheMapShared struct {
shutdown chan bool
cleanupCycle time.Duration
items map[string]*Item
sync.RWMutex // Read Write mutex, guards access to internal map.
}
// Creates a new cache map
func New(opts ...CacheOption) CacheMap {
cmp := CacheMap{options: defaultCacheOptions()}
for _, opt := range opts {
opt(&cmp.options)
}
cmp.items = make([]*CacheMapShared, cmp.options.shardCount)
for i := 0; i < cmp.options.shardCount; i++ {
cmp.items[i] = &CacheMapShared{items: make(map[string]*Item)}
cmp.items[i].initCleanup(cmp.options.cleanupDuration)
}
return cmp
}
func (m CacheMap) Close() {
for i := 0; i < m.options.shardCount; i++ {
m.items[i].Close()
}
}
// Close stops the cleanup
func (ms CacheMapShared) Close() {
ms.shutdown <- true
}
// Returns shard under given key
func (m CacheMap) GetShard(key string) *CacheMapShared {
return m.items[uint(fnv32(key))%uint(m.options.shardCount)]
}
func (m CacheMap) MSet(data map[string]interface{}, duration time.Duration) {
for key, value := range data {
shard := m.GetShard(key)
shard.Lock()
shard.items[key] = newItem(value, duration, time.Now().Add(m.options.maxLifetime), nil)
shard.Unlock()
}
}
func (m CacheMap) SetWithCleanup(key string, value interface{}, duration *time.Duration, cleanup func(*Item)) {
// Get map shard.
shard := m.GetShard(key)
shard.Lock()
if duration == nil {
duration = &m.options.defaultCacheDuration
}
itm := newItem(value, *duration, time.Now().Add(m.options.maxLifetime), cleanup)
shard.items[key] = itm
shard.Unlock()
}
// Sets the given value under the specified key
func (m CacheMap) Set(key string, value interface{}, duration *time.Duration) {
m.SetWithCleanup(key, value, duration, nil)
}
// Retrieves an item from the map with the given key, and optionally increase its expiry time if found
func (m CacheMap) TouchGet(key string, touch bool) (interface{}, bool) {
shard := m.GetShard(key)
shard.RLock()
// Get item from shard.
val, ok := shard.items[key]
var ret interface{}
if ok {
if val.Expired() {
ok = false
} else {
if touch {
val.Touch()
}
ret = val.GetValue()
}
}
shard.RUnlock()
return ret, ok
}
// Retrieves an item from the map with the given key, and increase its expiry time if found
func (m CacheMap) Get(key string) (interface{}, bool) {
return m.TouchGet(key, true)
}
// Retrieves an item from the map with the given key, and increase its expiry time if found
func (m CacheMap) GetItem(key string) (*Item, bool) {
shard := m.GetShard(key)
shard.RLock()
defer shard.RUnlock()
if val, ok := shard.items[key]; ok {
return &Item{
data: val.data,
deadline: val.deadline,
ttl: val.ttl,
expires: val.expires,
}, true
}
return nil, false
}
// Removes an element from the map
func (m CacheMap) Remove(key string) {
shard := m.GetShard(key)
if shard != nil {
shard.Remove(key)
}
}
// Removes an element from the map
func (ms CacheMapShared) Remove(key string) {
ms.Lock()
ms.remove(key)
ms.Unlock()
}
// Removes an element from the map
func (ms CacheMapShared) remove(key string) {
if itm, ok := ms.items[key]; ok && itm.onDelete != nil {
itm.onDelete(itm)
}
delete(ms.items, key)
}
// Has checks to see if an item exists
func (m CacheMap) Has(key string) bool {
shard := m.GetShard(key)
shard.RLock()
val, ok := shard.items[key]
if ok && val.Expired() {
ok = false
}
shard.RUnlock()
return ok
}
func (m CacheMap) GetExpiry(key string) *time.Time {
shard := m.GetShard(key)
shard.RLock()
var expiry *time.Time
val, ok := shard.items[key]
if ok {
expiry = val.expires
}
shard.RUnlock()
return expiry
}