PERSIST
The PERSIST
command is used to remove the expiration from a key in DiceDB. If a key is set to expire after a certain amount of time, using the PERSIST
command will make the key persistent, meaning it will no longer have an expiration time and will remain in the database until explicitly deleted.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the key to persist. | String | Yes |
Return Value
Condition | Return Value |
---|---|
The timeout was successfully removed | 1 |
The key does not exist or does not have a timeout | 0 |
Behaviour
When the PERSIST
command is executed:
- If the specified key has an expiration time, the
PERSIST
command removes that expiration, making the key persistent. - If the key does not exist or does not have an expiration, the command does nothing and returns
0
. - This command does not alter the key’s value, only its expiration state.
Errors
Wrong type of value or key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs when attempting to use the command on a key that contains a non-string value or one that does not support expiration.
- Error Message:
No timeout to persist
:- This is not an error but occurs when the key either does not exist or does not have an expiration time. The command will return
0
in such cases.
- This is not an error but occurs when the key either does not exist or does not have an expiration time. The command will return
Example Usage
Removing Expiration from a Key
SET mykey "Hello"
: Sets the value ofmykey
to “Hello”.EXPIRE mykey 10
: Sets an expiration of 10 seconds onmykey
.PERSIST mykey
: Removes the expiration frommykey
.TTL mykey
: Returns-1
, indicating thatmykey
does not have an expiration time.
Attempting to Persist a Non-Existent Key
- The command returns
0
becausemykey
does not exist in the database.
Persisting a Key Without Expiration
- The command returns
0
becausemykey
does not have an expiration time set.
Conclusion
The PERSIST
command is a useful tool for managing the lifecycle of keys in a DiceDB database. By removing the expiration from a key, you can ensure that the key remains in the database until explicitly deleted. This command is straightforward but powerful, allowing for greater control over key persistence.