This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
redis.go
229 lines (215 loc) · 4.34 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
223
224
225
226
227
228
229
package cachestore
import (
"encoding/json"
"errors"
"github.com/go-xorm/cachestore/redigo/redis"
"log"
)
var (
// the collection name of redis for cache adapter.
DefaultKey string = "comcmsRedis"
)
// Redis cache adapter.
type RedisCache struct {
c redis.Conn
conninfo string
key string
password string
Debug bool
}
// create new redis cache with default collection name.
func NewRedisCache(cf map[string]string) *RedisCache {
rc := &RedisCache{key: DefaultKey}
if _, ok := cf["key"]; !ok {
cf["key"] = DefaultKey
}
if _, ok := cf["password"]; !ok {
cf["password"] = ""
}
rc.password = cf["password"]
rc.key = cf["key"]
rc.conninfo = cf["conn"]
var err error
rc.c, err = rc.connectInit()
if err != nil {
log.Println("[Redis]InitErr: ", err)
rc.c = nil
}
return rc
}
// Get cache from redis.
func (rc *RedisCache) Get(key string) (interface{}, error) {
if rc.c == nil {
var err error
rc.c, err = rc.connectInit()
if err != nil {
return nil, err
}
}
val, err := rc.c.Do("HGET", rc.key, key)
if err != nil {
if rc.Debug {
log.Println("[Redis]GetErr: ", err, "Key:", key)
}
return nil, err
}
var v interface{}
x, _ := val.([]byte)
err = Decode(x, &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 *RedisCache) Put(key string, value interface{}) error {
if rc.c == nil {
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
val, err := Encode(value)
if err != nil {
if rc.Debug {
log.Println("[Redis]EncodeErr: ", err, "Key:", key)
}
return err
}
_, err = rc.c.Do("HSET", rc.key, key, val)
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 *RedisCache) Del(key string) error {
if rc.c == nil {
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, err := rc.c.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 *RedisCache) IsExist(key string) bool {
if rc.c == nil {
var err error
rc.c, err = rc.connectInit()
if err != nil {
return false
}
}
v, err := redis.Bool(rc.c.Do("HEXISTS", rc.key, key))
if err != nil {
return false
}
return v
}
// increase counter in redis.
func (rc *RedisCache) Incr(key string, delta uint64) error {
if rc.c == nil {
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, delta))
if err != nil {
return err
}
return nil
}
// decrease counter in redis.
func (rc *RedisCache) Decr(key string, delta uint64) error {
if rc.c == nil {
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -delta))
if err != nil {
return err
}
return nil
}
// clean all cache in redis. delete this redis collection.
func (rc *RedisCache) ClearAll() error {
if rc.c == nil {
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
}
_, err := rc.c.Do("DEL", rc.key)
return err
}
// start redis cache adapter.
// config is like {"key":"collection key","conn":"connection info"}
// the cache item in redis are stored forever,
// so no gc operation.
func (rc *RedisCache) 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")
}
rc.key = cf["key"]
rc.conninfo = cf["conn"]
var err error
rc.c, err = rc.connectInit()
if err != nil {
return err
}
if rc.c == nil {
return errors.New("dial tcp conn error")
}
return nil
}
// connect to redis.
func (rc *RedisCache) connectInit() (redis.Conn, error) {
c, err := redis.Dial("tcp", rc.conninfo)
if err != nil {
return nil, err
}
if rc.password != "" {
if _, err := c.Do("AUTH", rc.password); err != nil {
c.Close()
return nil, err
}
}
return c, nil
}