-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_test.go
110 lines (92 loc) · 2.3 KB
/
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
package cache_test
import (
"context"
"log"
"os"
"testing"
"time"
cache "github.com/pobyzaarif/go-cache"
redis "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
)
func TestRedisCache(t *testing.T) {
timeNow := time.Now()
tc := map[string]interface{}{
"a": 1,
"b": 2.5,
"c": "3",
"d": true,
"f": timeNow,
"g": []string{"1", "2", "3"},
"h": map[string]int{"1": 2, "3": 4},
"i": 1,
}
// Create a new Redis client
redisURL := os.Getenv("GOCACHE_REDIS_URL")
if redisURL == "" {
redisURL = "redis://localhost:6379/0?protocol=3"
}
opts, err := redis.ParseURL(redisURL)
if err != nil {
log.Fatalf("Could not connect to Redis: %v", err)
}
rdb := redis.NewClient(opts)
// Ping the Redis server to check the connection
_, err = rdb.Ping(context.TODO()).Result()
if err != nil {
log.Fatalf("Could not connect to Redis: %v", err)
}
redisCache := cache.NewRedisCacheRepository(rdb) // increase 1 the size to avoid overcapacity of the cache because lru, lfu, and arc will remove unpopular keys automatically based on each algorithm
assert.NoError(t, err)
var aa int
err = redisCache.Get("a", &aa)
assert.NoError(t, err)
assert.Equal(t, 0, aa)
for k, v := range tc {
err := redisCache.Set(k, v, 5*time.Minute)
assert.NoError(t, err)
}
// a test
err = redisCache.Get("a", &aa)
assert.NoError(t, err)
assert.Equal(t, tc["a"], aa)
var aaa int
redisCache.Delete("a")
redisCache.Get("a", &aaa)
assert.Equal(t, 0, aaa)
// b test
var bb float64
err = redisCache.Get("b", &bb)
assert.NoError(t, err)
assert.Equal(t, tc["b"], bb)
// c test
var cc string
err = redisCache.Get("c", &cc)
assert.NoError(t, err)
assert.Equal(t, tc["c"], cc)
// d test
var dd bool
err = redisCache.Get("d", &dd)
assert.NoError(t, err)
assert.Equal(t, tc["d"], dd)
// f test
var ff time.Time
err = redisCache.Get("f", &ff)
assert.NoError(t, err)
assert.WithinDuration(t, tc["f"].(time.Time), ff, time.Second)
// g test
var gg []string
err = redisCache.Get("g", &gg)
assert.NoError(t, err)
assert.Equal(t, tc["g"], gg)
// h test
var hh map[string]int
err = redisCache.Get("h", &hh)
assert.NoError(t, err)
assert.Equal(t, tc["h"], hh)
// h test negative case
var hhh map[int]string
err = redisCache.Get("h", &hhh)
assert.Error(t, err)
assert.NotEqual(t, tc["h"], hhh)
}