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 0317072 commit a304dd2
Showing 1 changed file with 20 additions and 42 deletions.
62 changes: 20 additions & 42 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package redis

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

"github.com/go-redis/redis"
"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
)

// NewRedisStore create an instance of a redis store
Expand All @@ -29,11 +29,6 @@ func NewRedisStore(opts *Options) session.ManagerStore {
func NewRedisStoreWithCli(cli *redis.Client) session.ManagerStore {
return &managerStore{
cli: cli,
pool: sync.Pool{
New: func() interface{} {
return newStore(cli)
},
},
}
}

Expand All @@ -49,11 +44,6 @@ func NewRedisClusterStore(opts *ClusterOptions) session.ManagerStore {
func NewRedisClusterStoreWithCli(cli *redis.ClusterClient) session.ManagerStore {
return &managerStore{
cli: cli,
pool: sync.Pool{
New: func() interface{} {
return newStore(cli)
},
},
}
}

Expand All @@ -68,8 +58,7 @@ type clienter interface {
}

type managerStore struct {
cli clienter
pool sync.Pool
cli clienter
}

func (s *managerStore) getValue(sid string) (string, error) {
Expand All @@ -96,20 +85,16 @@ func (s *managerStore) parseValue(value string) (map[string]interface{}, 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.cli, 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.cli, sid, expired, nil), nil
}

cmd := s.cli.Expire(sid, time.Duration(expired)*time.Second)
Expand All @@ -121,9 +106,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.cli, sid, expired, values), nil
}

func (s *managerStore) Delete(_ context.Context, sid string) error {
Expand All @@ -146,14 +130,11 @@ 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 == "" {
store.reset(ctx, sid, expired, nil)
return store, nil
return newStore(ctx, s.cli, sid, expired, nil), nil
}

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

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

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

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

return &store{
cli: cli,
cli: cli,
ctx: ctx,
sid: sid,
expired: expired,
values: values,
}
}

Expand All @@ -192,16 +180,6 @@ type store struct {
cli clienter
}

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 a304dd2

Please sign in to comment.