Expiration time setting
Setting the expiration time refers to setting a time on the key so that the key survives within this time. After this time, the key and Its corresponding value; the expiration time is generally set in redis instead of using the del command to eliminate elements;
Once the expiration time is set, this key can only be commanded Clear, delete or rewrite its contents. These commands include del, set, getset, and all *store commands. These commands can only change the stored value of the value corresponding to the key without changing the expiration time setting.
For example: use incr to change the value corresponding to the key, use lpush to add a new element to the lists, use hset to set the value corresponding to the field, etc., these operations will not affect the key settings. The expiration time attribute.
1. Implemented through the expire command: demo: expire key 10 ----->Set the expiration time for the given key to 10 seconds
2. Implemented through the setex command: demo: setex key 10 value ----->Set the value of the key to value and survive for 10 seconds--->The value for the key is of String type;
The above two methods are for the key and set the expiration time for it; if you want to set the expiration time for a certain part of the data (other data types) in the value, you need to use other methods;
Keys with an expiration time set can still be re-persisted using the persist command. (The PERSIST command can remove the expiration time of a key)
Note: After the rename command renames the key, all the attributes corresponding to the original key will be transferred. If the key has an expiration time set and has not been deleted, after using the rename command to rename it, the expiration time will be transferred to the new key.
If a negative value is passed as a parameter when calling expire or pexpire and the timestamp has passed when expireat or pexpireat is called, the key will be deleted directly instead of waiting for expiration.
Refresh expiration time
For a key with an expiration time set, you can still call expire to update its expiration time.
Return value
Setting the expiration time will return an integer value;
1) If the expiration time is If the setting is successful, 1 will be returned;
2) If the setting fails or the key does not exist, 0 will be returned;
The above is the detailed content of How long is the appropriate time to set the redis expiration time?. For more information, please follow other related articles on the PHP Chinese website!