@@ -125,120 +125,17 @@ func (cacheBackend *Redis) Get(ctx context.Context, key string) (*cache.Item, bo
125125
126126// Set stores the Item in the cacheBackend.
127127func (cacheBackend * Redis ) Set (ctx context.Context , item * cache.Item ) error {
128- pipe := cacheBackend .rdb .TxPipeline ()
129-
130- // Check if the item is valid
131- err := item .Valid ()
132- if err != nil {
133- // Return the item to the pool
134- return err
135- }
136-
137- // Serialize the item
138- data , err := cacheBackend .Serializer .Marshal (item )
139- if err != nil {
140- // Return the item to the pool
141- return err
142- }
143-
144- expiration := item .Expiration .String ()
145-
146- // Store the item in the cacheBackend
147- err = pipe .HSet (ctx , item .Key , map [string ]any {
148- "data" : data ,
149- "expiration" : expiration ,
150- }).Err ()
151- if err != nil {
152- return ewrap .Wrap (err , "failed to set item in redis" )
153- }
154- // Add the key to the set of keys associated with the cache prefix
155- pipe .SAdd (ctx , cacheBackend .keysSetName , item .Key )
156- // Set the expiration if it is not zero
157- if item .Expiration > 0 {
158- pipe .Expire (ctx , item .Key , item .Expiration )
159- }
160-
161- _ , err = pipe .Exec (ctx )
162- if err != nil {
163- return ewrap .Wrap (err , "failed to execute redis pipeline" )
164- }
165-
166- return nil
128+ return redisSet (ctx , cacheBackend .rdb , cacheBackend .keysSetName , item , cacheBackend .Serializer )
167129}
168130
169131// List returns a list of all the items in the cacheBackend that match the given filter options.
170132func (cacheBackend * Redis ) List (ctx context.Context , filters ... IFilter ) ([]* cache.Item , error ) {
171- // Get the set of keys held in the cacheBackend with the given `keysSetName`
172- keys , err := cacheBackend .rdb .SMembers (ctx , cacheBackend .keysSetName ).Result ()
173- if err != nil {
174- return nil , ewrap .Wrap (err , "failed to get keys from redis" )
175- }
176-
177- // Execute the Redis commands in a pipeline transaction to improve performance and reduce the number of round trips
178- cmds , err := cacheBackend .rdb .Pipelined (ctx , func (pipe redis.Pipeliner ) error {
179- // Get the items from the cacheBackend
180- for _ , key := range keys {
181- pipe .HGetAll (ctx , key )
182- }
183-
184- return nil
185- })
186- if err != nil {
187- return nil , ewrap .Wrap (err , "failed to execute redis pipeline while listing" )
188- }
189-
190- // Create a slice to hold the items
191- items := make ([]* cache.Item , 0 , len (keys ))
192-
193- // Deserialize the items and add them to the slice of items to return
194- for _ , cmd := range cmds {
195- command , ok := cmd .(* redis.MapStringStringCmd )
196- if ! ok {
197- continue
198- }
199-
200- data , err := command .Result () // Change the type assertion to match HGetAll
201- if err != nil {
202- return nil , ewrap .Wrap (err , "failed to get item data from redis" )
203- }
204-
205- item := cacheBackend .itemPoolManager .Get ()
206- // Return the item to the pool
207- defer cacheBackend .itemPoolManager .Put (item )
208-
209- err = cacheBackend .Serializer .Unmarshal ([]byte (data ["data" ]), item )
210- if err == nil {
211- items = append (items , item )
212- }
213- }
214-
215- // Apply the filters
216- if len (filters ) > 0 {
217- for _ , filter := range filters {
218- items , err = filter .ApplyFilter (constants .RedisBackend , items )
219- }
220- }
221-
222- return items , err
133+ return redisList (ctx , cacheBackend .rdb , cacheBackend .keysSetName , cacheBackend .Serializer , cacheBackend .itemPoolManager , filters ... )
223134}
224135
225136// Remove removes an item from the cache with the given key.
226137func (cacheBackend * Redis ) Remove (ctx context.Context , keys ... string ) error {
227- pipe := cacheBackend .rdb .TxPipeline ()
228-
229- _ , err := pipe .SRem (ctx , cacheBackend .keysSetName , keys ).Result ()
230- if err != nil {
231- return ewrap .Wrap (err , "removing keys from set" )
232- }
233-
234- _ , err = pipe .Del (ctx , keys ... ).Result ()
235- if err != nil {
236- return ewrap .Wrap (err , "removing keys" )
237- }
238-
239- _ , err = pipe .Exec (ctx )
240-
241- return ewrap .Wrap (err , "executing pipeline" )
138+ return redisRemove (ctx , cacheBackend .rdb , cacheBackend .keysSetName , keys ... )
242139}
243140
244141// Clear removes all items from the cache.
0 commit comments