Skip to content

Commit a304dd2

Browse files
committed
optimization 🍻
1 parent 0317072 commit a304dd2

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

redis.go

+20-42
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ package redis
22

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

89
"github.com/go-redis/redis"
910
"github.com/go-session/session"
10-
"github.com/json-iterator/go"
1111
)
1212

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

2020
// NewRedisStore create an instance of a redis store
@@ -29,11 +29,6 @@ func NewRedisStore(opts *Options) session.ManagerStore {
2929
func NewRedisStoreWithCli(cli *redis.Client) session.ManagerStore {
3030
return &managerStore{
3131
cli: cli,
32-
pool: sync.Pool{
33-
New: func() interface{} {
34-
return newStore(cli)
35-
},
36-
},
3732
}
3833
}
3934

@@ -49,11 +44,6 @@ func NewRedisClusterStore(opts *ClusterOptions) session.ManagerStore {
4944
func NewRedisClusterStoreWithCli(cli *redis.ClusterClient) session.ManagerStore {
5045
return &managerStore{
5146
cli: cli,
52-
pool: sync.Pool{
53-
New: func() interface{} {
54-
return newStore(cli)
55-
},
56-
},
5747
}
5848
}
5949

@@ -68,8 +58,7 @@ type clienter interface {
6858
}
6959

7060
type managerStore struct {
71-
cli clienter
72-
pool sync.Pool
61+
cli clienter
7362
}
7463

7564
func (s *managerStore) getValue(sid string) (string, error) {
@@ -96,20 +85,16 @@ func (s *managerStore) parseValue(value string) (map[string]interface{}, error)
9685
}
9786

9887
func (s *managerStore) Create(ctx context.Context, sid string, expired int64) (session.Store, error) {
99-
store := s.pool.Get().(*store)
100-
store.reset(ctx, sid, expired, nil)
101-
return store, nil
88+
return newStore(ctx, s.cli, sid, expired, nil), nil
10289
}
10390

10491
func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (session.Store, error) {
105-
store := s.pool.Get().(*store)
106-
10792
value, err := s.getValue(sid)
10893
if err != nil {
10994
return nil, err
11095
} else if value == "" {
111-
store.reset(ctx, sid, expired, nil)
112-
return store, nil
96+
97+
return newStore(ctx, s.cli, sid, expired, nil), nil
11398
}
11499

115100
cmd := s.cli.Expire(sid, time.Duration(expired)*time.Second)
@@ -121,9 +106,8 @@ func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (s
121106
if err != nil {
122107
return nil, err
123108
}
124-
store.reset(ctx, sid, expired, values)
125109

126-
return store, nil
110+
return newStore(ctx, s.cli, sid, expired, values), nil
127111
}
128112

129113
func (s *managerStore) Delete(_ context.Context, sid string) error {
@@ -146,14 +130,11 @@ func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
146130
}
147131

148132
func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired int64) (session.Store, error) {
149-
store := s.pool.Get().(*store)
150-
151133
value, err := s.getValue(oldsid)
152134
if err != nil {
153135
return nil, err
154136
} else if value == "" {
155-
store.reset(ctx, sid, expired, nil)
156-
return store, nil
137+
return newStore(ctx, s.cli, sid, expired, nil), nil
157138
}
158139

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

173-
return store, nil
153+
return newStore(ctx, s.cli, sid, expired, values), nil
174154
}
175155

176156
func (s *managerStore) Close() error {
177157
return s.cli.Close()
178158
}
179159

180-
func newStore(cli clienter) *store {
160+
func newStore(ctx context.Context, cli clienter, sid string, expired int64, values map[string]interface{}) *store {
161+
if values == nil {
162+
values = make(map[string]interface{})
163+
}
164+
181165
return &store{
182-
cli: cli,
166+
cli: cli,
167+
ctx: ctx,
168+
sid: sid,
169+
expired: expired,
170+
values: values,
183171
}
184172
}
185173

@@ -192,16 +180,6 @@ type store struct {
192180
cli clienter
193181
}
194182

195-
func (s *store) reset(ctx context.Context, sid string, expired int64, values map[string]interface{}) {
196-
if values == nil {
197-
values = make(map[string]interface{})
198-
}
199-
s.ctx = ctx
200-
s.sid = sid
201-
s.expired = expired
202-
s.values = values
203-
}
204-
205183
func (s *store) Context() context.Context {
206184
return s.ctx
207185
}

0 commit comments

Comments
 (0)