-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathycache_test.go
217 lines (208 loc) · 6.43 KB
/
ycache_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
package ycache
import (
"context"
"fmt"
"testing"
"time"
)
var redisHost = "127.0.0.1:6379"
var redisPwd = "123456"
var cache *YCache
var bizCacheIns *YInstance
func TestMain(m *testing.M) {
redisConf := &RedisConfig{
MaxTry: 2,
WriteAddr: redisHost,
ReadAddr: redisHost,
Password: redisPwd,
ReadTimeout: 1,
WriteTimeout: 1,
ConnTimeout: 3,
MaxActiveConns: 64,
MaxIdleConns: 5,
IdleTimeout: 10,
}
cache = NewYCache("my_first_test",
WithCacheOptionErrorHandle(func(desc string, err error) {
//fmt.Printf("ycache err:%s\n", err.Error())
}),
WithCacheOptionCacheLevel(CacheL1,
NewMemCache("my_first_mem_cache", 10000000, -1),
),
WithCacheOptionCacheLevel(CacheL2,
NewRedisClient("my_redis_cache", redisConf),
),
)
var err error
bizCacheIns, err = cache.CreateInstance("my_instance_1",
[]CacheLevel{CacheL1, CacheL2},
WithInstanceOptionCacheTtl(60),
WithInstanceOptionRandomTtl(20),
WithInstanceOptionTtlFactor(1),
)
if err != nil {
panic(fmt.Sprintf("create instance1 err:%s", err.Error()))
}
m.Run()
}
func TestYInstanceGetSet(t *testing.T) {
key := "k1"
val, err := bizCacheIns.Get(context.Background(), "_abc_", key, func(ctx context.Context, key string) ([]byte, error) {
return []byte("hello"), nil
})
if err != nil {
t.Fatalf("instance get err:%s", err.Error())
}
if string(val) != "hello" {
t.Fatalf("instance get key must equal 'hello'")
}
time.Sleep(time.Second * 1)
val, _ = bizCacheIns.Get(context.Background(), "_abc_", key, func(ctx context.Context, key string) ([]byte, error) {
return []byte("second"), nil
})
if string(val) == "second" {
t.Fatalf("instance get key must not equal 'second'")
}
val, err = bizCacheIns.Get(context.Background(), "_abcd_", key, func(ctx context.Context, key string) ([]byte, error) {
return nil, fmt.Errorf("prefix _abcd_ err")
})
if err != nil {
t.Logf("instance get key load should err, key:%s,err:%s", key, err.Error())
} else {
t.Fatalf("instance get key must err")
}
time.Sleep(time.Millisecond * 50)
}
func TestYInstanceBatchGetSet(t *testing.T) {
key1 := "k1"
key2 := "k2"
val, err := bizCacheIns.Get(context.Background(), "_abc_", key1, func(ctx context.Context, key string) ([]byte, error) {
return []byte("hello"), nil
})
if err != nil {
t.Fatalf("instance get key1 err:%s", err.Error())
}
time.Sleep(time.Second * 2)
keys := []string{key1, key2}
mp, _ := bizCacheIns.BatchGet(context.Background(), "_abc_", keys, func(ctx context.Context, keys []string) (map[string][]byte, error) {
ret := map[string][]byte{
"k2": []byte("world"),
}
return ret, nil
})
for k, v := range mp {
if k == key1 && string(v) != "hello" {
t.Fatalf("batch get key1(%s) not equal", k)
} else if k == key2 && string(v) != "world" {
t.Fatalf("batch get key2(%s) not equal", k)
}
}
val, err = bizCacheIns.Get(context.Background(), "_abc_", key2, func(ctx context.Context, key string) ([]byte, error) {
return []byte("world2"), nil
})
if err != nil {
t.Fatalf("instance get key2 load err, key2:%s,err:%s", key2, err.Error())
} else if string(val) == "world2" {
t.Fatalf("instance get key2 not expired, must not equal 'world2'")
}
time.Sleep(time.Millisecond * 50)
}
func TestYInstanceBatchDel(t *testing.T) {
key1 := "k1"
key2 := "k2"
val, err := bizCacheIns.Get(context.Background(), "_abc_", key1, func(ctx context.Context, key string) ([]byte, error) {
return []byte("hello"), nil
})
if err != nil {
t.Fatalf("instance get key1 err:%s", err.Error())
}
time.Sleep(time.Second * 2)
keys := []string{key1, key2}
mp, _ := bizCacheIns.BatchGet(context.Background(), "_abc_", keys, func(ctx context.Context, keys []string) (map[string][]byte, error) {
ret := map[string][]byte{
"k2": []byte("world"),
}
return ret, nil
})
val, err = bizCacheIns.Get(context.Background(), "_abc_", key2, nil)
if err != nil {
t.Fatalf("instance empty get key2 load err:%s", err.Error())
} else if string(mp[key2]) != "world" {
t.Fatalf("instance empty get key2 must equal 'world'")
}
err = bizCacheIns.Delete(context.Background(), "_abc_", key2)
if err != nil {
t.Fatalf("instance delete key2 err:%s", err.Error())
}
val, err = bizCacheIns.Get(context.Background(), "_abc_", key2, nil)
if err != nil {
t.Logf("instance get key2 load must err is normal")
} else {
t.Fatalf("instance get key2 must err")
}
val, err = bizCacheIns.Get(context.Background(), "_abc_", key2, func(ctx context.Context, key string) ([]byte, error) {
return []byte("new key2 val"), nil
})
if err != nil {
t.Fatalf("instance get key2 load error, key:%s,err:%s", key2, err.Error())
} else if string(val) != "new key2 val" {
t.Fatalf("instance get key2 success, key:%s must equal 'new key2 val'", key2)
}
err = bizCacheIns.BatchDelete(context.Background(), "_abc_", []string{key2})
if err != nil {
t.Fatalf("instance batch delete key2 error, key:%s,err:%s", key2, err.Error())
}
val, err = bizCacheIns.Get(context.Background(), "_abc_", key2, nil)
if err != nil {
t.Logf("instance get key2 error is normal, key:%s,err:%s", key2, err.Error())
} else {
t.Fatalf("instance get key must error, key:%s", key2)
}
time.Sleep(time.Millisecond * 50)
}
func TestYInstanceConcurrent(t *testing.T) {
key1 := "k15"
oldStat := bizCacheIns.Stat()
for i := 0; i < 10; i++ {
i := i
go func() {
_, _ = bizCacheIns.Get(context.Background(), "_abc_", key1, func(ctx context.Context, key string) ([]byte, error) {
time.Sleep(time.Second)
if i < 10 {
return []byte("k1 hello"), nil
} else {
return []byte("k1 strategy ok"), nil
}
})
//t.Logf("instance get key1 success, index:%d,val:%v", i, string(val))
}()
}
time.Sleep(time.Second * 3)
stat := bizCacheIns.Stat()
if stat.TotalLoadCount-oldStat.TotalLoadCount != 1 {
t.Fatalf("must load once")
}
key2 := "k25"
for i := 0; i < 5; i++ {
//i := i
go func() {
kvs, _ := bizCacheIns.BatchGet(context.Background(), "_abc_", []string{key2, key1}, func(ctx context.Context, keys []string) (map[string][]byte, error) {
time.Sleep(time.Second)
return map[string][]byte{
key1: []byte("k1 batch"),
key2: []byte("k2 batch"),
}, nil
})
mp := make(map[string]string)
for k, v := range kvs {
mp[k] = string(v)
}
//t.Logf("instance batch get key1,key2 success, index:%d,kvs:%v", i, mp)
}()
}
time.Sleep(time.Second * 3)
stat = bizCacheIns.Stat()
if stat.TotalLoadCount-oldStat.TotalLoadCount != 2 {
t.Fatalf("must load twice")
}
}