124 lines
3.0 KiB
Markdown
124 lines
3.0 KiB
Markdown
### 使用方法
|
||
```
|
||
|
||
1、import (
|
||
"gitlab.finogeeks.club:finclip-backend-v2/finclip-redis"
|
||
"github.com/go-redis/redis/v8"//这里初始化时需要传入相应的配置
|
||
)
|
||
|
||
2、调用接口进行初始化: fcredis.Init(mode,opt,ext...)
|
||
//mode--redis模式,可选项为: single--单例,sentinel--主从,cluster--集群
|
||
//opt--对应的redis配置,需要根据不同的模式选用不同的go-redis option
|
||
//ext参数用来为主从模式指定是否采用"主节点写从节点读"的读写策略(默认采用)。不采用该策略时ext参数填为: fcredis.SentinelNormalMode
|
||
|
||
3、调用相应的接口
|
||
|
||
```
|
||
|
||
### example
|
||
#### single模式
|
||
```
|
||
func main() {
|
||
ctx := context.Background()
|
||
redisMode = "single"
|
||
redisAddr = "redis://proxy.redis-cluster:6379/10"
|
||
opt, err := goRedis.ParseURL(redisAddr)
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
opt.Password = ""
|
||
opt.PoolSize = 20
|
||
err = fcredis.Init(redisMode, opt)
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
|
||
//初始化之后就可以用了
|
||
key := "test_key_111"
|
||
_, err = fcredis.Set(ctx, key, "test_value", 0)
|
||
if err != nil {
|
||
common.GLog.Errorf("fcredis set err:%v", err.Error())
|
||
return
|
||
}
|
||
result, err := fcredis.GetString(ctx, key)
|
||
fmt.Printf("fcredis get info:%+v,err:%v\n", result, err)
|
||
}
|
||
```
|
||
|
||
#### sentinel模式
|
||
```
|
||
func main() {
|
||
ctx := context.Background()
|
||
//主写从读模式
|
||
err := fcredis.Init("sentinel", &goRedis.FailoverOptions{
|
||
MasterName: "mymaster",
|
||
SentinelAddrs: []string{"redis-cluster-redis-cluster-sentinel.redis-cluster:26379"},
|
||
})
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
|
||
key := "test_key_111"
|
||
_, err = fcredis.Set(ctx, key, "test_value", 0)
|
||
if err != nil {
|
||
fmt.Printf("fcredis set err:%v\n", err.Error())
|
||
return
|
||
}
|
||
|
||
result, err := fcredis.GetString(ctx, key)
|
||
fmt.Printf("fcredis get info:%+v,err:%v\n", result, err)
|
||
}
|
||
```
|
||
|
||
### cluster模式
|
||
```
|
||
func main() {
|
||
ctx := context.Background()
|
||
err := fcredis.Init("cluster", &goRedis.ClusterOptions{
|
||
Addrs: strings.Split(config.Cfg.RedisAddr, ","),
|
||
Password: config.Cfg.RedisPassword,
|
||
})
|
||
if err != nil {
|
||
fmt.Println("err:", err)
|
||
return
|
||
}
|
||
|
||
key := "test_key_111"
|
||
_, err = fcredis.Set(ctx, key, "test_value", 0)
|
||
if err != nil {
|
||
fmt.Printf("fcredis set err:%v\n", err.Error())
|
||
return
|
||
}
|
||
|
||
result, err := fcredis.GetString(ctx, key)
|
||
fmt.Printf("fcredis get info:%+v,err:%v\n", result, err)
|
||
}
|
||
```
|
||
|
||
### other
|
||
这里初始化之后也可以直接使用fcredis提供的client接口,直接调用go-redis的接口
|
||
进行自定义的操作,示例如下:
|
||
```
|
||
func main() {
|
||
ctx := context.Background()
|
||
//主写从读模式
|
||
err := fcredis.Init("sentinel", &goRedis.FailoverOptions{
|
||
MasterName: "mymaster",
|
||
SentinelAddrs: []string{"redis-cluster-redis-cluster-sentinel.redis-cluster:26379"},
|
||
})
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
|
||
key := "test_key_111"
|
||
_, err = fcredis.Client().Set(ctx, key, "test_value", 0).Result()
|
||
if err != nil {
|
||
fmt.Printf("fcredis set err:%v\n", err.Error())
|
||
return
|
||
}
|
||
|
||
result, err := fcredis.Client().Get(ctx, key).String()
|
||
fmt.Printf("fcredis get info:%+v,err:%v\n", result, err)
|
||
}
|
||
|
||
``` |