forked from go-session/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
247 lines (210 loc) · 5.09 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package redis
import (
"context"
"encoding/json"
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/go-session/session"
)
var (
_ session.ManagerStore = &managerStore{}
_ session.Store = &store{}
jsonMarshal = json.Marshal
jsonUnmarshal = json.Unmarshal
)
// NewRedisStore create an instance of a redis store
func NewRedisStore(opts *redis.Options, prefix ...string) session.ManagerStore {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisStoreWithCli(
redis.NewClient(opts),
prefix...,
)
}
// NewRedisStoreWithCli create an instance of a redis store
func NewRedisStoreWithCli(cli *redis.Client, prefix ...string) session.ManagerStore {
store := &managerStore{
cli: newClient(cli),
}
if len(prefix) > 0 {
store.prefix = prefix[0]
}
return store
}
// NewRedisClusterStore create an instance of a redis cluster store
func NewRedisClusterStore(opts *redis.ClusterOptions, prefix ...string) session.ManagerStore {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisClusterStoreWithCli(
redis.NewClusterClient(opts),
prefix...,
)
}
// NewRedisClusterStoreWithCli create an instance of a redis cluster store
func NewRedisClusterStoreWithCli(cli *redis.ClusterClient, prefix ...string) session.ManagerStore {
store := &managerStore{
cli: newClusterClient(cli),
}
if len(prefix) > 0 {
store.prefix = prefix[0]
}
return store
}
type managerStore struct {
cli clienter
prefix string
}
func (s *managerStore) getKey(key string) string {
return s.prefix + key
}
func (s *managerStore) getValue(sid string) (string, error) {
cmd := s.cli.Get(s.getKey(sid))
if err := cmd.Err(); err != nil {
if err == redis.Nil {
return "", nil
}
return "", err
}
return cmd.Val(), nil
}
func (s *managerStore) parseValue(value string) (map[string]interface{}, error) {
var values map[string]interface{}
if len(value) > 0 {
err := jsonUnmarshal([]byte(value), &values)
if err != nil {
return nil, err
}
}
return values, nil
}
func (s *managerStore) Create(ctx context.Context, sid string, expired int64) (session.Store, error) {
return newStore(ctx, s, sid, expired, nil), nil
}
func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (session.Store, error) {
value, err := s.getValue(sid)
if err != nil {
return nil, err
} else if value == "" {
return newStore(ctx, s, sid, expired, nil), nil
}
cmd := s.cli.Expire(s.getKey(sid), time.Duration(expired)*time.Second)
if err = cmd.Err(); err != nil {
return nil, err
}
values, err := s.parseValue(value)
if err != nil {
return nil, err
}
return newStore(ctx, s, sid, expired, values), nil
}
func (s *managerStore) Delete(_ context.Context, sid string) error {
if ok, err := s.Check(nil, sid); err != nil {
return err
} else if !ok {
return nil
}
cmd := s.cli.Del(s.getKey(sid))
return cmd.Err()
}
func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
cmd := s.cli.Exists(s.getKey(sid))
if err := cmd.Err(); err != nil {
return false, err
}
return cmd.Val() > 0, nil
}
func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired int64) (session.Store, error) {
value, err := s.getValue(oldsid)
if err != nil {
return nil, err
} else if value == "" {
return newStore(ctx, s, sid, expired, nil), nil
}
pipe := s.cli.TxPipeline()
pipe.Set(ctx, s.getKey(sid), value, time.Duration(expired)*time.Second)
pipe.Del(ctx, s.getKey(oldsid))
_, err = pipe.Exec(ctx)
if err != nil {
return nil, err
}
values, err := s.parseValue(value)
if err != nil {
return nil, err
}
return newStore(ctx, s, sid, expired, values), nil
}
func (s *managerStore) Close() error {
return s.cli.Close()
}
func newStore(ctx context.Context, ms *managerStore, sid string, expired int64, values map[string]interface{}) *store {
if values == nil {
values = make(map[string]interface{})
}
return &store{
ms: ms,
ctx: ctx,
sid: sid,
expired: expired,
values: values,
}
}
type store struct {
sync.RWMutex
ms *managerStore
ctx context.Context
sid string
expired int64
values map[string]interface{}
}
func (s *store) Context() context.Context {
return s.ctx
}
func (s *store) SessionID() string {
return s.sid
}
func (s *store) Set(key string, value interface{}) {
s.Lock()
s.values[key] = value
s.Unlock()
}
func (s *store) Get(key string) (interface{}, bool) {
s.RLock()
defer s.RUnlock()
val, ok := s.values[key]
return val, ok
}
func (s *store) Delete(key string) interface{} {
s.RLock()
v, ok := s.values[key]
s.RUnlock()
if ok {
s.Lock()
delete(s.values, key)
s.Unlock()
}
return v
}
func (s *store) Flush() error {
s.Lock()
s.values = make(map[string]interface{})
s.Unlock()
return s.Save()
}
func (s *store) Save() error {
var value string
s.RLock()
if len(s.values) > 0 {
buf, err := jsonMarshal(s.values)
if err != nil {
s.RUnlock()
return err
}
value = string(buf)
}
s.RUnlock()
cmd := s.ms.cli.Set(s.ms.getKey(s.sid), value, time.Duration(s.expired)*time.Second)
return cmd.Err()
}