内容简介:使用redis的pipeline,批量获取redis中的key值时,报错,分析发现,批量获取时,exec后,会返回两种值:nil 及 redis.Nil,这种情况下,使用常规的 if err!=nil 的判断方法判断错误,是会报错的。
21天精品区块链课程免费学习,深入实战行家带路,助力开发者轻松玩转区块链! >>>
使用redis的pipeline,批量获取redis中的key值时,报错,分析发现,批量获取时,exec后,会返回两种值:nil 及 redis.Nil,
这种情况下,使用常规的 if err!=nil 的判断方法判断错误,是会报错的。
测试代码:
package main
import (
"github.com/go-redis/redis"
"log"
"strconv"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
Network: "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()
}
运行结果:
可以看到,如果批量获取的key中,有不存在的key,返回的会是redis.Nil,否则返回的是nil。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Glide获取缓存大小并清除缓存图片
- Guava 缓存批量获取的一个坑
- 惊讶!缓存刚Put再Get居然获取不到?
- 惊讶!缓存刚 Put 再 Get 居然获取不到?
- ADO.NET获取数据(DataSet)同时获取表的架构实例
- 根据 PID 获取 K8S Pod名称 - 反之 POD名称 获取 PID
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Coming of Age in Second Life
Tom Boellstorff / Princeton University Press / 2008-04-21 / USD 29.95
The gap between the virtual and the physical, and its effect on the ideas of personhood and relationships, is the most interesting aspect of Boellstorff's analysis... Boellstorff's portrayal of a virt......一起来看看 《Coming of Age in Second Life》 这本书的介绍吧!