-
Notifications
You must be signed in to change notification settings - Fork 2
/
localmemcache.go
225 lines (189 loc) · 4.63 KB
/
localmemcache.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 appwrap
import (
"fmt"
"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 {
l := &LocalMemcache{}
l.Flush()
return l
}
func (mc *LocalMemcache) Add(item *CacheItem) error {
if _, exists := mc.get(item.Key); exists {
return CacheErrNotStored
} else {
return mc.Set(item)
}
return nil
}
func (mc *LocalMemcache) AddMulti(items []*CacheItem) error {
errList := make(MultiError, len(items))
errors := false
for i, item := range items {
if err := mc.Add(item); err != nil {
errList[i] = err
errors = true
}
}
if errors {
return errList
}
return nil
}
func (mc *LocalMemcache) CompareAndSwap(item *CacheItem) error {
if entry, err := mc.Get(item.Key); err != nil && err == ErrCacheMiss {
return CacheErrNotStored
} else if err != nil {
return err
} else if !entry.casTime.(time.Time).Equal(item.casTime.(time.Time)) {
return CacheErrCASConflict
}
return mc.SetMulti([]*CacheItem{item})
}
func (mc *LocalMemcache) delete(key string) {
mc.mtx.Lock()
defer mc.mtx.Unlock()
delete(mc.items, key)
}
func (mc *LocalMemcache) Delete(key string) error {
if _, exists := mc.get(key); !exists {
return ErrCacheMiss
}
mc.delete(key)
return nil
}
func (mc *LocalMemcache) DeleteMulti(keys []string) error {
errors := false
multiError := make(MultiError, len(keys))
for i, key := range keys {
if _, exists := mc.get(key); !exists {
multiError[i] = ErrCacheMiss
errors = true
} else {
delete(mc.items, key)
}
}
if errors {
return multiError
}
return nil
}
func (mc *LocalMemcache) Flush() error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
mc.items = make(map[string]cachedItem)
return nil
}
func (mc *LocalMemcache) FlushShard(shard int) error {
mc.mtx.Lock()
defer mc.mtx.Unlock()
mc.items = make(map[string]cachedItem)
return nil
}
func (mc *LocalMemcache) get(key string) (item cachedItem, found bool) {
mc.mtx.Lock()
defer mc.mtx.Unlock()
item, found = mc.items[key]
return
}
func (mc *LocalMemcache) Get(key string) (*CacheItem, error) {
if item, exists := mc.get(key); !exists {
return nil, ErrCacheMiss
} else if !item.expires.IsZero() && item.expires.Before(time.Now()) {
mc.delete(key)
return nil, ErrCacheMiss
} else {
cacheItem := CacheItem{
Key: key,
Value: item.value,
casTime: item.addedAt,
}
if !item.expires.IsZero() {
cacheItem.Expiration = item.expires.Sub(item.addedAt)
}
return &cacheItem, nil
}
}
func (mc *LocalMemcache) GetMulti(keys []string) (map[string]*CacheItem, error) {
results := make(map[string]*CacheItem)
for _, key := range keys {
if item, err := mc.Get(key); err == nil {
cpy := *item
results[key] = &cpy
}
}
return results, nil
}
func (mc *LocalMemcache) Increment(key string, amount int64, initialValue uint64, initialExpires time.Duration) (uint64, error) {
return mc.increment(key, amount, &initialValue, initialExpires)
}
func (mc *LocalMemcache) IncrementExisting(key string, amount int64) (uint64, error) {
return mc.increment(key, amount, nil, time.Duration(0))
}
func (mc *LocalMemcache) increment(key string, amount int64, initialValue *uint64, initialExpires time.Duration) (uint64, error) {
if item, exists := mc.get(key); !exists && initialValue == nil {
return 0, ErrCacheMiss
} else {
var oldValue uint64
if !exists {
addedAt := time.Now()
item = cachedItem{addedAt: addedAt}
if initialExpires > 0 {
item.expires = addedAt.Add(initialExpires)
}
if initialValue != nil {
oldValue = *initialValue
}
} 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(fmt.Sprintf("%d", newValue))
mc.set(key, item)
return newValue, nil
}
}
func (mc *LocalMemcache) set(key string, cachedItem cachedItem) {
mc.mtx.Lock()
defer mc.mtx.Unlock()
mc.items[key] = cachedItem
}
func (mc *LocalMemcache) Set(item *CacheItem) error {
var expires time.Time
if item.Expiration > 0 {
expires = time.Now().Add(time.Duration(item.Expiration))
} else {
expires = time.Time{}
}
mc.set(item.Key, cachedItem{value: item.Value, expires: expires, addedAt: time.Now()})
return nil
}
func (mc *LocalMemcache) SetMulti(items []*CacheItem) error {
for _, item := range items {
mc.Set(item)
}
return nil
}
func (mc *LocalMemcache) Namespace(ns string) Memcache {
// this should do something maybe
return mc
}