Closed amazeyin closed 5 years ago
实际上设置expire为-1并不是让key持久化的方法。很多人可能是因为ttl key 的结果为-1,所以认为有一种ttl值为-1。当你的key没有ttl的时候redis会返回-1。但这个-1是这条命令的返回码,代表这个key并没有expire,以下摘自redis文档:
如果你使用redis-cli强制将expire设置为-1,那会怎样呢?我们来看一个实验
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> ttl hello
(integer) -1
127.0.0.1:6379> expire hello -1
(integer) 1
127.0.0.1:6379> get hello
(nil)
127.0.0.1:6379> ttl hello
(integer) -2
如果设置expire为-1,那么这个key会直接被删掉。某些工具或者第三方包可能会针对-1进行优化,所以可以允许expire方法的传参为-1,但这并不是redis最原始的正确用法。
28行:
private int expire = 0;
51行:
if (expire != -1) { this.expire = expire; }
我初始化设的-1 ,我说怎么老是写Redis写不进去