Skip to content

Commit

Permalink
optimization 🍻
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Oct 28, 2018
1 parent 0d983e6 commit b82b496
Showing 1 changed file with 16 additions and 38 deletions.
54 changes: 16 additions & 38 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package mongo

import (
"context"
"encoding/json"
"sync"
"time"

"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/go-session/session"
"github.com/json-iterator/go"
)

var (
_ session.ManagerStore = &managerStore{}
_ session.Store = &store{}
jsonMarshal = jsoniter.Marshal
jsonUnmarshal = jsoniter.Unmarshal
jsonMarshal = json.Marshal
jsonUnmarshal = json.Unmarshal
)

// NewStore Create an instance of a mongo store
Expand Down Expand Up @@ -45,16 +45,10 @@ func newManagerStore(session *mgo.Session, dbName, cName string) *managerStore {
session: session,
dbName: dbName,
cName: cName,
pool: sync.Pool{
New: func() interface{} {
return newStore(session, dbName, cName)
},
},
}
}

type managerStore struct {
pool sync.Pool
session *mgo.Session
dbName string
cName string
Expand Down Expand Up @@ -98,20 +92,15 @@ func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
}

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)

value, err := s.getValue(sid)
if err != nil {
return nil, err
} else if value == "" {
store.reset(ctx, sid, expired, nil)
return store, nil
return newStore(ctx, s, sid, expired, nil), nil
}

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

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

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

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

value, err := s.getValue(oldsid)
if err != nil {
return nil, err
} else if value == "" {
store.reset(ctx, sid, expired, nil)
return store, nil
return newStore(ctx, s, sid, expired, nil), nil
}

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

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

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

func newStore(session *mgo.Session, dbName, cName string) *store {
func newStore(ctx context.Context, s *managerStore, sid string, expired int64, values map[string]interface{}) *store {
return &store{
session: session,
dbName: dbName,
cName: cName,
session: s.session,
dbName: s.dbName,
cName: s.cName,
ctx: ctx,
sid: sid,
expired: expired,
values: values,
}
}

Expand All @@ -200,16 +188,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 b82b496

Please sign in to comment.