-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
93 lines (76 loc) · 2 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
package cache
import (
"context"
"fmt"
"testing"
"github.com/AImager/gocache/config"
)
const (
cacheKey = "test_cache:a:%d:b:%s"
)
func getRcConn() config.ClientI {
client, _ := GetClient(config.ClientConfig{
Addr: "127.0.0.1:6379",
ClientType: config.Goredis,
})
return client
}
func testDb(ctx context.Context, a int, b string) (c int, err error) {
return 1, nil
}
func testUpdateDb(ctx context.Context, a int, b string) (err error) {
return nil
}
func TestDb(t *testing.T) {
a := 1
b := "string123"
c := &Cache{Client: getRcConn()}
decoratedFunc := testDb
c.CacheWithFunc(context.TODO(), config.CacheConfig{
Key: fmt.Sprintf(cacheKey, a, b),
Expire: 220,
}, &decoratedFunc, testDb)
fmt.Println(decoratedFunc(context.TODO(), a, b))
}
func TestUpdateDb(t *testing.T) {
a := 1
b := "string123"
c := &Cache{Client: getRcConn()}
decoratedFunc := testUpdateDb
c.CacheDelWithFunc(context.TODO(), config.CacheDelConfig{
Key: fmt.Sprintf(cacheKey, a, b),
}, &decoratedFunc, testUpdateDb)
fmt.Println(decoratedFunc(context.TODO(), a, b))
}
type hand struct{}
func (h hand) testDb(ctx context.Context, a int, b string) (c int, err error) {
return 1, nil
}
func (h hand) testUpdateDb(ctx context.Context, a int, b string) (err error) {
return nil
}
func TestMethodDb(t *testing.T) {
a := 1
b := "string123"
h := hand{}
c := &Cache{Client: getRcConn()}
decoratedFunc := h.testDb
c.CacheWithFunc(context.TODO(), config.CacheConfig{
Key: fmt.Sprintf(cacheKey, a, b),
Expire: 220,
}, &decoratedFunc, testDb)
fmt.Println(decoratedFunc(context.TODO(), a, b))
fmt.Println(h.testDb(context.TODO(), a, b))
}
func TestMethodUpdateDb(t *testing.T) {
a := 1
b := "string123"
h := hand{}
c := &Cache{Client: getRcConn()}
decoratedFunc := h.testUpdateDb
c.CacheDelWithFunc(context.TODO(), config.CacheDelConfig{
Key: fmt.Sprintf(cacheKey, a, b),
}, &decoratedFunc, testUpdateDb)
fmt.Println(decoratedFunc(context.TODO(), a, b))
fmt.Println(h.testUpdateDb(context.TODO(), a, b))
}