@@ -2,20 +2,20 @@ package mongo
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"sync"
6
7
"time"
7
8
8
9
"github.com/globalsign/mgo"
9
10
"github.com/globalsign/mgo/bson"
10
11
"github.com/go-session/session"
11
- "github.com/json-iterator/go"
12
12
)
13
13
14
14
var (
15
15
_ session.ManagerStore = & managerStore {}
16
16
_ session.Store = & store {}
17
- jsonMarshal = jsoniter .Marshal
18
- jsonUnmarshal = jsoniter .Unmarshal
17
+ jsonMarshal = json .Marshal
18
+ jsonUnmarshal = json .Unmarshal
19
19
)
20
20
21
21
// NewStore Create an instance of a mongo store
@@ -45,16 +45,10 @@ func newManagerStore(session *mgo.Session, dbName, cName string) *managerStore {
45
45
session : session ,
46
46
dbName : dbName ,
47
47
cName : cName ,
48
- pool : sync.Pool {
49
- New : func () interface {} {
50
- return newStore (session , dbName , cName )
51
- },
52
- },
53
48
}
54
49
}
55
50
56
51
type managerStore struct {
57
- pool sync.Pool
58
52
session * mgo.Session
59
53
dbName string
60
54
cName string
@@ -98,20 +92,15 @@ func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
98
92
}
99
93
100
94
func (s * managerStore ) Create (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
101
- store := s .pool .Get ().(* store )
102
- store .reset (ctx , sid , expired , nil )
103
- return store , nil
95
+ return newStore (ctx , s , sid , expired , nil ), nil
104
96
}
105
97
106
98
func (s * managerStore ) Update (ctx context.Context , sid string , expired int64 ) (session.Store , error ) {
107
- store := s .pool .Get ().(* store )
108
-
109
99
value , err := s .getValue (sid )
110
100
if err != nil {
111
101
return nil , err
112
102
} else if value == "" {
113
- store .reset (ctx , sid , expired , nil )
114
- return store , nil
103
+ return newStore (ctx , s , sid , expired , nil ), nil
115
104
}
116
105
117
106
session := s .session .Clone ()
@@ -130,8 +119,7 @@ func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (s
130
119
return nil , err
131
120
}
132
121
133
- store .reset (ctx , sid , expired , values )
134
- return store , nil
122
+ return newStore (ctx , s , sid , expired , values ), nil
135
123
}
136
124
137
125
func (s * managerStore ) Delete (_ context.Context , sid string ) error {
@@ -141,14 +129,11 @@ func (s *managerStore) Delete(_ context.Context, sid string) error {
141
129
}
142
130
143
131
func (s * managerStore ) Refresh (ctx context.Context , oldsid , sid string , expired int64 ) (session.Store , error ) {
144
- store := s .pool .Get ().(* store )
145
-
146
132
value , err := s .getValue (oldsid )
147
133
if err != nil {
148
134
return nil , err
149
135
} else if value == "" {
150
- store .reset (ctx , sid , expired , nil )
151
- return store , nil
136
+ return newStore (ctx , s , sid , expired , nil ), nil
152
137
}
153
138
154
139
session := s .session .Clone ()
@@ -172,20 +157,23 @@ func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired
172
157
return nil , err
173
158
}
174
159
175
- store .reset (ctx , sid , expired , values )
176
- return store , nil
160
+ return newStore (ctx , s , sid , expired , values ), nil
177
161
}
178
162
179
163
func (s * managerStore ) Close () error {
180
164
s .session .Close ()
181
165
return nil
182
166
}
183
167
184
- func newStore (session * mgo. Session , dbName , cName string ) * store {
168
+ func newStore (ctx context. Context , s * managerStore , sid string , expired int64 , values map [ string ] interface {} ) * store {
185
169
return & store {
186
- session : session ,
187
- dbName : dbName ,
188
- cName : cName ,
170
+ session : s .session ,
171
+ dbName : s .dbName ,
172
+ cName : s .cName ,
173
+ ctx : ctx ,
174
+ sid : sid ,
175
+ expired : expired ,
176
+ values : values ,
189
177
}
190
178
}
191
179
@@ -200,16 +188,6 @@ type store struct {
200
188
values map [string ]interface {}
201
189
}
202
190
203
- func (s * store ) reset (ctx context.Context , sid string , expired int64 , values map [string ]interface {}) {
204
- if values == nil {
205
- values = make (map [string ]interface {})
206
- }
207
- s .ctx = ctx
208
- s .sid = sid
209
- s .expired = expired
210
- s .values = values
211
- }
212
-
213
191
func (s * store ) Context () context.Context {
214
192
return s .ctx
215
193
}
0 commit comments