21-day Boutique blockchain free learning, in-depth expert led the way, to help developers easily play the blockchain! >>>
Using Redis's pipeline, when retrieving the key value in Redis in bulk, the error, analysis found that when the batch acquisition, exec, will return two values: nil and Redis. Nil,
In this case, the use of conventional if Err!=nil judgment method to determine errors, will be error.
Test code:
package mainimport ("github.com/go-redis/redis""log""strconv")func main() {client := redis.NewClient(&redis.Options{Addr: "localhost:6379",Password: "", // no password setDB: 0, // use default DBNetwork: "tcp",PoolSize: 50,})if _, err := client.Ping().Result(); err != nil {panic(err)}pipe := client.Pipeline()log.Println("所有的key都存在:")for i := 0; i < 4; i++ {key := "n:" + strconv.FormatInt(int64(i), 10)pipe.Get(key)}result, err := pipe.Exec()log.Println(result)log.Println(err == nil)log.Println(err == redis.Nil)log.Println("有不存在的key:")for i := 0; i < 5; i++ {key := "n:" + strconv.FormatInt(int64(i), 10)pipe.Get(key)}result, err = pipe.Exec()log.Println(result)log.Println(err == nil)log.Println(err == redis.Nil)defer client.Close()}
Operation Result:
As you can see, if there is a nonexistent key in the bulk fetch key, the return will be redis. Nil, otherwise the return is nil.