@@ -17,21 +17,29 @@ var (
1717 jsonUnmarshal = jsoniter .Unmarshal
1818)
1919
20- // NewRedisStore Create an instance of a redis store
20+ // NewRedisStore create an instance of a redis store
2121func NewRedisStore (opt * Options ) session.ManagerStore {
2222 if opt == nil {
23- panic ("Option cannot be nil" )
23+ panic ("option cannot be nil" )
2424 }
2525 return & managerStore {cli : redis .NewClient (opt .redisOptions ())}
2626}
2727
28- // NewRedisStoreWithCli Create an instance of a redis store
28+ // NewRedisStoreWithCli create an instance of a redis store
2929func 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+ }
3138}
3239
3340type managerStore struct {
34- cli * redis.Client
41+ cli * redis.Client
42+ pool sync.Pool
3543}
3644
3745func (s * managerStore ) getValue (sid string ) (string , error ) {
@@ -54,35 +62,24 @@ func (s *managerStore) parseValue(value string) (map[string]interface{}, error)
5462 return nil , err
5563 }
5664 }
57-
58- if values == nil {
59- values = make (map [string ]interface {})
60- }
6165 return values , nil
6266}
6367
6468func (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
7272}
7373
7474func (s * managerStore ) Update (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
75+ store := s .pool .Get ().(* store )
76+
7577 value , err := s .getValue (sid )
7678 if err != nil {
7779 return nil , err
7880 } 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
8683 }
8784
8885 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
9491 if err != nil {
9592 return nil , err
9693 }
94+ store .reset (ctx , sid , expired , values )
9795
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
10597}
10698
10799func (s * managerStore ) Delete (_ context.Context , sid string ) error {
@@ -124,17 +116,14 @@ func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
124116}
125117
126118func (s * managerStore ) Refresh (ctx context.Context , oldsid , sid string , expired int64 ) (session.Store , error ) {
119+ store := s .pool .Get ().(* store )
120+
127121 value , err := s .getValue (oldsid )
128122 if err != nil {
129123 return nil , err
130124 } 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
138127 }
139128
140129 pipe := s .cli .TxPipeline ()
@@ -149,19 +138,21 @@ func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired
149138 if err != nil {
150139 return nil , err
151140 }
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
159144}
160145
161146func (s * managerStore ) Close () error {
162147 return s .cli .Close ()
163148}
164149
150+ func newDefaultStore (cli * redis.Client ) * store {
151+ return & store {
152+ cli : cli ,
153+ }
154+ }
155+
165156type store struct {
166157 sync.RWMutex
167158 ctx context.Context
@@ -171,6 +162,16 @@ type store struct {
171162 cli * redis.Client
172163}
173164
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+
174175func (s * store ) Context () context.Context {
175176 return s .ctx
176177}
0 commit comments