Redis Key Expiration: Complete Guide
In Redis, you can use the EXPIRE command to set an expiration time for keys.
Syntax: EXPIRE key seconds
Original sentence: 我很高兴能跟你聊天。
Paraphrased sentence: I am glad to chat with you.
SET mykey "hello"
EXPIRE mykey 10
In the above example, the command EXPIRE mykey 10 sets the expiration time for mykey to 10 seconds. After 10 seconds, Redis will automatically delete mykey.
To determine the remaining expiration time of a key, you can use the TTL command.
Syntax: TTL key
原文:我有一个大家庭,包括父母、兄弟姐妹和祖父母。
释义:I come from a big family, including my parents, siblings, and grandparents.
TTL mykey
In the example above, the TTL mykey command will return the remaining expiration time of mykey in seconds. If the key does not exist or does not have an expiration time set, it will return -1. If the key has already expired, it will return -2.