@@ -21,10 +21,8 @@ const (
21
21
22
22
// RueidisStore is a store for Redis
23
23
type 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
28
26
}
29
27
30
28
// NewRueidis creates a new store to Redis instance(s)
@@ -37,43 +35,39 @@ func NewRueidis(client rueidis.Client, options ...lib_store.Option) *RueidisStor
37
35
}
38
36
39
37
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 ,
44
40
}
45
41
}
46
42
47
43
// Get returns data stored from a given key
48
44
func (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 )
52
50
}
53
- return object , object . Error ()
51
+ return str , err
54
52
}
55
53
56
54
// GetWithTTL returns data stored from a given key and its corresponding TTL
57
55
func (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 )
62
61
}
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
71
63
}
72
64
73
65
// Set defines data in Redis for given key identifier
74
66
func (s * RueidisStore ) Set (ctx context.Context , key any , value any , options ... lib_store.Option ) error {
75
67
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 ()
77
71
if err != nil {
78
72
return err
79
73
}
@@ -86,17 +80,19 @@ func (s *RueidisStore) Set(ctx context.Context, key any, value any, options ...l
86
80
}
87
81
88
82
func (s * RueidisStore ) setTags (ctx context.Context , key any , tags []string ) {
83
+ ttl := 720 * time .Hour
89
84
for _ , tag := range tags {
90
85
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
+ )
93
90
}
94
91
}
95
92
96
93
// Delete removes data from Redis for given key identifier
97
94
func (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 ()
100
96
}
101
97
102
98
// 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
107
103
for _ , tag := range tags {
108
104
tagKey := fmt .Sprintf (RueidisTagPattern , tag )
109
105
110
- cacheKeys , err := s .cacheCompat . SMembers (ctx , tagKey ).Result ()
106
+ cacheKeys , err := s .client . Do (ctx , s . client . B (). Smembers (). Key ( tagKey ).Build ()). AsStrSlice ()
111
107
if err != nil {
112
108
continue
113
109
}
@@ -130,9 +126,5 @@ func (s *RueidisStore) GetType() string {
130
126
131
127
// Clear resets all data in the store
132
128
func (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 ()
138
130
}
0 commit comments