@@ -17,21 +17,29 @@ var (
17
17
jsonUnmarshal = jsoniter .Unmarshal
18
18
)
19
19
20
- // NewRedisStore Create an instance of a redis store
20
+ // NewRedisStore create an instance of a redis store
21
21
func NewRedisStore (opt * Options ) session.ManagerStore {
22
22
if opt == nil {
23
- panic ("Option cannot be nil" )
23
+ panic ("option cannot be nil" )
24
24
}
25
25
return & managerStore {cli : redis .NewClient (opt .redisOptions ())}
26
26
}
27
27
28
- // NewRedisStoreWithCli Create an instance of a redis store
28
+ // NewRedisStoreWithCli create an instance of a redis store
29
29
func NewRedisStoreWithCli (cli * redis.Client ) session.ManagerStore {
30
- return & managerStore {cli : cli }
30
+ return & managerStore {
31
+ cli : cli ,
32
+ pool : sync.Pool {
33
+ New : func () interface {} {
34
+ return newDefaultStore (cli )
35
+ },
36
+ },
37
+ }
31
38
}
32
39
33
40
type managerStore struct {
34
- cli * redis.Client
41
+ cli * redis.Client
42
+ pool sync.Pool
35
43
}
36
44
37
45
func (s * managerStore ) getValue (sid string ) (string , error ) {
@@ -54,35 +62,24 @@ func (s *managerStore) parseValue(value string) (map[string]interface{}, error)
54
62
return nil , err
55
63
}
56
64
}
57
-
58
- if values == nil {
59
- values = make (map [string ]interface {})
60
- }
61
65
return values , nil
62
66
}
63
67
64
68
func (s * managerStore ) Create (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
65
- return & store {
66
- ctx : ctx ,
67
- sid : sid ,
68
- cli : s .cli ,
69
- expired : expired ,
70
- values : make (map [string ]interface {}),
71
- }, nil
69
+ store := s .pool .Get ().(* store )
70
+ store .reset (ctx , sid , expired , nil )
71
+ return store , nil
72
72
}
73
73
74
74
func (s * managerStore ) Update (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
75
+ store := s .pool .Get ().(* store )
76
+
75
77
value , err := s .getValue (sid )
76
78
if err != nil {
77
79
return nil , err
78
80
} else if value == "" {
79
- return & store {
80
- ctx : ctx ,
81
- sid : sid ,
82
- cli : s .cli ,
83
- expired : expired ,
84
- values : make (map [string ]interface {}),
85
- }, nil
81
+ store .reset (ctx , sid , expired , nil )
82
+ return store , nil
86
83
}
87
84
88
85
cmd := s .cli .Expire (sid , time .Duration (expired )* time .Second )
@@ -94,14 +91,9 @@ func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (s
94
91
if err != nil {
95
92
return nil , err
96
93
}
94
+ store .reset (ctx , sid , expired , values )
97
95
98
- return & store {
99
- ctx : ctx ,
100
- sid : sid ,
101
- cli : s .cli ,
102
- expired : expired ,
103
- values : values ,
104
- }, nil
96
+ return store , nil
105
97
}
106
98
107
99
func (s * managerStore ) Delete (_ context.Context , sid string ) error {
@@ -124,17 +116,14 @@ func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
124
116
}
125
117
126
118
func (s * managerStore ) Refresh (ctx context.Context , oldsid , sid string , expired int64 ) (session.Store , error ) {
119
+ store := s .pool .Get ().(* store )
120
+
127
121
value , err := s .getValue (oldsid )
128
122
if err != nil {
129
123
return nil , err
130
124
} else if value == "" {
131
- return & store {
132
- ctx : ctx ,
133
- sid : sid ,
134
- cli : s .cli ,
135
- expired : expired ,
136
- values : make (map [string ]interface {}),
137
- }, nil
125
+ store .reset (ctx , sid , expired , nil )
126
+ return store , nil
138
127
}
139
128
140
129
pipe := s .cli .TxPipeline ()
@@ -149,19 +138,21 @@ func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired
149
138
if err != nil {
150
139
return nil , err
151
140
}
152
- return & store {
153
- ctx : ctx ,
154
- sid : sid ,
155
- cli : s .cli ,
156
- expired : expired ,
157
- values : values ,
158
- }, nil
141
+ store .reset (ctx , sid , expired , values )
142
+
143
+ return store , nil
159
144
}
160
145
161
146
func (s * managerStore ) Close () error {
162
147
return s .cli .Close ()
163
148
}
164
149
150
+ func newDefaultStore (cli * redis.Client ) * store {
151
+ return & store {
152
+ cli : cli ,
153
+ }
154
+ }
155
+
165
156
type store struct {
166
157
sync.RWMutex
167
158
ctx context.Context
@@ -171,6 +162,16 @@ type store struct {
171
162
cli * redis.Client
172
163
}
173
164
165
+ func (s * store ) reset (ctx context.Context , sid string , expired int64 , values map [string ]interface {}) {
166
+ if values == nil {
167
+ values = make (map [string ]interface {})
168
+ }
169
+ s .ctx = ctx
170
+ s .sid = sid
171
+ s .expired = expired
172
+ s .values = values
173
+ }
174
+
174
175
func (s * store ) Context () context.Context {
175
176
return s .ctx
176
177
}
0 commit comments