redis Redis Keys TTL and Key Expiration

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any redis Question?