-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_redis.go
46 lines (37 loc) · 885 Bytes
/
db_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
package app
import (
"errors"
"github.com/cnlisea/ant/app/db/redis"
)
func (a *App) DBRedisRegister(name string,
password string, addr string, port uint16, db int,
active int, idle int, idleTimeout int) error {
if a.redis != nil && a.redis[name] != nil {
return ErrRedisInstanceAlreadyExisted
}
client, err := redis.NewClient(password, addr, port, db, active, idle, idleTimeout)
if err != nil {
return errors.New("redis client new fail, err:" + err.Error())
}
if a.redis == nil {
a.redis = make(map[string]*redis.Client, 1)
}
a.redis[name] = client
return nil
}
func (a *App) DBRedisInstance(name ...string) *redis.Client {
if a.redis == nil {
return nil
}
var key string
if name != nil && len(name) > 0 {
key = name[0]
}
return a.redis[key]
}
func (a *App) DBRedisAllClose() {
ctx := a.Context()
for _, c := range a.redis {
c.Close(ctx)
}
}