redis.go
797 字节
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
package xredis
import (
"context"
"fmt"
"strconv"
"github.com/go-redis/redis/v8"
"github.com/go-redsync/redsync/v4"
"github.com/go-redsync/redsync/v4/redis/goredis/v8"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
)
var rdb *redis.Client
var rsync *redsync.Redsync
func init() {
host := constant.REDIS_HOST
port := constant.REDIS_PORT
auth := constant.REDIS_AUTH
index := 0
if constant.REDIS_DB != "" {
indexDb := constant.REDIS_DB
index, _ = strconv.Atoi(indexDb)
}
rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%v:%v", host, port),
Password: auth,
DB: index, // use default DB
})
res := rdb.Ping(context.TODO())
if res.Err() != nil {
panic("redis not found")
}
pool := goredis.NewPool(rdb)
rsync = redsync.New(pool)
}