1 回答

TA貢獻1951條經驗 獲得超3個贊
Redis 事務中的每個命令都有兩個結果。一種是將命令添加到事務中的結果,另一種是在事務中執(zhí)行命令的結果。
該Do方法返回將命令添加到事務的結果。
Redis EXEC命令返回一個數(shù)組,其中每個元素都是在事務中執(zhí)行命令的結果。檢查每個元素以檢查單個命令錯誤:
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.
}
用于測試事務命令錯誤的輔助函數(shù)可能很有用:
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 關注
- 157 瀏覽
添加回答
舉報