@@ -37,6 +37,16 @@ func (s *StringKeyspace[K]) Get(ctx context.Context, key K) (string, error) {
3737 return s .basicKeyspace .Get (ctx , key )
3838}
3939
40+ // MultiGet gets the values stored at multiple keys.
41+ // For each key, the result contains an Err field indicating success or failure.
42+ // If Err is nil, Value contains the cached value.
43+ // If Err matches Miss, the key was not found.
44+ //
45+ // See https://redis.io/commands/mget/ for more information.
46+ func (s * StringKeyspace [K ]) MultiGet (ctx context.Context , keys ... K ) ([]Result [string ], error ) {
47+ return s .basicKeyspace .MultiGet (ctx , keys ... )
48+ }
49+
4050// Set updates the value stored at key to val.
4151//
4252// See https://redis.io/commands/set/ for more information.
@@ -230,6 +240,16 @@ func (s *IntKeyspace[K]) Get(ctx context.Context, key K) (int64, error) {
230240 return s .basicKeyspace .Get (ctx , key )
231241}
232242
243+ // MultiGet gets the values stored at multiple keys.
244+ // For each key, the result contains an Err field indicating success or failure.
245+ // If Err is nil, Value contains the cached value.
246+ // If Err matches Miss, the key was not found.
247+ //
248+ // See https://redis.io/commands/mget/ for more information.
249+ func (s * IntKeyspace [K ]) MultiGet (ctx context.Context , keys ... K ) ([]Result [int64 ], error ) {
250+ return s .basicKeyspace .MultiGet (ctx , keys ... )
251+ }
252+
233253// Set updates the value stored at key to val.
234254//
235255// See https://redis.io/commands/set/ for more information.
@@ -371,6 +391,16 @@ func (s *FloatKeyspace[K]) Get(ctx context.Context, key K) (float64, error) {
371391 return s .basicKeyspace .Get (ctx , key )
372392}
373393
394+ // MultiGet gets the values stored at multiple keys.
395+ // For each key, the result contains an Err field indicating success or failure.
396+ // If Err is nil, Value contains the cached value.
397+ // If Err matches Miss, the key was not found.
398+ //
399+ // See https://redis.io/commands/mget/ for more information.
400+ func (s * FloatKeyspace [K ]) MultiGet (ctx context.Context , keys ... K ) ([]Result [float64 ], error ) {
401+ return s .basicKeyspace .MultiGet (ctx , keys ... )
402+ }
403+
374404// Set updates the value stored at key to val.
375405//
376406// See https://redis.io/commands/set/ for more information.
@@ -498,6 +528,43 @@ func (s *basicKeyspace[K, V]) Get(ctx context.Context, key K) (val V, err error)
498528 return val , err
499529}
500530
531+ func (s * basicKeyspace [K , V ]) MultiGet (ctx context.Context , keys ... K ) ([]Result [V ], error ) {
532+ const op = "multi get"
533+ ks , err := s .keys (keys , op )
534+ endTrace := s .doTrace (op , false , ks ... )
535+ defer func () { endTrace (err ) }()
536+ if err != nil {
537+ return nil , err
538+ }
539+ var firstKey string
540+ if len (ks ) > 0 {
541+ firstKey = ks [0 ]
542+ }
543+ res , err := s .redis .MGet (ctx , ks ... ).Result ()
544+ if err != nil {
545+ return nil , toErr (err , op , firstKey )
546+ }
547+ results := make ([]Result [V ], 0 , len (res ))
548+ for i , r := range res {
549+ if r == nil {
550+ results = append (results , Result [V ]{Err : toErr (Miss , op , ks [i ])})
551+ continue
552+ }
553+ strVal , ok := r .(string )
554+ if ! ok {
555+ results = append (results , Result [V ]{Err : toErr (errors .New ("invalid redis value type" ), op , ks [i ])})
556+ continue
557+ }
558+ val , fromRedisErr := s .fromRedis (strVal )
559+ if fromRedisErr != nil {
560+ results = append (results , Result [V ]{Err : toErr (fromRedisErr , op , ks [i ])})
561+ continue
562+ }
563+ results = append (results , Result [V ]{Value : val })
564+ }
565+ return results , nil
566+ }
567+
501568func (s * basicKeyspace [K , V ]) Set (ctx context.Context , key K , val V ) error {
502569 _ , _ , err := s .set (ctx , key , val , 0 , "set" )
503570 return err
0 commit comments