EXPIRE
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 expirtation time on the key, you can use the PERSIST command.
The command returns 1 if the expiry was set, and 0 if the key already had an expiry set. 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
locahost:7379> SET k1 v1OK OKlocahost:7379> EXPIRE k1 10OK 1locahost:7379> SET k2 v2OK OKlocahost:7379> EXPIRE k2 10 NXOK 1locahost:7379> EXPIRE k2 20 XXOK 1locahost:7379> EXPIRE k2 20 NXOK 0