@@ -2,19 +2,19 @@ package redis
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"sync"
6
7
"time"
7
8
8
9
"github.com/go-redis/redis"
9
10
"github.com/go-session/session"
10
- "github.com/json-iterator/go"
11
11
)
12
12
13
13
var (
14
14
_ session.ManagerStore = & managerStore {}
15
15
_ session.Store = & store {}
16
- jsonMarshal = jsoniter .Marshal
17
- jsonUnmarshal = jsoniter .Unmarshal
16
+ jsonMarshal = json .Marshal
17
+ jsonUnmarshal = json .Unmarshal
18
18
)
19
19
20
20
// NewRedisStore create an instance of a redis store
@@ -29,11 +29,6 @@ func NewRedisStore(opts *Options) session.ManagerStore {
29
29
func NewRedisStoreWithCli (cli * redis.Client ) session.ManagerStore {
30
30
return & managerStore {
31
31
cli : cli ,
32
- pool : sync.Pool {
33
- New : func () interface {} {
34
- return newStore (cli )
35
- },
36
- },
37
32
}
38
33
}
39
34
@@ -49,11 +44,6 @@ func NewRedisClusterStore(opts *ClusterOptions) session.ManagerStore {
49
44
func NewRedisClusterStoreWithCli (cli * redis.ClusterClient ) session.ManagerStore {
50
45
return & managerStore {
51
46
cli : cli ,
52
- pool : sync.Pool {
53
- New : func () interface {} {
54
- return newStore (cli )
55
- },
56
- },
57
47
}
58
48
}
59
49
@@ -68,8 +58,7 @@ type clienter interface {
68
58
}
69
59
70
60
type managerStore struct {
71
- cli clienter
72
- pool sync.Pool
61
+ cli clienter
73
62
}
74
63
75
64
func (s * managerStore ) getValue (sid string ) (string , error ) {
@@ -96,20 +85,16 @@ func (s *managerStore) parseValue(value string) (map[string]interface{}, error)
96
85
}
97
86
98
87
func (s * managerStore ) Create (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
99
- store := s .pool .Get ().(* store )
100
- store .reset (ctx , sid , expired , nil )
101
- return store , nil
88
+ return newStore (ctx , s .cli , sid , expired , nil ), nil
102
89
}
103
90
104
91
func (s * managerStore ) Update (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
105
- store := s .pool .Get ().(* store )
106
-
107
92
value , err := s .getValue (sid )
108
93
if err != nil {
109
94
return nil , err
110
95
} else if value == "" {
111
- store . reset ( ctx , sid , expired , nil )
112
- return store , nil
96
+
97
+ return newStore ( ctx , s . cli , sid , expired , nil ) , nil
113
98
}
114
99
115
100
cmd := s .cli .Expire (sid , time .Duration (expired )* time .Second )
@@ -121,9 +106,8 @@ func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (s
121
106
if err != nil {
122
107
return nil , err
123
108
}
124
- store .reset (ctx , sid , expired , values )
125
109
126
- return store , nil
110
+ return newStore ( ctx , s . cli , sid , expired , values ) , nil
127
111
}
128
112
129
113
func (s * managerStore ) Delete (_ context.Context , sid string ) error {
@@ -146,14 +130,11 @@ func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
146
130
}
147
131
148
132
func (s * managerStore ) Refresh (ctx context.Context , oldsid , sid string , expired int64 ) (session.Store , error ) {
149
- store := s .pool .Get ().(* store )
150
-
151
133
value , err := s .getValue (oldsid )
152
134
if err != nil {
153
135
return nil , err
154
136
} else if value == "" {
155
- store .reset (ctx , sid , expired , nil )
156
- return store , nil
137
+ return newStore (ctx , s .cli , sid , expired , nil ), nil
157
138
}
158
139
159
140
pipe := s .cli .TxPipeline ()
@@ -168,18 +149,25 @@ func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired
168
149
if err != nil {
169
150
return nil , err
170
151
}
171
- store .reset (ctx , sid , expired , values )
172
152
173
- return store , nil
153
+ return newStore ( ctx , s . cli , sid , expired , values ) , nil
174
154
}
175
155
176
156
func (s * managerStore ) Close () error {
177
157
return s .cli .Close ()
178
158
}
179
159
180
- func newStore (cli clienter ) * store {
160
+ func newStore (ctx context.Context , cli clienter , sid string , expired int64 , values map [string ]interface {}) * store {
161
+ if values == nil {
162
+ values = make (map [string ]interface {})
163
+ }
164
+
181
165
return & store {
182
- cli : cli ,
166
+ cli : cli ,
167
+ ctx : ctx ,
168
+ sid : sid ,
169
+ expired : expired ,
170
+ values : values ,
183
171
}
184
172
}
185
173
@@ -192,16 +180,6 @@ type store struct {
192
180
cli clienter
193
181
}
194
182
195
- func (s * store ) reset (ctx context.Context , sid string , expired int64 , values map [string ]interface {}) {
196
- if values == nil {
197
- values = make (map [string ]interface {})
198
- }
199
- s .ctx = ctx
200
- s .sid = sid
201
- s .expired = expired
202
- s .values = values
203
- }
204
-
205
183
func (s * store ) Context () context.Context {
206
184
return s .ctx
207
185
}
0 commit comments