-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
222 lines (199 loc) · 4.73 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright 2016 Wenhui Shen <www.webx.top>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cachestore
import (
"encoding/json"
"errors"
"log"
"strconv"
"time"
"github.com/webx-top/webx/lib/cachestore/redigo/redis"
)
var (
// the collection name of redis for cache adapter.
DefaultKey string = "WebxRedis"
)
// Redis cache adapter.
type Redis struct {
p *redis.Pool // redis connection pool
conninfo string
dbnum int
key string
Debug bool
LifeTime int32
}
// create new redis cache with default collection name.
func NewRedis(cf map[string]string, lifeTime int32) *Redis {
rc := &Redis{key: DefaultKey}
if _, ok := cf["key"]; !ok {
cf["key"] = DefaultKey
}
rc.key = cf["key"]
rc.conninfo = cf["conn"]
rc.LifeTime = lifeTime
rc.connectInit()
return rc
}
// actually do the redis cmds
func (rc *Redis) do(commandName string, args ...interface{}) (reply interface{}, err error) {
if rc.p == nil {
rc.connectInit()
}
c := rc.p.Get()
defer c.Close()
return c.Do(commandName, args...)
}
// Get cache from redis.
func (rc *Redis) Get(key string) (interface{}, error) {
val, err := rc.do("GET", key)
if err != nil {
if rc.Debug {
log.Println("[Redis]GetErr: ", err, "Key:", key)
}
return nil, err
}
var v interface{}
err = Decode(val.([]byte), &v)
if err != nil {
if rc.Debug {
log.Println("[Redis]DecodeErr: ", err, "Key:", key)
}
return nil, err
}
if rc.Debug {
log.Println("[Redis]Get: ", key)
}
return v, err
}
// put cache to redis.
// timeout is ignored.
func (rc *Redis) Put(key string, value interface{}) error {
val, err := Encode(value)
if err != nil {
if rc.Debug {
log.Println("[Redis]EncodeErr: ", err, "Key:", key)
}
return err
}
if _, err = rc.do("SETEX", key, rc.LifeTime, val); err != nil {
return err
}
_, err = rc.do("HSET", rc.key, key, true)
if err != nil {
if rc.Debug {
log.Println("[Redis]PutErr: ", err, "Key:", key)
}
return err
}
if rc.Debug {
log.Println("[Redis]Put: ", key)
}
return err
}
// delete cache in redis.
func (rc *Redis) Del(key string) error {
var err error
if _, err = rc.do("DEL", key); err != nil {
return err
}
_, err = rc.do("HDEL", rc.key, key)
if err != nil {
if rc.Debug {
log.Println("[Redis]DelErr: ", err, "Key:", key)
}
return err
}
if rc.Debug {
log.Println("[Redis]Del: ", key)
}
return err
}
// check cache exist in redis.
func (rc *Redis) IsExist(key string) bool {
v, err := redis.Bool(rc.do("EXISTS", key))
if err != nil {
return false
}
if v == false {
if _, err = rc.do("HDEL", rc.key, key); err != nil {
return false
}
}
return v
}
// increase counter in redis.
func (rc *Redis) Incr(key string, delta uint64) error {
_, err := redis.Bool(rc.do("INCRBY", key, delta))
return err
}
// decrease counter in redis.
func (rc *Redis) Decr(key string, delta uint64) error {
_, err := redis.Bool(rc.do("INCRBY", key, delta))
return err
}
// clean all cache in redis. delete this redis collection.
func (rc *Redis) ClearAll() error {
cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
if err != nil {
return err
}
for _, str := range cachedKeys {
if _, err = rc.do("DEL", str); err != nil {
return err
}
}
_, err = rc.do("DEL", rc.key)
return err
}
// start redis cache adapter.
// config is like {"key":"collection key","conn":"connection info","dbnum":"0"}
// the cache item in redis are stored forever,
// so no gc operation.
func (rc *Redis) Connect(config string) error {
var cf map[string]string
json.Unmarshal([]byte(config), &cf)
if _, ok := cf["key"]; !ok {
cf["key"] = DefaultKey
}
if _, ok := cf["conn"]; !ok {
return errors.New("config has no conn key")
}
if _, ok := cf["dbnum"]; !ok {
cf["dbnum"] = "0"
}
rc.key = cf["key"]
rc.conninfo = cf["conn"]
rc.dbnum, _ = strconv.Atoi(cf["dbnum"])
rc.connectInit()
c := rc.p.Get()
defer c.Close()
return c.Err()
}
// connect to redis.
func (rc *Redis) connectInit() {
dialFunc := func() (c redis.Conn, err error) {
c, err = redis.Dial("tcp", rc.conninfo)
_, selecterr := c.Do("SELECT", rc.dbnum)
if selecterr != nil {
c.Close()
return nil, selecterr
}
return
}
// initialize a new pool
rc.p = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 180 * time.Second,
Dial: dialFunc,
}
}