redis Redis Keys TTL and Key Expiration


Example

The expiration values of a key can be managed by a user outside of the update commands. Redis allows a user to determine the current time to live (TTL) of a key using the TTL command:

TTL key

This command will return the TTL of a key in seconds or will return the special values -1 or -2. A -1 indicates that the key is persistent (won't expire) and a -2 indicates that the key does not exist.

An expiring key can be made persistent using the PERSIST command:

PERSIST KEY

and a persistent key can be made to expire using the EXPIRE command:

EXPIRE KEY seconds

Expire can also be used to modify the TTL of an existing key. Alternatively, you can use the EXPIREAT command with a UNIX timestamp to set an expire time.

There are millisecond versions of TTL, EXPIRE and EXPIREAT commands that are prefixed with a P.