-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator-redis_test.go
125 lines (107 loc) · 3.07 KB
/
decorator-redis_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
package examples
import (
"testing"
"time"
"github.com/ahuigo/gofnext"
"github.com/go-redis/redis"
)
func TestRedisCacheClient(t *testing.T) {
// method 1: by default: localhost:6379
cache := gofnext.NewCacheRedis("redis-cache-key") // you can list value `HGETALL _gofnext:redis-cache-key`
// method 2: set redis addr
cache.SetRedisAddr("192.168.1.1:6379")
// method 3: set redis options
cache.SetRedisOpts(&redis.Options{
Addr: "localhost:6379",
})
// method 4: set redis universal options
cache.SetRedisUniversalOpts(&redis.UniversalOptions{
Addrs: []string{"localhost:6379"},
})
cache.SetMaxHashKeyLen(0)
cache.SetMaxHashKeyLen(100)
}
func TestRedisCacheFuncWithTTL(t *testing.T) {
// Original function
executeCount := 0
add98Origin := func(more int) (int, error) {
executeCount++
return 98 + more, nil
}
redisCache := gofnext.NewCacheRedis("redis-cache-key")
redisCache.ClearAll() // redis> del _gofnext:redis-cache-key
// redisCache.SetRedisOpts(&redis.Options{
// Addr: "localhost:6379",
// })
// Cacheable Function
add98 := gofnext.CacheFn1Err(add98Origin, &gofnext.Config{
TTL: time.Hour,
CacheMap: redisCache,
})
// Execute the function 10 times
for i := 0; i < 10; i++ {
score, err := add98(1)
if err != nil || score != 99 {
t.Errorf("score should be 99, but get %d", score)
}
score, _ = add98(2)
if score != 100 {
t.Fatalf("score should be 100, but get %d", score)
}
add98(3)
add98(3)
}
if executeCount != 3 {
t.Errorf("executeCount should be 1, but get %d", executeCount)
}
}
func TestRedisCacheFuncWithNoTTL(t *testing.T) {
// Original function
executeCount := 0
getUserScore := func(more int, flag bool) (int, error) {
executeCount++
return 98 + more, nil
}
// Cacheable Function
getUserScoreFromDbWithCache := gofnext.CacheFn2Err(
getUserScore,
&gofnext.Config{
CacheMap: gofnext.NewCacheRedis("redis-cache-key").ClearAll(),
},
) // getFunc can only accept 1 parameter
// Execute the function multi times in parallel.
parallelCall(func() {
score, err := getUserScoreFromDbWithCache(1, true)
if err != nil || score != 99 {
t.Errorf("score should be 99, but get %d", score)
}
getUserScoreFromDbWithCache(2, true)
getUserScoreFromDbWithCache(3, true)
getUserScoreFromDbWithCache(3, true)
}, 10)
if executeCount != 3 {
t.Errorf("executeCount should be 3, but get %d", executeCount)
}
}
func TestRedisCacheFuncWithTTLTimeout(t *testing.T) {
// Original function
executeCount := 0
getUserScore := func(more int) (int, error) {
executeCount++
return 98 + more, nil
}
// Cacheable Function
getUserScoreFromDbWithCache := gofnext.CacheFn1Err(getUserScore, &gofnext.Config{
TTL: time.Millisecond * 200,
CacheMap: gofnext.NewCacheRedis("redis-cache-key").ClearAll(),
})
// Execute the function multi times in parallel.
for i := 0; i < 5; i++ { //5 times
getUserScoreFromDbWithCache(1)
getUserScoreFromDbWithCache(1) // cache hit: read from redis
time.Sleep(time.Millisecond * 200)
}
if executeCount != 5 {
t.Errorf("executeCount should be 5, but get %d", executeCount)
}
}