How to get get value from redis.Cmder in golang go-redis?

huangapple go评论89阅读模式
英文:

How to get get value from redis.Cmder in golang go-redis?

问题

temp1Ctxtemp1Cancer := lib.GetTimeoutCtx(ctx)
pipeline := util.RedisClusterClient.Pipeline()
for _, key := range userIdRedisSlice {
    pipeline.HMGet(temp1Ctx, key, userIdRedisFeature...)
}
userProfileerr := pipeline.Exec(temp1Ctx)
if err != nil {
    lib.ErrorLogger.Errorf(": %v\n", err)
}
defer temp1Cancer()
// lib.ErrorLogger.Infof(": %v", userProfile)

for _, redisCmd := range userProfile {
    //TODO
}

如何从中获取值?我找不到任何文档......

英文:
	temp1Ctx, temp1Cancer := lib.GetTimeoutCtx(ctx)
	pipeline := util.RedisClusterClient.Pipeline()
	for _, key := range userIdRedisSlice {
		pipeline.HMGet(temp1Ctx, key, userIdRedisFeature...)
	}
	userProfile, err := pipeline.Exec(temp1Ctx)
	if err != nil {
		lib.ErrorLogger.Errorf(": %v\n", err)
	}
	defer temp1Cancer()
	// lib.ErrorLogger.Infof(": %v", userProfile)

	for _, redisCmd := range userProfile {
		//TODO
	}

How to get the value from it ? I don't find any document .......

答案1

得分: 2

保留HMGet返回的具体命令类型:

temp1Ctx, temp1Cancer := lib.GetTimeoutCtx(ctx)
pipeline := util.RedisClusterClient.Pipeline()
cmds := []*redis.SliceCmd{}
for _, key := range userIdRedisSlice {
    cmds = append(cmds, pipeline.HMGet(temp1Ctx, key, userIdRedisFeature...))
}
if _, err := pipeline.Exec(temp1Ctx); err != nil {
    lib.ErrorLogger.Errorf(": %v\n", err)
}
defer temp1Cancer()

for _, c := range cmds {
    // 使用 c.Result()
    // 或者使用 c.Scan
}

或者使用类型断言/类型切换将Cmder转换为具体类型:

temp1Ctx, temp1Cancer := lib.GetTimeoutCtx(ctx)
pipeline := util.RedisClusterClient.Pipeline()
for _, key := range userIdRedisSlice {
    pipeline.HMGet(temp1Ctx, key, userIdRedisFeature...)
}
userProfile, err := pipeline.Exec(temp1Ctx)
if err != nil {
    lib.ErrorLogger.Errorf(": %v\n", err)
}
defer temp1Cancer()

for _, redisCmd := range userProfile {
    switch c := redisCmd.(type) {
    case *redis.SliceCmd:
          // 使用 c.Result()
          // 或者 c.Scan()
    }
}
英文:

Either retain the concrete command type returned by HMGet

temp1Ctx, temp1Cancer := lib.GetTimeoutCtx(ctx)
pipeline := util.RedisClusterClient.Pipeline()
cmds := []*redis.SliceCmd{}
for _, key := range userIdRedisSlice {
    cmds = append(cmds, pipeline.HMGet(temp1Ctx, key, userIdRedisFeature...))
}
if _, err := pipeline.Exec(temp1Ctx); err != nil {
    lib.ErrorLogger.Errorf(": %v\n", err)
}
defer temp1Cancer()

for _, c := range cmds {
    // use c.Result()
    // or use c.Scan
}

or type-assert / type-switch the Cmder to the concrete type.

temp1Ctx, temp1Cancer := lib.GetTimeoutCtx(ctx)
pipeline := util.RedisClusterClient.Pipeline()
for _, key := range userIdRedisSlice {
    pipeline.HMGet(temp1Ctx, key, userIdRedisFeature...)
}
userProfile, err := pipeline.Exec(temp1Ctx)
if err != nil {
    lib.ErrorLogger.Errorf(": %v\n", err)
}
defer temp1Cancer()

for _, redisCmd := range userProfile {
    switch c := redisCmd.(type) {
    case *redis.SliceCmd:
          // use c.Result()
          // or c.Scan()
    }
}

huangapple
  • 本文由 发表于 2021年7月1日 19:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/68209052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定