EXPIREKeyseconds
Available since 1.0.0.
Time Complexity: O (1)
Sets an expiration time for the specified key. When the expiration time expires, the key will be deleted automatically. If you use the DEL command to remove this key, either use the SET command or the Getset command to reset the key value. The expiration time will be cleared. Use persist to convert this key to a persistent key, or to clear the expiration time.
If you use Rename to rename a key, the expiration time of the key association will be converted to the expiration time of the new key.
You can use expire to update the expiration time of a key
The Expiration time information for key is stored as a Unix timestamp. This means that even if the Redis instance is not running, the expiration time will slowly decrease. For example, if you set a key with a TTL of 1000 seconds, then set the current time of the computer to 2000 seconds in the future, then the key will expire immediately instead of waiting for 1000 seconds before expiring.
How Redis expires Keys
There are two types of Redis key expiration policies: passive and active.
If a client tries to access a key and discovers that the key is obsolete, this is the active expiration of key.
However, if some key is not accessed by the client, these keys will expire anyway. Redis randomly tests some keys that set the expiration time, and if it expires, delete the keys.
Specifically, Redis will do the following 10 times per second:
Test 20 keys with expiration time set
Delete keys that have been found to have expired
If you find that the extra 25 keys have expired, start from the beginning of the first step.
Reference Documentation:
Http://redis.io/commands/expire
This article is from the Linux SA John blog, so be sure to keep this source http://john88wang.blog.51cto.com/2165294/1629149
Expiration policy for Redis