我正在使用go-redis与REDIS服务器(版本3.2.100)进行交互。
根据Redis documentation,如果键不存在,则命令TTL应返回值-2。
但是,如果键不存在,方法TTL将返回一个表示某个持续时间(-2s)的值,而不是一个整数。
下面的代码说明了这种行为。
package main
import (
"github.com/go-redis/redis"
"fmt"
)
func main() {
fmt.Print("Create a REDIS client now.\n")
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
ttl, _ := client.TTL("MyKey").Result()
fmt.Printf("%v\n", ttl)
if ttl < 0 {
if -1 == ttl.Seconds() {
fmt.Print("The key will not expire.\n")
} else if -2 == ttl.Seconds() {
fmt.Print("The key does not exist.\n")
} else {
fmt.Printf("Unexpected error %d.\n", ttl.Seconds())
}
}
}输出:
Create a REDIS client now.
-2s
The key does not exist.可以吗?我认为GO方法TTL应该返回一个整数,而不是负的持续时间。
发布于 2018-04-13 23:13:33
从redis中获取已有密钥的TTL作为time.Duration会更有用。-1和-2是例外,断言为主要类型。如果TTL返回(*DurationCmd,error)可能会更方便,但我没有深入研究go-redis逻辑。我看不出有什么问题。只要考虑一下你总是得到time.Duration的结果。
https://stackoverflow.com/questions/49819777
复制相似问题