-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcache_test.go
346 lines (280 loc) · 7.67 KB
/
cache_test.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package cache
import (
"crypto/rand"
"fmt"
"math"
"math/big"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func getRand(tb testing.TB) int64 {
out, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
tb.Fatal(err)
}
return out.Int64()
}
func BenchmarkLRU_Rand_NoExpire(b *testing.B) {
l, _ := NewCache(MaxKeys(8192), LRU())
trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
trace[i] = getRand(b) % 32768
}
b.ResetTimer()
var hit, miss int
for i := 0; i < 2*b.N; i++ {
if i%2 == 0 {
l.Set(strconv.Itoa(int(trace[i])), trace[i], 0)
} else {
if _, ok := l.Get(strconv.Itoa(int(trace[i]))); ok {
hit++
} else {
miss++
}
}
}
b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
}
func BenchmarkLRU_Freq_NoExpire(b *testing.B) {
l, _ := NewCache(MaxKeys(8192), LRU())
trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
if i%2 == 0 {
trace[i] = getRand(b) % 16384
} else {
trace[i] = getRand(b) % 32768
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Set(strconv.Itoa(int(trace[i])), trace[i], 0)
}
var hit, miss int
for i := 0; i < b.N; i++ {
if _, ok := l.Get(strconv.Itoa(int(trace[i]))); ok {
hit++
} else {
miss++
}
}
b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
}
func BenchmarkLRU_Rand_WithExpire(b *testing.B) {
l, _ := NewCache(MaxKeys(8192), LRU(), TTL(time.Millisecond*10))
trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
trace[i] = getRand(b) % 32768
}
b.ResetTimer()
var hit, miss int
for i := 0; i < 2*b.N; i++ {
if i%2 == 0 {
l.Set(strconv.Itoa(int(trace[i])), trace[i], 0)
} else {
if _, ok := l.Get(strconv.Itoa(int(trace[i]))); ok {
hit++
} else {
miss++
}
}
}
b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
}
func BenchmarkLRU_Freq_WithExpire(b *testing.B) {
l, _ := NewCache(MaxKeys(8192), LRU(), TTL(time.Millisecond*10))
trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
if i%2 == 0 {
trace[i] = getRand(b) % 16384
} else {
trace[i] = getRand(b) % 32768
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Set(strconv.Itoa(int(trace[i])), trace[i], 0)
}
var hit, miss int
for i := 0; i < b.N; i++ {
if _, ok := l.Get(strconv.Itoa(int(trace[i]))); ok {
hit++
} else {
miss++
}
}
b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
}
func TestCacheNoPurge(t *testing.T) {
lc, err := NewCache()
assert.NoError(t, err)
lc.Set("key1", "val1", 0)
assert.Equal(t, 1, lc.Len())
v, ok := lc.Peek("key1")
assert.Equal(t, "val1", v)
assert.True(t, ok)
v, ok = lc.Peek("key2")
assert.Empty(t, v)
assert.False(t, ok)
assert.Equal(t, []string{"key1"}, lc.Keys())
}
func TestCacheWithDeleteExpired(t *testing.T) {
var evicted []string
lc, err := NewCache(
TTL(150*time.Millisecond),
OnEvicted(func(key string, value interface{}) { evicted = append(evicted, key, value.(string)) }),
)
assert.NoError(t, err)
lc.Set("key1", "val1", 0)
time.Sleep(100 * time.Millisecond) // not enough to expire
lc.DeleteExpired()
assert.Equal(t, 1, lc.Len())
v, ok := lc.Get("key1")
assert.Equal(t, "val1", v)
assert.True(t, ok)
time.Sleep(200 * time.Millisecond) // expire
lc.DeleteExpired()
v, ok = lc.Get("key1")
assert.False(t, ok)
assert.Nil(t, v)
assert.Equal(t, 0, lc.Len())
assert.Equal(t, []string{"key1", "val1"}, evicted)
// add new entry
lc.Set("key2", "val2", 0)
assert.Equal(t, 1, lc.Len())
// nothing deleted
lc.DeleteExpired()
assert.Equal(t, 1, lc.Len())
assert.Equal(t, []string{"key1", "val1"}, evicted)
// Purge, cache should be clean
lc.Purge()
assert.Equal(t, 0, lc.Len())
assert.Equal(t, []string{"key1", "val1", "key2", "val2"}, evicted)
}
func TestCacheWithPurgeEnforcedBySize(t *testing.T) {
lc, err := NewCache(MaxKeys(10), TTL(time.Hour))
assert.NoError(t, err)
for i := 0; i < 100; i++ {
i := i
lc.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("val%d", i), 0)
v, ok := lc.Get(fmt.Sprintf("key%d", i))
assert.Equal(t, fmt.Sprintf("val%d", i), v)
assert.True(t, ok)
assert.True(t, lc.Len() < 20)
}
assert.Equal(t, 10, lc.Len())
}
func TestCacheConcurrency(t *testing.T) {
lc, err := NewCache()
assert.NoError(t, err)
wg := sync.WaitGroup{}
wg.Add(1000)
for i := 0; i < 1000; i++ {
go func(i int) {
lc.Set(fmt.Sprintf("key-%d", i/10), fmt.Sprintf("val-%d", i/10), 0)
wg.Done()
}(i)
}
wg.Wait()
assert.Equal(t, 100, lc.Len())
}
func TestCacheInvalidateAndEvict(t *testing.T) {
var evicted int
lc, err := NewCache(LRU(), OnEvicted(func(_ string, _ interface{}) { evicted++ }))
assert.NoError(t, err)
lc.Set("key1", "val1", 0)
lc.Set("key2", "val2", 0)
val, ok := lc.Get("key1")
assert.True(t, ok)
assert.Equal(t, "val1", val)
assert.Equal(t, 0, evicted)
lc.Invalidate("key1")
assert.Equal(t, 1, evicted)
val, ok = lc.Get("key1")
assert.Empty(t, val)
assert.False(t, ok)
val, ok = lc.Get("key2")
assert.True(t, ok)
assert.Equal(t, "val2", val)
lc.InvalidateFn(func(key string) bool {
return key == "key2"
})
assert.Equal(t, 2, evicted)
_, ok = lc.Get("key2")
assert.False(t, ok)
assert.Equal(t, 0, lc.Len())
}
func TestCacheBadOption(t *testing.T) {
lc, err := NewCache(func(_ *cacheImpl) error {
return fmt.Errorf("mock err")
})
assert.EqualError(t, err, "failed to set cache option: mock err")
assert.Nil(t, lc)
}
func TestCacheExpired(t *testing.T) {
lc, err := NewCache(TTL(time.Millisecond * 5))
assert.NoError(t, err)
lc.Set("key1", "val1", 0)
assert.Equal(t, 1, lc.Len())
v, ok := lc.Peek("key1")
assert.Equal(t, v, "val1")
assert.True(t, ok)
v, ok = lc.Get("key1")
assert.Equal(t, v, "val1")
assert.True(t, ok)
time.Sleep(time.Millisecond * 10) // wait for entry to expire
assert.Equal(t, 1, lc.Len()) // but not purged
v, ok = lc.Peek("key1")
assert.Empty(t, v)
assert.False(t, ok)
v, ok = lc.Get("key1")
assert.Empty(t, v)
assert.False(t, ok)
}
func TestCacheRemoveOldest(t *testing.T) {
lc, err := NewCache(LRU(), MaxKeys(2))
assert.NoError(t, err)
lc.Set("key1", "val1", 0)
assert.Equal(t, 1, lc.Len())
v, ok := lc.Get("key1")
assert.True(t, ok)
assert.Equal(t, "val1", v)
assert.Equal(t, []string{"key1"}, lc.Keys())
assert.Equal(t, 1, lc.Len())
lc.Set("key2", "val2", 0)
assert.Equal(t, []string{"key1", "key2"}, lc.Keys())
assert.Equal(t, 2, lc.Len())
lc.RemoveOldest()
assert.Equal(t, []string{"key2"}, lc.Keys())
assert.Equal(t, 1, lc.Len())
}
func ExampleCache() {
// make cache with short TTL and 3 max keys
cache, _ := NewCache(MaxKeys(3), TTL(time.Millisecond*10))
// set value under key1.
// with 0 ttl (last parameter) will use cache-wide setting instead (10ms).
cache.Set("key1", "val1", 0)
// get value under key1
r, ok := cache.Get("key1")
// check for OK value, because otherwise return would be nil and
// type conversion will panic
if ok {
rstr := r.(string) // convert cached value from interface{} to real type
fmt.Printf("value before expiration is found: %v, value: %v\n", ok, rstr)
}
time.Sleep(time.Millisecond * 11)
// get value under key1 after key expiration
r, ok = cache.Get("key1")
// don't convert to string as with ok == false value would be nil
fmt.Printf("value after expiration is found: %v, value: %v\n", ok, r)
// set value under key2, would evict old entry because it is already expired.
// ttl (last parameter) overrides cache-wide ttl.
cache.Set("key2", "val2", time.Minute*5)
fmt.Printf("%+v\n", cache)
// Output:
// value before expiration is found: true, value: val1
// value after expiration is found: false, value: <nil>
// Size: 1, Stats: {Hits:1 Misses:1 Added:2 Evicted:1} (50.0%)
}