-
Notifications
You must be signed in to change notification settings - Fork 0
/
out_redis.go
184 lines (164 loc) · 3.54 KB
/
out_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package g2cache
import (
"fmt"
"github.com/gomodule/redigo/redis"
"sync"
)
var DefaultPubSubRedisChannel = "g2cache-pubsub-channel"
var DefaultRedisConf RedisConf
var DefaultPubSubRedisConf RedisConf
func init() {
DefaultRedisConf.DSN = "127.0.0.1:6379"
DefaultRedisConf.MaxConn = 10
DefaultPubSubRedisConf = DefaultRedisConf
}
type RedisCache struct {
pool *redis.Pool
pubsubPool *redis.Pool
stop chan struct{}
stopOnce sync.Once
}
type RedisConf struct {
DSN string
Pwd string
DB int
MaxConn int
}
func NewRedisCache() (*RedisCache, error) {
pool, err := GetRedisPool(&DefaultRedisConf)
if err != nil {
return nil, fmt.Errorf("redis pool init err %v", err)
}
var pubsubPool *redis.Pool
if OutCachePubSub {
pubsubPool, err = GetRedisPool(&DefaultPubSubRedisConf)
if err != nil {
return nil, fmt.Errorf("redis pubsubPool init err %v", err)
}
}
c := &RedisCache{
pool: pool,
pubsubPool: pubsubPool,
stop: make(chan struct{}, 1),
}
return c, nil
}
func (r *RedisCache) Del(key string) error {
select {
case <-r.stop:
return OutStorageClose
default:
}
return RedisDelKey(key, r.pool)
}
func (r *RedisCache) Close() {
r.stopOnce.Do(r.close)
}
func (r *RedisCache) close() {
close(r.stop)
r.pool.Close()
}
func (r *RedisCache) Set(key string, obj *Entry) error {
select {
case <-r.stop:
return OutStorageClose
default:
}
str, err := json.MarshalToString(obj)
if err != nil {
return err
}
// out storage should set Expiration time
rdsTtl := obj.GetExpireTTL()
return RedisSetString(key, str, int(rdsTtl), r.pool)
}
func (r *RedisCache) DistributedEnable() bool {
return true
}
func (r *RedisCache) Subscribe(ch chan<- *ChannelMeta) error {
select {
case <-r.stop:
return OutStorageClose
default:
}
conn := r.pubsubPool.Get()
defer conn.Close()
psc := redis.PubSubConn{Conn: conn}
if err := psc.Subscribe(DefaultPubSubRedisChannel); err != nil {
LogErrF("rds subscribe channel=%v, err=%v\n", DefaultPubSubRedisChannel, err)
return err
}
if CacheDebug {
LogDebugF("rds subscribe channel=%v start ...\n", DefaultPubSubRedisChannel)
}
LOOP:
for {
select {
case <-r.stop:
return OutStorageClose
default:
}
switch v := psc.Receive().(type) {
case redis.Message:
meta := &ChannelMeta{}
err := json.Unmarshal(v.Data, meta)
if err != nil || meta.Key == "" {
LogErrF("rds subscribe Unmarshal data: %+v,err:%v\n", v.Data, err)
continue
}
select {
case <-r.stop:
return OutStorageClose
default:
}
ch <- meta
case error:
LogErrF("rds subscribe receive error, msg=%v\n", v)
break LOOP
}
}
return nil
}
func (r *RedisCache) Get(key string, obj interface{}) (*Entry, bool, error) {
select {
case <-r.stop:
return nil, false, OutStorageClose
default:
}
str, err := RedisGetString(key, r.pool)
if err != nil {
if err == redis.ErrNil {
return nil, false, nil
}
return nil, false, err
}
if str == "" {
return nil, false, nil
}
var e Entry
e.Value = obj // Save the reflection structure of obj
err = json.UnmarshalFromString(str, &e)
if err != nil {
return nil, false, err
}
return &e, true, err
}
func (r *RedisCache) Publish(gid, key string, action int8, value *Entry) error {
select {
case <-r.stop:
return OutStorageClose
default:
}
meta := ChannelMeta{
Gid: gid,
Key: key,
Action: action,
Data: value,
}
s, err := json.MarshalToString(meta)
if err != nil {
return err
}
return RedisPublish(DefaultPubSubRedisChannel, s, r.pubsubPool)
}
func (r *RedisCache) ThreadSafe() {}