-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_test.go
125 lines (98 loc) · 2.94 KB
/
memory_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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xcache/blob/main/LICENSE.
package xcache_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/actforgood/xcache"
)
const (
freecacheMinMem = 512 * 1024 // 512 Kb
memoryBenchSize = 10 * 1024 * 1024 // 10 Mb
)
func init() {
var _ xcache.Cache = (*xcache.Memory)(nil) // test Memory is a Cache
}
func TestMemory(t *testing.T) {
t.Parallel()
subject := xcache.NewMemory(1)
t.Run("key that does not expire", testCacheWithNoExpireKey(subject))
t.Run("key expires", testCacheWithExpireKey(subject))
t.Run("key does not exist", testCacheWithNotExistKey(subject))
t.Run("delete key", testCacheDeleteKey(subject))
t.Run("ttl for not yet expired key", testCacheTTLWithNotYetExpiredKey(subject))
t.Run("stats", testCacheStats(subject, freecacheMinMem, freecacheMinMem, "==", true))
}
func BenchmarkMemory_Save(b *testing.B) {
cache := xcache.NewMemory(memoryBenchSize)
benchSaveSequential(cache)(b)
b.StopTimer()
stats, _ := cache.Stats(context.Background())
b.Log(stats)
}
func BenchmarkMemory_Save_parallel(b *testing.B) {
cache := xcache.NewMemory(memoryBenchSize)
benchSaveParallel(cache)(b)
b.StopTimer()
stats, _ := cache.Stats(context.Background())
b.Log(stats)
}
func BenchmarkMemory_Load(b *testing.B) {
cache := xcache.NewMemory(memoryBenchSize)
benchLoadSequential(cache)(b)
b.StopTimer()
stats, _ := cache.Stats(context.Background())
b.Log(stats)
}
func BenchmarkMemory_Load_parallel(b *testing.B) {
cache := xcache.NewMemory(memoryBenchSize)
benchLoadParallel(cache)(b)
b.StopTimer()
stats, _ := cache.Stats(context.Background())
b.Log(stats)
}
func BenchmarkMemory_TTL(b *testing.B) {
cache := xcache.NewMemory(memoryBenchSize)
benchTTLSequential(cache)(b)
b.StopTimer()
stats, _ := cache.Stats(context.Background())
b.Log(stats)
}
func BenchmarkMemory_TTL_parallel(b *testing.B) {
cache := xcache.NewMemory(memoryBenchSize)
benchTTLParallel(cache)(b)
b.StopTimer()
stats, _ := cache.Stats(context.Background())
b.Log(stats)
}
func BenchmarkMemory_Stats(b *testing.B) {
cache := xcache.NewMemory(memoryBenchSize)
benchStatsSequential(cache)(b)
}
func BenchmarkMemory_Stats_parallel(b *testing.B) {
cache := xcache.NewMemory(memoryBenchSize)
benchStatsParallel(cache)(b)
}
func ExampleMemory() {
cache := xcache.NewMemory(10 * 1024 * 1024) // 10 Mb
ctx := context.Background()
key := "example-memory"
value := []byte("Hello Memory Cache")
ttl := 10 * time.Minute
// save a key for 10 minutes
if err := cache.Save(ctx, key, value, ttl); err != nil {
fmt.Println("could not save Memory cache key: " + err.Error())
}
// load the key's value
if value, err := cache.Load(ctx, key); err != nil {
fmt.Println("could not get Memory cache key: " + err.Error())
} else {
fmt.Println(string(value))
}
// Output:
// Hello Memory Cache
}