Available since 0.09.
Time complexity
O(1)
Set a timeout on key. After the timeout has expired, the key will
automatically be deleted. A key with an associated timeout is said to be
volatile in Redis terminology.
If key is updated before the timeout has expired, then the timeout is removed
as if the PERSIST command was invoked on key.
For Redis versions < 2.1.3, existing timeouts cannot be overwritten. So, if
key already has an associated timeout, it will do nothing and return 0.
Since Redis 2.1.3, you can update the timeout of a key. It is also possible
to remove the timeout using the PERSIST command. See the page on key expiry
for more information.
Note that in Redis 2.4 the expire might not be pin-point accurate, and it could be between zero to one seconds out. Development versions of Redis fixed this bug and Redis 2.6 will feature a millisecond precision EXPIRE.
Return value
Integer reply, specifically:
1if the timeout was set.0ifkeydoes not exist or the timeout could not be set.
Examples
OKredis> EXPIRE mykey 10
(integer) 1redis> TTL mykey
(integer) 10redis> SET mykey "Hello World"
OKredis> TTL mykey
(integer) -1