go-redis
go-redis copied to clipboard
Cannot get value as []byte from MGet
When trying to get the values saved as []byte in cache using MGet, the interface value returned has the concrete type string instead of []byte. Why does this happen???
Expected Behavior
var redis = libredis.NewClient(myCfg)
func batch_set(ctx context.Context, keyValues map[string]any, expiration time.Duration) error{
cmds := make(map[string]*libredis.StatusCmd)
for key, value := range keyValues {
packed, err := msgpack.Marshal(value)
if err != nil {
return err
}
cmds[key] = pipe.Set(ctx, key, packed, expiration)
}
_, err := pipe.Exec(ctx)
if err != nil {
return err
}
return nil
}
func batch_get(ctx context.Context, keys []string)error {
myValues := c.redis.MGet(ctx, keys...).Val()
for i := range values {
if values[i] == nil {
continue
}
bytesValue, ok := values[i].([]byte) //Expected to work
if !ok {
return errors.New("error reading!")
}
myFunc((bytesValue)
return nil
}
Possible Solution
var redis = libredis.NewClient(myCfg)
func batch_set(ctx context.Context, keyValues map[string]any, expiration time.Duration) error{
cmds := make(map[string]*libredis.StatusCmd)
for key, value := range keyValues {
packed, err := msgpack.Marshal(value)
if err != nil {
return err
}
cmds[key] = pipe.Set(ctx, key, packed, expiration)
}
_, err := pipe.Exec(ctx)
if err != nil {
return err
}
return nil
}
func batch_get(ctx context.Context, keys []string)error {
myValues := c.redis.MGet(ctx, keys...).Val()
for i := range values {
if values[i] == nil {
continue
}
bytesValue := []byte(values[i].(string)) //This works
myFunc((bytesValue)
return nil
}