-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis6_test.go
98 lines (86 loc) · 2.37 KB
/
redis6_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
package xcache_test
import (
"bytes"
"context"
"fmt"
"time"
"github.com/actforgood/xcache"
"github.com/actforgood/xconf"
)
func init() {
var _ xcache.Cache = (*xcache.Redis6)(nil) // test Redis6 is a Cache
}
func ExampleRedis6() {
cache := xcache.NewRedis6(xcache.RedisConfig{
Addrs: []string{"127.0.0.1:6379"},
})
ctx := context.Background()
key := "example-redis"
value := []byte("Hello Redis Cache")
ttl := 10 * time.Minute
// save a key for 10 minutes
if err := cache.Save(ctx, key, value, ttl); err != nil {
fmt.Println("could not save Redis cache key: " + err.Error())
}
// load the key's value
if value, err := cache.Load(ctx, key); err != nil {
fmt.Println("could not get Redis cache key: " + err.Error())
} else {
fmt.Println(string(value))
}
// close the cache when no needed anymore/at your application shutdown.
if err := cache.Close(); err != nil {
fmt.Println("could not close Redis cache: " + err.Error())
}
// should output:
// Hello Redis Cache
}
func ExampleRedis6_withXConf() {
// Setup the config our application will use (here used a NewFlattenLoader over a json source)
// You can use whatever config loader suits you as long as needed xcache keys are present.
config, err := xconf.NewDefaultConfig(
xconf.NewFlattenLoader(xconf.JSONReaderLoader(bytes.NewReader([]byte(`{
"xcache": {
"redis": {
"addrs": [
"127..0.0.1:6379"
],
"db": 0,
"auth": {
"username": "",
"password": ""
},
"timeout": {
"dial": "5s",
"read": "6s",
"write": "10s"
},
"cluster": {
"readonly": true
},
"failover": {
"mastername": "mymaster",
"auth": {
"username": "",
"password": ""
}
}
}
}
}`)))),
xconf.DefaultConfigWithReloadInterval(5*time.Minute),
)
if err != nil {
panic(err)
}
defer config.Close()
// Initialize the cache our application will use.
cache := xcache.NewRedis6WithConfig(config)
defer cache.Close()
// From this point forward you can do whatever you want with the cache.
// Any config that gets changed, cache will reconfigure itself in a time up to reload interval (5 mins here)
// without the need of restarting/redeploying our application.
// For example, let's assume we notice a lot of timeout errors,
// until we figure it out what's happening with our Redis server,
// we can increase read/write timeouts.
}