This repository was archived by the owner on Jul 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocalmemcache.go
More file actions
270 lines (218 loc) · 5.53 KB
/
localmemcache.go
File metadata and controls
270 lines (218 loc) · 5.53 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package appwrap
import (
"strconv"
"sync"
"time"
)
type cachedItem struct {
value []byte
expires time.Time
addedAt time.Time
}
type LocalMemcache struct {
items map[string]cachedItem
mtx sync.Mutex
}
func NewLocalMemcache() Memcache {
return &LocalMemcache{items: make(map[string]cachedItem)}
}
func (mc *LocalMemcache) Add(item *CacheItem) error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
var expires time.Time
addedAt := time.Now()
if item.Expiration > 0 {
expires = addedAt.Add(item.Expiration)
}
return mc.set(item.Key, cachedItem{value: item.Value, expires: expires, addedAt: addedAt}, true)
}
func (mc *LocalMemcache) AddMulti(items []*CacheItem) error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
errList := make(MultiError, len(items))
errors := false
addedAt := time.Now()
for i, item := range items {
var expires time.Time
if item.Expiration > 0 {
expires = addedAt.Add(item.Expiration)
}
if err := mc.set(item.Key, cachedItem{value: item.Value, expires: expires, addedAt: addedAt}, true); err != nil {
errList[i] = err
errors = true
}
}
if errors {
return errList
}
return nil
}
func (mc *LocalMemcache) CompareAndSwap(item *CacheItem) error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
if item == nil {
panic("item cannot be nil")
}
existingItem, found := mc.items[item.Key]
if !found {
return CacheErrNotStored
}
if !existingItem.addedAt.Equal(item.casTime.(time.Time)) {
return CacheErrCASConflict
}
existingItem.value = item.Value
existingItem.addedAt = time.Now()
mc.items[item.Key] = existingItem
return nil
}
func (mc *LocalMemcache) Delete(key string) error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
return mc.delete(key)
}
func (mc *LocalMemcache) DeleteMulti(keys []string) error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
hasErrors := false
multiError := make(MultiError, len(keys))
for i, key := range keys {
if err := mc.delete(key); err != nil {
multiError[i] = err
hasErrors = true
}
}
if hasErrors {
return multiError
}
return nil
}
func (mc *LocalMemcache) delete(key string) error {
if _, found := mc.items[key]; !found {
return ErrCacheMiss
}
delete(mc.items, key)
return nil
}
func (mc *LocalMemcache) Flush() error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
clear(mc.items)
return nil
}
func (mc *LocalMemcache) FlushShard(_ int) error {
return mc.Flush()
}
func (mc *LocalMemcache) get(key string) (*CacheItem, bool) {
cachedItem, found := mc.items[key]
if !found {
return nil, false
}
if !cachedItem.expires.IsZero() && cachedItem.expires.Before(time.Now()) {
delete(mc.items, key)
return nil, false
}
item := &CacheItem{
Key: key,
Value: cachedItem.value,
casTime: cachedItem.addedAt,
}
if !cachedItem.expires.IsZero() {
item.Expiration = cachedItem.expires.Sub(cachedItem.addedAt)
}
return item, true
}
func (mc *LocalMemcache) Get(key string) (*CacheItem, error) {
mc.mtx.Lock()
defer mc.mtx.Unlock()
item, exists := mc.get(key)
if !exists {
return nil, ErrCacheMiss
}
return item, nil
}
func (mc *LocalMemcache) GetMulti(keys []string) (map[string]*CacheItem, error) {
mc.mtx.Lock()
defer mc.mtx.Unlock()
results := make(map[string]*CacheItem)
for _, key := range keys {
if item, found := mc.get(key); found {
cpy := *item
results[key] = &cpy
}
}
return results, nil
}
func (mc *LocalMemcache) Increment(key string, amount int64, initialValue uint64, initialExpires time.Duration) (uint64, error) {
mc.mtx.Lock()
defer mc.mtx.Unlock()
return mc.increment(key, amount, &initialValue, initialExpires)
}
func (mc *LocalMemcache) IncrementExisting(key string, amount int64) (uint64, error) {
mc.mtx.Lock()
defer mc.mtx.Unlock()
return mc.increment(key, amount, nil, time.Duration(0))
}
func (mc *LocalMemcache) increment(key string, amount int64, initialValue *uint64, initialExpires time.Duration) (uint64, error) {
var oldValue uint64
item, exists := mc.items[key]
if !exists {
if initialValue == nil {
return 0, ErrCacheMiss
}
oldValue = *initialValue
item.addedAt = time.Now()
if initialExpires > 0 {
item.expires = item.addedAt.Add(initialExpires)
}
} else {
var err error
if oldValue, err = strconv.ParseUint(string(item.value), 10, 64); err != nil {
return 0, err
}
}
var newValue uint64
if amount < 0 {
newValue = oldValue - uint64(-amount)
} else {
newValue = oldValue + uint64(amount)
}
item.value = []byte(strconv.FormatUint(newValue, 10))
mc.items[key] = item
return newValue, nil
}
func (mc *LocalMemcache) set(key string, cachedItem cachedItem, addOnly bool) error {
if addOnly {
if _, exists := mc.items[key]; exists && (cachedItem.expires.IsZero() || !cachedItem.expires.Before(time.Now())) {
return CacheErrNotStored
}
}
mc.items[key] = cachedItem
return nil
}
func (mc *LocalMemcache) Set(item *CacheItem) error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
var expires time.Time
addedAt := time.Now()
if item.Expiration > 0 {
expires = addedAt.Add(item.Expiration)
}
return mc.set(item.Key, cachedItem{value: item.Value, expires: expires, addedAt: addedAt}, false)
}
func (mc *LocalMemcache) SetMulti(items []*CacheItem) error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
addedAt := time.Now()
for _, item := range items {
var expires time.Time
if item.Expiration > 0 {
expires = addedAt.Add(item.Expiration)
}
_ = mc.set(item.Key, cachedItem{value: item.Value, expires: expires, addedAt: addedAt}, false) // Set cannot error here, so ignore
}
return nil
}
func (mc *LocalMemcache) Namespace(_ string) Memcache {
// this should do something maybe
return mc
}