/
redis.go
49 lines (42 loc) · 1020 Bytes
/
redis.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
package groot
import (
"fmt"
"github.com/go-redsync/redsync/v4"
"github.com/go-redsync/redsync/v4/redis/goredis/v9"
goredislib "github.com/redis/go-redis/v9"
)
var client *goredislib.Client
var rs *redsync.Redsync
func InitRedis() error {
if redisCfg.Addr == "" {
fmt.Println("没有配置redis信息!")
Debug("没有配置redis信息!")
return nil
}
client = goredislib.NewClient(&goredislib.Options{
Addr: redisCfg.Addr,
Password: redisCfg.Password, // no password set
DB: redisCfg.Db, // use default DB
})
pool := goredis.NewPool(client) // or, pool := redigo.NewPool(...)
// Create an instance of redisync to be used to obtain a mutual exclusion
// lock.
rs = redsync.New(pool)
return nil
}
func GetRedisClient() *goredislib.Client {
if client == nil {
lock.Lock()
defer lock.Unlock()
if client == nil {
InitRedis()
}
}
if client == nil {
panic("redis配置不正确为空!")
}
return client
}
func GetRedisSync() *redsync.Redsync {
return rs
}