Skip to content

Commit

Permalink
use object pool
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Jun 2, 2018
1 parent e9d10ee commit e898262
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@ var (
jsonUnmarshal = jsoniter.Unmarshal
)

// NewRedisStore Create an instance of a redis store
// NewRedisStore create an instance of a redis store
func NewRedisStore(opt *Options) session.ManagerStore {
if opt == nil {
panic("Option cannot be nil")
panic("option cannot be nil")
}
return &managerStore{cli: redis.NewClient(opt.redisOptions())}
}

// NewRedisStoreWithCli Create an instance of a redis store
// NewRedisStoreWithCli create an instance of a redis store
func NewRedisStoreWithCli(cli *redis.Client) session.ManagerStore {
return &managerStore{cli: cli}
return &managerStore{
cli: cli,
pool: sync.Pool{
New: func() interface{} {
return newDefaultStore(cli)
},
},
}
}

type managerStore struct {
cli *redis.Client
cli *redis.Client
pool sync.Pool
}

func (s *managerStore) getValue(sid string) (string, error) {
Expand All @@ -54,35 +62,24 @@ func (s *managerStore) parseValue(value string) (map[string]interface{}, error)
return nil, err
}
}

if values == nil {
values = make(map[string]interface{})
}
return values, nil
}

func (s *managerStore) Create(ctx context.Context, sid string, expired int64) (session.Store, error) {
return &store{
ctx: ctx,
sid: sid,
cli: s.cli,
expired: expired,
values: make(map[string]interface{}),
}, nil
store := s.pool.Get().(*store)
store.reset(ctx, sid, expired, nil)
return store, 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 == "" {
return &store{
ctx: ctx,
sid: sid,
cli: s.cli,
expired: expired,
values: make(map[string]interface{}),
}, nil
store.reset(ctx, sid, expired, nil)
return store, nil
}

cmd := s.cli.Expire(sid, time.Duration(expired)*time.Second)
Expand All @@ -94,14 +91,9 @@ 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{
ctx: ctx,
sid: sid,
cli: s.cli,
expired: expired,
values: values,
}, nil
return store, nil
}

func (s *managerStore) Delete(_ context.Context, sid string) error {
Expand All @@ -124,17 +116,14 @@ func (s *managerStore) Check(_ 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)

value, err := s.getValue(oldsid)
if err != nil {
return nil, err
} else if value == "" {
return &store{
ctx: ctx,
sid: sid,
cli: s.cli,
expired: expired,
values: make(map[string]interface{}),
}, nil
store.reset(ctx, sid, expired, nil)
return store, nil
}

pipe := s.cli.TxPipeline()
Expand All @@ -149,19 +138,21 @@ func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired
if err != nil {
return nil, err
}
return &store{
ctx: ctx,
sid: sid,
cli: s.cli,
expired: expired,
values: values,
}, nil
store.reset(ctx, sid, expired, values)

return store, nil
}

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

func newDefaultStore(cli *redis.Client) *store {
return &store{
cli: cli,
}
}

type store struct {
sync.RWMutex
ctx context.Context
Expand All @@ -171,6 +162,16 @@ type store struct {
cli *redis.Client
}

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 e898262

Please sign in to comment.