Skip to content

Commit

Permalink
optimization 👌
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Oct 29, 2018
1 parent 2132f35 commit ef6d610
Showing 1 changed file with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,27 @@ func NewCookieStore(opt ...Option) session.ManagerStore {
return &managerStore{
opts: opts,
cookie: cookie,
pool: sync.Pool{
New: func() interface{} {
return newStore(opts, cookie)
},
},
}
}

type managerStore struct {
opts options
cookie *securecookie.SecureCookie
pool sync.Pool
}

func (s *managerStore) Create(ctx context.Context, sid string, expired int64) (session.Store, error) {
store := s.pool.Get().(*store)
store.reset(ctx, sid, expired, nil)
return store, nil
return newStore(ctx, s, sid, expired, nil), nil
}

func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (session.Store, error) {
store := s.pool.Get().(*store)
req, ok := session.FromReqContext(ctx)
if !ok {
return nil, nil
}

cookie, err := req.Cookie(s.opts.cookieName)
if err != nil {
store.reset(ctx, sid, expired, nil)
return store, nil
return newStore(ctx, s, sid, expired, nil), nil
}

res, ok := session.FromResContext(ctx)
Expand All @@ -88,9 +78,8 @@ func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (s
if err != nil {
return nil, err
}
store.reset(ctx, sid, expired, values)

return store, nil
return newStore(ctx, s, sid, expired, values), nil
}

func (s *managerStore) Delete(ctx context.Context, sid string) error {
Expand Down Expand Up @@ -128,17 +117,14 @@ func (s *managerStore) Check(ctx context.Context, sid string) (bool, error) {
}

func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired int64) (session.Store, error) {
store := s.pool.Get().(*store)

req, ok := session.FromReqContext(ctx)
if !ok {
return nil, nil
}

cookie, err := req.Cookie(s.opts.cookieName)
if err != nil {
store.reset(ctx, sid, expired, nil)
return store, nil
return newStore(ctx, s, sid, expired, nil), nil
}

var values map[string]interface{}
Expand All @@ -160,19 +146,26 @@ func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired
return nil, nil
}
http.SetCookie(res, cookie)
store.reset(ctx, sid, expired, values)

return store, nil
return newStore(ctx, s, sid, expired, values), nil
}

func (s *managerStore) Close() error {
return nil
}

func newStore(opts options, cookie *securecookie.SecureCookie) *store {
func newStore(ctx context.Context, s *managerStore, sid string, expired int64, values map[string]interface{}) *store {
if values == nil {
values = make(map[string]interface{})
}

return &store{
opts: opts,
cookie: cookie,
opts: s.opts,
cookie: s.cookie,
ctx: ctx,
sid: sid,
expired: expired,
values: values,
}
}

Expand All @@ -186,16 +179,6 @@ type store struct {
values map[string]interface{}
}

func (s *store) reset(ctx context.Context, sid string, expired int64, values map[string]interface{}) {
if values == nil {
values = make(map[string]interface{})
}
s.ctx = ctx
s.sid = sid
s.expired = expired
s.values = values
}

func (s *store) Context() context.Context {
return s.ctx
}
Expand Down

0 comments on commit ef6d610

Please sign in to comment.