-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgocache.go
225 lines (178 loc) · 3.8 KB
/
gocache.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
package gocache
import (
"sync"
"time"
"unsafe"
"github.com/cespare/xxhash"
"github.com/kpango/fastime"
)
// Gocache is base gocache interface.
type Gocache interface {
// Get returns object with the given name from the cache.
Get(string) (interface{}, bool)
// Set sets object in the cache.
Set(string, interface{}) bool
// SetWithExpire sets object in cache with an expiration date.
SetWithExpire(string, interface{}, time.Duration) bool
// Delete deletes cache object of given name.
Delete(string)
// Clear clears cache.
Clear()
// StartExpired starts worker that deletes an expired cache object.
// Deletion processing is executed at intervals of given time.
StartExpired(dur time.Duration) Gocache
// StopExpired stop worker that deletes an expired cache object.
StopExpired() Gocache
}
type (
gocache struct {
shards shards
*config
}
shard struct {
*sync.Map
starting bool
doneCh chan struct{}
}
shards []*shard
record struct {
val interface{}
expire int64
}
)
func (r *record) isValid() bool {
return fastime.Now().UnixNano() < r.expire
}
// New returns Gocache (*gocache) instance.
func New(options ...Option) Gocache {
g := newDefaultGocache()
for _, opt := range options {
opt(g)
}
for i := 0; i < int(g.ShardsCount); i++ {
g.shards[i] = newDefaultShard()
}
return g
}
func newDefaultGocache() *gocache {
return &gocache{
shards: make(shards, DefaultShardsCount),
config: newDefaultConfig(),
}
}
func newDefaultShard() *shard {
return &shard{
Map: new(sync.Map),
starting: false,
doneCh: make(chan struct{}),
}
}
func (g *gocache) getShard(key string) *shard {
return g.shards[xxhash.Sum64(*(*[]byte)(unsafe.Pointer(&key)))%g.ShardsCount]
}
func (g *gocache) Get(key string) (interface{}, bool) {
val, ok := g.getShard(key).get(key)
if !ok {
return nil, false
}
return val, ok
}
func (g *gocache) Set(key string, val interface{}) bool {
shard := g.getShard(key)
return shard.set(key, val, g.Expire)
}
func (g *gocache) SetWithExpire(key string, val interface{}, expire time.Duration) bool {
shard := g.getShard(key)
return shard.set(key, val, *(*int64)(unsafe.Pointer(&expire)))
}
func (g *gocache) Delete(key string) {
g.getShard(key).delete(key)
}
func (g *gocache) DeleteExpired() {
for _, shard := range g.shards {
shard.deleteExpired()
}
}
func (g *gocache) Clear() {
for _, shard := range g.shards {
shard.deleteAll()
}
}
func (g *gocache) StartExpired(dur time.Duration) Gocache {
if dur <= 0 {
return g
}
for _, shard := range g.shards {
if shard.starting {
return g
}
}
for _, shard := range g.shards {
shard.starting = true
go shard.start(dur)
}
return g
}
func (g *gocache) StopExpired() Gocache {
for _, shard := range g.shards {
if shard.starting {
shard.doneCh <- struct{}{}
shard.starting = false
}
}
return g
}
func (s *shard) get(key string) (interface{}, bool) {
val, ok := s.Load(key)
if !ok {
return nil, false
}
rcd := val.(*record)
if rcd.isValid() {
return rcd.val, ok
}
s.Delete(key)
return nil, false
}
func (s *shard) set(key string, val interface{}, expire int64) bool {
if expire <= 0 {
return false
}
s.Store(key, &record{
val: val,
expire: fastime.Now().UnixNano() + expire,
})
return true
}
func (s *shard) delete(key string) {
s.Delete(key)
}
func (s *shard) deleteAll() {
s.Range(func(key interface{}, val interface{}) bool {
s.Delete(key)
return true
})
}
func (s *shard) deleteExpired() {
s.Range(func(key interface{}, val interface{}) bool {
if !val.(*record).isValid() {
s.Delete(key)
}
return true
})
}
func (s *shard) start(dur time.Duration) {
t := time.NewTicker(dur)
defer func() {
t.Stop()
close(s.doneCh)
}()
for {
select {
case _ = <-s.doneCh:
return
case _ = <-t.C:
s.deleteExpired()
}
}
}