第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Redis 不會將 WRONGTYPE 作為事務中的錯誤返回

Redis 不會將 WRONGTYPE 作為事務中的錯誤返回

Go
搖曳的薔薇 2022-07-18 17:02:37
首先,讓我展示如何重現(xiàn)我的問題:在 docker 容器中運行 Redis連接到 Redis 并執(zhí)行以下命令:> SET test 10在 Go 中,運行以下代碼:func main() {    redisClient := getConnection() // Abstracting get connection for simplicity    r, err := redisClient.Do("HSET", "test", "f1", "v1", "f2", "v2")    fmt.Printf("%+v e: %+v\n")}很公平,在此步驟中顯示以下錯誤(這意味著err != nil):WRONGTYPE Operation against a key holding the wrong kind of value e: WRONGTYPE Operation against a key holding the wrong kind of value相比之下,執(zhí)行以下代碼:func main() {    redisClient := getConnection()    redisClient.Send("MULTI")    redisClient.Send("HSET", "test", "f1", "v1", "f2", "v2")    r, err := redisClient.Do("EXEC")    fmt.Printf("%+v e: %+v\n")}正在打印的行是:WRONGTYPE Operation against a key holding the wrong kind of value e: <nil>這對我來說似乎不一致,因為我也希望在錯誤變量中MULTI返回。WRONGTYPE這是預期的行為還是我錯過了什么?
查看完整描述

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.

}


查看完整回答
反對 回復 2022-07-18
  • 1 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號