@@ -2,7 +2,6 @@ package rueidis
22
33import (
44 "context"
5- "sync"
65 "time"
76
87 "github.com/redis/rueidis"
@@ -12,9 +11,7 @@ var cacheTTL = time.Second
1211
1312// Storage interface that is implemented by storage providers
1413type Storage struct {
15- mu sync.Mutex
16- db rueidis.Client
17- cfg Config
14+ db rueidis.Client
1815}
1916
2017// New creates a new rueidis storage
@@ -48,7 +45,23 @@ func New(config ...Config) *Storage {
4845 }
4946 }
5047
51- db , err := newRueidisClient (cfg )
48+ db , err := rueidis .NewClient (rueidis.ClientOption {
49+ Username : cfg .Username ,
50+ Password : cfg .Password ,
51+ ClientName : cfg .ClientName ,
52+ SelectDB : cfg .SelectDB ,
53+ InitAddress : cfg .InitAddress ,
54+ TLSConfig : cfg .TLSConfig ,
55+ CacheSizeEachConn : cfg .CacheSizeEachConn ,
56+ RingScaleEachConn : cfg .RingScaleEachConn ,
57+ ReadBufferEachConn : cfg .ReadBufferEachConn ,
58+ WriteBufferEachConn : cfg .WriteBufferEachConn ,
59+ BlockingPoolSize : cfg .BlockingPoolSize ,
60+ PipelineMultiplex : cfg .PipelineMultiplex ,
61+ DisableRetry : cfg .DisableRetry ,
62+ DisableCache : cfg .DisableCache ,
63+ AlwaysPipelining : cfg .AlwaysPipelining ,
64+ })
5265 if err != nil {
5366 if ! cfg .DisableStartupCheck {
5467 panic (err )
@@ -69,8 +82,7 @@ func New(config ...Config) *Storage {
6982
7083 // Create new store
7184 return & Storage {
72- db : db ,
73- cfg : cfg ,
85+ db : db ,
7486 }
7587}
7688
@@ -79,11 +91,7 @@ func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error
7991 if len (key ) <= 0 {
8092 return nil , nil
8193 }
82- db , err := s .getClient ()
83- if err != nil {
84- return nil , err
85- }
86- val , err := db .DoCache (ctx , db .B ().Get ().Key (key ).Cache (), cacheTTL ).AsBytes ()
94+ val , err := s .db .DoCache (ctx , s .db .B ().Get ().Key (key ).Cache (), cacheTTL ).AsBytes ()
8795 if err != nil && rueidis .IsRedisNil (err ) {
8896 return nil , nil
8997 }
@@ -100,14 +108,10 @@ func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, ex
100108 if len (key ) <= 0 || len (val ) <= 0 {
101109 return nil
102110 }
103- db , err := s .getClient ()
104- if err != nil {
105- return err
106- }
107111 if exp > 0 {
108- return db .Do (ctx , db .B ().Set ().Key (key ).Value (string (val )).Ex (exp ).Build ()).Error ()
112+ return s . db .Do (ctx , s . db .B ().Set ().Key (key ).Value (string (val )).Ex (exp ).Build ()).Error ()
109113 } else {
110- return db .Do (ctx , db .B ().Set ().Key (key ).Value (string (val )).Build ()).Error ()
114+ return s . db .Do (ctx , s . db .B ().Set ().Key (key ).Value (string (val )).Build ()).Error ()
111115 }
112116}
113117
@@ -121,11 +125,7 @@ func (s *Storage) DeleteWithContext(ctx context.Context, key string) error {
121125 if len (key ) <= 0 {
122126 return nil
123127 }
124- db , err := s .getClient ()
125- if err != nil {
126- return err
127- }
128- return db .Do (ctx , db .B ().Del ().Key (key ).Build ()).Error ()
128+ return s .db .Do (ctx , s .db .B ().Del ().Key (key ).Build ()).Error ()
129129}
130130
131131// Delete deletes key by key
@@ -135,11 +135,7 @@ func (s *Storage) Delete(key string) error {
135135
136136// ResetWithContext resets all keys with context
137137func (s * Storage ) ResetWithContext (ctx context.Context ) error {
138- db , err := s .getClient ()
139- if err != nil {
140- return err
141- }
142- return db .Do (ctx , db .B ().Flushdb ().Build ()).Error ()
138+ return s .db .Do (ctx , s .db .B ().Flushdb ().Build ()).Error ()
143139}
144140
145141// Reset resets all keys
@@ -149,56 +145,15 @@ func (s *Storage) Reset() error {
149145
150146// Close the database
151147func (s * Storage ) Close () error {
152- s .mu .Lock ()
153- defer s .mu .Unlock ()
154-
155148 if s .db == nil {
156149 return nil
157150 }
158151
159152 s .db .Close ()
160- s .db = nil
161153 return nil
162154}
163155
164156// Return database client
165157func (s * Storage ) Conn () rueidis.Client {
166158 return s .db
167159}
168-
169- func (s * Storage ) getClient () (rueidis.Client , error ) {
170- s .mu .Lock ()
171- defer s .mu .Unlock ()
172-
173- if s .db != nil {
174- return s .db , nil
175- }
176-
177- db , err := newRueidisClient (s .cfg )
178- if err != nil {
179- return nil , err
180- }
181-
182- s .db = db
183- return s .db , nil
184- }
185-
186- func newRueidisClient (cfg Config ) (rueidis.Client , error ) {
187- return rueidis .NewClient (rueidis.ClientOption {
188- Username : cfg .Username ,
189- Password : cfg .Password ,
190- ClientName : cfg .ClientName ,
191- SelectDB : cfg .SelectDB ,
192- InitAddress : cfg .InitAddress ,
193- TLSConfig : cfg .TLSConfig ,
194- CacheSizeEachConn : cfg .CacheSizeEachConn ,
195- RingScaleEachConn : cfg .RingScaleEachConn ,
196- ReadBufferEachConn : cfg .ReadBufferEachConn ,
197- WriteBufferEachConn : cfg .WriteBufferEachConn ,
198- BlockingPoolSize : cfg .BlockingPoolSize ,
199- PipelineMultiplex : cfg .PipelineMultiplex ,
200- DisableRetry : cfg .DisableRetry ,
201- DisableCache : cfg .DisableCache ,
202- AlwaysPipelining : cfg .AlwaysPipelining ,
203- })
204- }
0 commit comments