-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
96 lines (82 loc) · 1.67 KB
/
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
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
package redisManager
import (
"encoding/json"
"github.com/go-redis/redis"
)
type ConnectionManager struct {
connList map[string]*Connection
}
type Connection struct {
client *redis.Client
options *redis.Options
}
func NewConnectionManager() *ConnectionManager {
m := ConnectionManager{
connList: make(map[string]*Connection),
}
return &m
}
func (m *ConnectionManager) Add(name string, options *redis.Options) {
m.connList[name] = &Connection{
options: options,
}
}
func (m *ConnectionManager) Remove(name string) {
delete(m.connList, name)
}
func (m *ConnectionManager) Get(name string) *Connection {
con, ok := m.connList[name]
if !ok {
return nil
}
return con
}
func (m *ConnectionManager) Exist(name string) bool {
con := m.Get(name)
if con == nil {
return false
}
return true
}
func (m *ConnectionManager) Length() int {
return len(m.connList)
}
func (m ConnectionManager) String() string {
type tmpT struct {
HasClient bool
Options struct{
Addr string
Password string
DB int
}
}
list := make(map[string]tmpT)
for k, v := range m.connList {
tmp := tmpT{}
if v.client != nil {
tmp.HasClient = true
}
tmp.Options.Addr = (*v).options.Addr
tmp.Options.Password = (*v).options.Password
tmp.Options.DB = (*v).options.DB
list[k] = tmp
}
bytes, _ := json.Marshal(list)
return string(bytes)
}
func (c *Connection) GetRedisClient() *redis.Client {
if c.client == nil {
c.ReconnectRedisClient()
}
return c.client
}
func (c *Connection) ReconnectRedisClient() {
c.client = redis.NewClient(c.options)
}
func (c *Connection) DisconnectRedisClient() bool {
if c.client != nil {
c.client.Close()
c.client = nil
}
return true
}