@@ -21,10 +21,8 @@ const (
2121
2222// RueidisStore is a store for Redis
2323type RueidisStore struct {
24- client rueidis.Client
25- options * lib_store.Options
26- cacheCompat rueidiscompat.CacheCompat
27- compat rueidiscompat.Cmdable
24+ client rueidis.Client
25+ options * lib_store.Options
2826}
2927
3028// NewRueidis creates a new store to Redis instance(s)
@@ -37,43 +35,39 @@ func NewRueidis(client rueidis.Client, options ...lib_store.Option) *RueidisStor
3735 }
3836
3937 return & RueidisStore {
40- client : client ,
41- cacheCompat : rueidiscompat .NewAdapter (client ).Cache (appliedOptions .ClientSideCacheExpiration ),
42- compat : rueidiscompat .NewAdapter (client ),
43- options : appliedOptions ,
38+ client : client ,
39+ options : appliedOptions ,
4440 }
4541}
4642
4743// Get returns data stored from a given key
4844func (s * RueidisStore ) Get (ctx context.Context , key any ) (any , error ) {
49- object := s .client .DoCache (ctx , s .client .B ().Get ().Key (key .(string )).Cache (), s .options .ClientSideCacheExpiration )
50- if object .RedisError () != nil && object .RedisError ().IsNil () {
51- return nil , lib_store .NotFoundWithCause (object .Error ())
45+ cmd := s .client .B ().Get ().Key (key .(string )).Cache ()
46+ res := s .client .DoCache (ctx , cmd , s .options .ClientSideCacheExpiration )
47+ str , err := res .ToString ()
48+ if rueidis .IsRedisNil (err ) {
49+ err = lib_store .NotFoundWithCause (err )
5250 }
53- return object , object . Error ()
51+ return str , err
5452}
5553
5654// GetWithTTL returns data stored from a given key and its corresponding TTL
5755func (s * RueidisStore ) GetWithTTL (ctx context.Context , key any ) (any , time.Duration , error ) {
58- // get object first
59- object , err := s .Get (ctx , key )
60- if err != nil {
61- return nil , 0 , err
56+ cmd := s .client .B ().Get ().Key (key .(string )).Cache ()
57+ res := s .client .DoCache (ctx , cmd , s .options .ClientSideCacheExpiration )
58+ str , err := res .ToString ()
59+ if rueidis .IsRedisNil (err ) {
60+ err = lib_store .NotFoundWithCause (err )
6261 }
63-
64- // get TTL and return
65- ttl , err := s .cacheCompat .TTL (ctx , key .(string )).Result ()
66- if err != nil {
67- return nil , 0 , err
68- }
69-
70- return object , ttl , err
62+ return str , time .Duration (res .CacheTTL ()) * time .Second , err
7163}
7264
7365// Set defines data in Redis for given key identifier
7466func (s * RueidisStore ) Set (ctx context.Context , key any , value any , options ... lib_store.Option ) error {
7567 opts := lib_store .ApplyOptionsWithDefault (s .options , options ... )
76- err := s .compat .Set (ctx , key .(string ), value , opts .Expiration ).Err ()
68+ ttl := int64 (opts .Expiration .Seconds ())
69+ cmd := s .client .B ().Set ().Key (key .(string )).Value (value .(string )).ExSeconds (ttl ).Build ()
70+ err := s .client .Do (ctx , cmd ).Error ()
7771 if err != nil {
7872 return err
7973 }
@@ -86,17 +80,19 @@ func (s *RueidisStore) Set(ctx context.Context, key any, value any, options ...l
8680}
8781
8882func (s * RueidisStore ) setTags (ctx context.Context , key any , tags []string ) {
83+ ttl := 720 * time .Hour
8984 for _ , tag := range tags {
9085 tagKey := fmt .Sprintf (RueidisTagPattern , tag )
91- s .compat .SAdd (ctx , tagKey , key .(string ))
92- s .compat .Expire (ctx , tagKey , 720 * time .Hour )
86+ s .client .DoMulti (ctx ,
87+ s .client .B ().Sadd ().Key (tagKey ).Member (key .(string )).Build (),
88+ s .client .B ().Expire ().Key (tagKey ).Seconds (int64 (ttl .Seconds ())).Build (),
89+ )
9390 }
9491}
9592
9693// Delete removes data from Redis for given key identifier
9794func (s * RueidisStore ) Delete (ctx context.Context , key any ) error {
98- _ , err := s .compat .Del (ctx , key .(string )).Result ()
99- return err
95+ return s .client .Do (ctx , s .client .B ().Del ().Key (key .(string )).Build ()).Error ()
10096}
10197
10298// Invalidate invalidates some cache data in Redis for given options
@@ -107,7 +103,7 @@ func (s *RueidisStore) Invalidate(ctx context.Context, options ...lib_store.Inva
107103 for _ , tag := range tags {
108104 tagKey := fmt .Sprintf (RueidisTagPattern , tag )
109105
110- cacheKeys , err := s .cacheCompat . SMembers (ctx , tagKey ).Result ()
106+ cacheKeys , err := s .client . Do (ctx , s . client . B (). Smembers (). Key ( tagKey ).Build ()). AsStrSlice ()
111107 if err != nil {
112108 continue
113109 }
@@ -130,9 +126,5 @@ func (s *RueidisStore) GetType() string {
130126
131127// Clear resets all data in the store
132128func (s * RueidisStore ) Clear (ctx context.Context ) error {
133- if err := s .compat .FlushAll (ctx ).Err (); err != nil {
134- return err
135- }
136-
137- return nil
129+ return rueidiscompat .NewAdapter (s .client ).FlushAll (ctx ).Err ()
138130}
0 commit comments