Skip to content

Commit b82b496

Browse files
committed
optimization 🍻
1 parent 0d983e6 commit b82b496

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

mongo.go

+16-38
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package mongo
22

33
import (
44
"context"
5+
"encoding/json"
56
"sync"
67
"time"
78

89
"github.com/globalsign/mgo"
910
"github.com/globalsign/mgo/bson"
1011
"github.com/go-session/session"
11-
"github.com/json-iterator/go"
1212
)
1313

1414
var (
1515
_ session.ManagerStore = &managerStore{}
1616
_ session.Store = &store{}
17-
jsonMarshal = jsoniter.Marshal
18-
jsonUnmarshal = jsoniter.Unmarshal
17+
jsonMarshal = json.Marshal
18+
jsonUnmarshal = json.Unmarshal
1919
)
2020

2121
// NewStore Create an instance of a mongo store
@@ -45,16 +45,10 @@ func newManagerStore(session *mgo.Session, dbName, cName string) *managerStore {
4545
session: session,
4646
dbName: dbName,
4747
cName: cName,
48-
pool: sync.Pool{
49-
New: func() interface{} {
50-
return newStore(session, dbName, cName)
51-
},
52-
},
5348
}
5449
}
5550

5651
type managerStore struct {
57-
pool sync.Pool
5852
session *mgo.Session
5953
dbName string
6054
cName string
@@ -98,20 +92,15 @@ func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
9892
}
9993

10094
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
10496
}
10597

10698
func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (session.Store, error) {
107-
store := s.pool.Get().(*store)
108-
10999
value, err := s.getValue(sid)
110100
if err != nil {
111101
return nil, err
112102
} else if value == "" {
113-
store.reset(ctx, sid, expired, nil)
114-
return store, nil
103+
return newStore(ctx, s, sid, expired, nil), nil
115104
}
116105

117106
session := s.session.Clone()
@@ -130,8 +119,7 @@ func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (s
130119
return nil, err
131120
}
132121

133-
store.reset(ctx, sid, expired, values)
134-
return store, nil
122+
return newStore(ctx, s, sid, expired, values), nil
135123
}
136124

137125
func (s *managerStore) Delete(_ context.Context, sid string) error {
@@ -141,14 +129,11 @@ func (s *managerStore) Delete(_ context.Context, sid string) error {
141129
}
142130

143131
func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired int64) (session.Store, error) {
144-
store := s.pool.Get().(*store)
145-
146132
value, err := s.getValue(oldsid)
147133
if err != nil {
148134
return nil, err
149135
} else if value == "" {
150-
store.reset(ctx, sid, expired, nil)
151-
return store, nil
136+
return newStore(ctx, s, sid, expired, nil), nil
152137
}
153138

154139
session := s.session.Clone()
@@ -172,20 +157,23 @@ func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired
172157
return nil, err
173158
}
174159

175-
store.reset(ctx, sid, expired, values)
176-
return store, nil
160+
return newStore(ctx, s, sid, expired, values), nil
177161
}
178162

179163
func (s *managerStore) Close() error {
180164
s.session.Close()
181165
return nil
182166
}
183167

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 {
185169
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,
189177
}
190178
}
191179

@@ -200,16 +188,6 @@ type store struct {
200188
values map[string]interface{}
201189
}
202190

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-
213191
func (s *store) Context() context.Context {
214192
return s.ctx
215193
}

0 commit comments

Comments
 (0)