Redis 過期時間
阿新 • • 發佈:2020-08-14
expire命令
1、expire key seconds,其中seconds表示鍵過期的時間,單位是秒, 返回值1表示成功, 0表示設定失敗或者鍵不存在
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> expire foo 20
ttl命令,一個鍵還有多少時間會被刪除,返回的是鍵的剩餘時間, 當鍵不存在就會返回-2, 當鍵沒有設定過期時間(即永久存在)的情況下,返回的是-1
127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> expire foo 20 (integer) 1 127.0.0.1:6379> ttl foo (integer) 18 127.0.0.1:6379> ttl foo (integer) 13 127.0.0.1:6379> ttl foo (integer) -2 127.0.0.1:6379> get foo (nil)
expire命令seconds引數必須是整數,所以最小單位是1秒,如果想要精確的控制鍵的過期時間應該使用pexpire命令,對應的也可以用pttl命令返回以毫秒為單位的返回鍵的剩餘過期時間。
使用watch命令監測一個擁有過期時間的鍵,鍵到期自動刪除,不會被watch命令認為該鍵被改變
persist取消鍵的過期時間設定(即將鍵恢復到永久的),除此之外set或getset命令鍵賦值也會清除鍵的過期時間設定
127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> expire foo 20 (integer) 1 127.0.0.1:6379> persist foo (integer) 1 127.0.0.1:6379> ttl foo (integer) -1