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 period, 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
- If the specified key has an expiration time, the
PERSIST
command will remove that expiration, ensuring the key remains in the database. - If the key does not have an expiration or if the key does not exist, the command returns
0
and no action is taken. - The value of the key remains unchanged. Only the expiration is affected.
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
Explanation:
- The
PERSIST
command removes the expiration frommykey
, and theTTL
command confirms that the key no longer has a time-to-live (TTL) value (-1
indicates no expiration).
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
Explanation:
- The command returns
0
because the keymykey
does not exist in the database.
Persisting a Key Without Expiration
Explanation:
- 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.