EXPIRE
Syntax
Section titled “Syntax”EXPIRE key seconds [NX | XX]
EXPIRE sets an expiry (in seconds) on a specified key. After the expiry time has elapsed, the key will be automatically deleted.
If you want to delete the expiration time on the key, you can use the PERSIST command.
The command returns true if the expiry was set (changed), and false if the expiry could not be set (changed) due to key not being present or due to the provided sub-command conditions not being met. The command supports the following options:
- NX: Set the expiration only if the key does not already have an expiration time.
- XX: Set the expiration only if the key already has an expiration time.
Examples
Section titled “Examples”localhost:7379> SET k1 v1OKlocalhost:7379> EXPIRE k1 10OK truelocalhost:7379> SET k2 v2OKlocalhost:7379> EXPIRE k2 10 NXOK truelocalhost:7379> EXPIRE k2 20 XXOK truelocalhost:7379> EXPIRE k2 20 NXOK false