@@ -331,13 +331,12 @@ func (r *Redis) Get(
331331
332332// MGet performs a batch GET operation for the given keys using Redis.
333333//
334- // It expects the number of returned values to exactly match the number
335- // of requested keys. If the underlying Redis call fails, or the response
336- // is incomplete or invalid, the method returns ErrFailedToMGetValues .
334+ // It returns a map where each successfully fetched key is mapped to its
335+ // corresponding string value. Keys that are missing in Redis or whose
336+ // values cannot be cast to strings are silently skipped .
337337//
338- // The returned map contains each requested key mapped to its corresponding
339- // value. If a key does not exist or the value cannot be cast to string,
340- // it will be mapped to nil.
338+ // Unlike atomic variants, this method does not fail if some keys are missing;
339+ // the resulting map may contain fewer entries than requested.
341340//
342341// Example:
343342//
@@ -347,20 +346,16 @@ func (r *Redis) Get(
347346// log.Fatalf("failed to fetch keys: %v", err)
348347// }
349348// for k, v := range values {
350- // if v != nil {
351- // fmt.Printf("%s = %s\n", k, *v)
352- // } else {
353- // fmt.Printf("%s = <nil>\n", k)
354- // }
349+ // fmt.Printf("%s = %s\n", k, v)
355350// }
356351//
357352// Returns:
358- // - map[string]* string: keys mapped to their corresponding string values (or nil)
359- // - yaerrors.Error: wrapped error if Redis call fails or result is inconsistent
353+ // - map[string]string: found keys mapped to their string values
354+ // - yaerrors.Error: wrapped Redis error if the MGET command fails
360355func (r * Redis ) MGet (
361356 ctx context.Context ,
362357 keys ... string ,
363- ) (map [string ]* string , yaerrors.Error ) {
358+ ) (map [string ]string , yaerrors.Error ) {
364359 values , err := r .client .MGet (ctx , keys ... ).Result ()
365360 if err != nil {
366361 return nil , yaerrors .FromError (
@@ -370,24 +365,19 @@ func (r *Redis) MGet(
370365 )
371366 }
372367
373- result := make (map [string ]* string )
368+ result := make (map [string ]string )
374369
375370 for i , key := range keys {
376371 if values [i ] == nil {
377- result [key ] = nil
378-
379372 continue
380373 }
381374
382375 value , ok := values [i ].(string )
383376 if ! ok {
384- result [key ] = nil
385-
386377 continue
387378 }
388379
389- v := value
390- result [key ] = & v
380+ result [key ] = value
391381 }
392382
393383 return result , nil
0 commit comments