-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.go
More file actions
227 lines (205 loc) · 5.26 KB
/
redis.go
File metadata and controls
227 lines (205 loc) · 5.26 KB
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
// Refere:
// https://github.com/gomodule/redigo/redis
// https://github.com/garyburd/redigo
// https://github.com/boj/redistore
package redis
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/gomodule/redigo/redis"
)
var (
ErrNil = redis.ErrNil
ErrDataExist = errors.New("Data already exist")
)
// RediStore stores sessions in a redis backend.
type RediStore struct {
Pool *redis.Pool
}
func dial(network, address, password string) (redis.Conn, error) {
c, err := redis.Dial(network, address)
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
return c, err
}
// NewRediStore returns a new RediStore.
// size: maximum number of idle connections.
func NewRediStore(size int, network, address, password string) (*RediStore, error) {
return NewRediStoreWithPool(&redis.Pool{
MaxIdle: size,
IdleTimeout: 240 * time.Second,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
return dial(network, address, password)
},
})
}
func dialWithDB(network, address, password, DB string) (redis.Conn, error) {
c, err := dial(network, address, password)
if err != nil {
return nil, err
}
if _, err := c.Do("SELECT", DB); err != nil {
c.Close()
return nil, err
}
return c, err
}
// NewRediStoreWithDB - like NewRedisStore but accepts `DB` parameter to select
// redis DB instead of using the default one ("0")
func NewRediStoreWithDB(size int, network, address, password, DB string) (*RediStore, error) {
return NewRediStoreWithPool(&redis.Pool{
MaxIdle: size,
IdleTimeout: 240 * time.Second,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
return dialWithDB(network, address, password, DB)
},
})
}
func NewRediStoreWithURL(ctx context.Context, size int, rawurl string, options ...redis.DialOption) (*RediStore, error) {
return NewRediStoreWithPool(&redis.Pool{
MaxIdle: size,
IdleTimeout: 240 * time.Second,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
return redis.DialURLContext(ctx, rawurl, options...)
},
})
}
// NewRediStoreWithPool instantiates a RediStore with a *redis.Pool passed in.
func NewRediStoreWithPool(pool *redis.Pool) (*RediStore, error) {
rs := &RediStore{
Pool: pool,
}
_, err := rs.ping()
return rs, err
}
// Close closes the underlying *redis.Pool
func (s *RediStore) Close() error {
return s.Pool.Close()
}
// ping does an internal ping against a server to check if it is alive.
func (s *RediStore) ping() (bool, error) {
conn := s.Pool.Get()
defer conn.Close()
data, err := conn.Do("PING")
if err != nil || data == nil {
return false, err
}
return (data == "PONG"), nil
}
func (s *RediStore) Do(cmd string, args ...interface{}) (interface{}, error) {
conn := s.Pool.Get()
defer conn.Close()
return conn.Do(cmd, args...)
}
// Delete removes the session from redis.
func (s *RediStore) Delete(key string) error {
conn := s.Pool.Get()
defer conn.Close()
if _, err := conn.Do("DEL", key); err != nil {
return err
}
return nil
}
// When the next corsor return is 0, it means that there is no more data
func (s *RediStore) ScanKey(cursor int64, pattern string, limit int) (int64, [][]byte, error) {
conn := s.Pool.Get()
defer conn.Close()
val, err := redis.Values(conn.Do("SCAN", cursor, "MATCH", pattern, "COUNT", limit))
if err != nil {
return 0, nil, err
}
nextCursor, err := redis.Int64(val[0], nil)
if err != nil {
return 0, nil, err
}
result, err := redis.ByteSlices(val[1], nil)
if err != nil {
return 0, nil, err
}
if nextCursor == 0 && len(result) == 0 {
return 0, result, ErrNil
}
return nextCursor, result, err
}
// save stores the session in redis.
// store data with json format
// age -- 0 for no expired.
func (s *RediStore) PutJSON(key string, val interface{}, age time.Duration) error {
b, err := json.Marshal(val)
if err != nil {
return err
}
conn := s.Pool.Get()
defer conn.Close()
if err = conn.Err(); err != nil {
return err
}
overdue := int64(age / time.Millisecond)
if overdue > 0 {
_, err = conn.Do("SET", key, b, "PX", overdue)
return err
}
_, err = conn.Do("SET", key, b)
return err
}
// scan the data to a json interface.
func (s *RediStore) ScanJSON(key string, val interface{}) error {
conn := s.Pool.Get()
defer conn.Close()
if err := conn.Err(); err != nil {
return err
}
reply, err := conn.Do("GET", key)
if err != nil {
return err
}
out, err := redis.Bytes(reply, err)
if err != nil {
return err
}
return json.Unmarshal(out, val)
}
func (s *RediStore) Get(key string) (interface{}, error) {
conn := s.Pool.Get()
defer conn.Close()
if err := conn.Err(); err != nil {
return nil, err
}
return conn.Do("GET", key)
}
func (s *RediStore) Put(key string, val interface{}, age time.Duration) error {
conn := s.Pool.Get()
defer conn.Close()
if err := conn.Err(); err != nil {
return err
}
var err error
overdue := int64(age / time.Millisecond)
if overdue > 0 {
_, err = conn.Do("SET", key, val, "PX", overdue)
return err
}
_, err = conn.Do("SET", key, val)
return err
}