-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
78 lines (67 loc) · 1.44 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
package cache
import (
"context"
"testing"
"time"
redisV9 "github.com/redis/go-redis/v9"
)
func TestCache(t *testing.T) {
rdb := redisV9.NewClient(&redisV9.Options{
Addr: "127.0.0.1:6379",
})
key := "key1"
c := New(context.Background(), rdb, WithPrefix("test"), WithExpired(3600*time.Second))
c.redis.Set(context.Background(), key, "value", c.expired)
v, err := c.redis.Get(context.Background(), key).Result()
if err != nil {
t.Error(err)
}
if v != "value" {
t.Errorf("expected value to be 'value', got '%s'", v)
}
c.redis.Del(context.Background(), key)
_, err = c.redis.Get(context.Background(), key).Result()
if err != nil {
if err != redisV9.Nil {
t.Error(err)
}
}
// tag
key = "key:user:1"
tags := []string{
"tag:all",
"tag:user:1",
}
type Data struct {
Name string
Age int64
}
data1 := &Data{Name: "jack", Age: 18}
err = c.Tag([]string{
"tag:all",
"tag:user:1",
}...).Set(context.Background(), key, data1)
if err != nil {
t.Error(err)
}
data2 := &Data{}
err = c.Get(context.Background(), key, data2)
if err != nil {
t.Error(err)
}
if data2.Name != "jack" {
t.Errorf("expected name to be 'jack', got '%s'", data2.Name)
}
err = c.Tag(tags...).Flush(context.Background())
if err != nil {
t.Error(err)
}
data3 := &Data{}
err = c.Get(context.Background(), key, data3)
if err != nil {
t.Error(err)
}
if data3.Name == "jack" {
t.Errorf("expected name to be '', got '%s'", data3.Name)
}
}