1 回答

TA贡献1951条经验 获得超3个赞
Redis 事务中的每个命令都有两个结果。一种是将命令添加到事务中的结果,另一种是在事务中执行命令的结果。
该Do方法返回将命令添加到事务的结果。
Redis EXEC命令返回一个数组,其中每个元素都是在事务中执行命令的结果。检查每个元素以检查单个命令错误:
values, err := redis.Values(redisClient.Do("EXEC"))
if err != nil {
// Handle error
}
if err, ok := values[0].(redis.Error); ok {
// Handle error for command 0.
// Adjust the index to match the actual index of
// of the HMSET command in the transaction.
}
用于测试事务命令错误的辅助函数可能很有用:
func execValues(reply interface{}, err error) ([]interface{}, error) {
if err != nil {
return nil, err
}
values, ok := reply.([]interface{})
if !ok {
return nil, fmt.Errorf("unexpected type for EXEC reply, got type %T", reply)
}
for _, v := range values {
if err, ok := v.(redis.Error); ok {
return values, err
}
}
return values, nil
}
像这样使用它:
values, err := execValues(redisClient.Do("EXEC"))
if err != nil {
// Handle error.
}
- 1 回答
- 0 关注
- 247 浏览
添加回答
举报