Set the data expiration time by expire the key seconds command. A return of 1 indicates that the setting was successful, and returning 0 indicates that the key does not exist or the expiration is not successfully set.
After the expiration time is set on the key, the key will be automatically deleted after the specified number of seconds. Keys that have been assigned an expiration time are known to be unstable in Redis.
The expiration time associated with a key being deleted by the del command or reset by the set, Getset command is cleared.
Redis 127.0.0.1:6379> set MyKey "test expire" Okredis 127.0.0.1:6379> expire MyKey (integer) 1redis 127.0.0.1:637 9> ttl mykey (integer) 97redis 127.0.0.1:6379> ttl mykey (integer) 93redis 127.0.0.1:6379> set MyKey "test expire R Eset "Okredis 127.0.0.1:6379> ttl mykey (integer) -1redis 127.0.0.1:6379> set MyKey" test expire "Okredis 127.0.0.1:6379> expire MyKey (integer) 1redis 127.0.0.1:6379> ttl mykey (integer) 98redis 127.0.0.1:6379> ttl MyKey (integer) 91redis 127.0.0.1:6379> getset mykey "test expire reset" "Test Expire" Redis 127.0.0.1:6379> ttl MyKey (integer)-1
This also means that only conceptually updating the value stored in a key without replacing the original value with the new value will affect the expiration time set on that key. For example, using the INCR command to increase the value of a key or to add a new element to the list by using the Lpush command or to update the value of the hash field with the Hset command will clear the old expiration setting.
Redis 127.0.0.1:6379> set MyKey 1OKredis 127.0.0.1:6379> expire MyKey (integer) 1redis 127.0.0.1:6379> ttl Myk EY (integer) 95redis 127.0.0.1:6379> incr mykey (integer) 2redis 127.0.0.1:6379> ttl mykey (integer) 77redis 127.0.0.1:6379> lpush listkey 1 (integer) 1redis 127.0.0.1:6379> expire Listkey (integer) 1redis 127.0.0.1:6379& Gt TTL listkey (integer) 94redis 127.0.0.1:6379> lpush listkey 2 (integer) 2redis 127.0.0.1:6379> ttl listkey (integer) 8 2redis 127.0.0.1:6379> hmset hashkey name "Redis" passwd "Redis" Okredis 127.0.0.1:6379> expire HashKey (integer) 1redis 127.0.0.1:6379> ttl hashkey (integer) 95redis 127.0.0.1:6379> hset hashkey passwd "Redis.vs.mysql" ( Integer) 0redis 127.0.0.1:6379> ttl hashkey (integer) 66
Of course, you can also reset the key to persistent by clearing the set expiration time by persist command.
Redis 127.0.0.1:6379> set MyKey ' Test clear expire ' Okredis 127.0.0.1:6379> expire MyKey (integer) 1redis 127.0.0. 1:6379> ttl mykey (integer) 96redis 127.0.0.1:6379> persist MyKey (integer) 1redis 127.0.0.1:6379> ttl MyKey ( Integer)-1
If key is renamed by the Rename command, the expiration time associated with it will be passed to the new name key.
Redis 127.0.0.1:6379> get Mykeynew (nil) Redis 127.0.0.1:6379> set MyKey ' Test expire transfer ' Okredis 127.0.0.1:6379> expire MyKey (integer) 1redis 127.0.0.1:6379> ttl mykey (integer) 96redis 127.0.0.1:6379> Rename MyKey Mykeynewokredis 127.0.0.1:6379> ttl mykey (integer) -1redis 127.0.0.1:6379> ttl mykeynew (integer) 80
If key is rewritten by the rename command, such as the existence of a key named Mykey_a and Mykey_b, a rename mykey_b mykey_a command to rename Mykey_b to the already existing mykey_a then no matter mykey_ A the original setting will inherit all the attributes of Mykey_b, including the expiration Time setting.
Redis 127.0.0.1:6379> Set Mykey_b ' B ' okredis 127.0.0.1:6379> set mykey_a ' a ' okredis 127.0.0.1:6379> expire MyKey _b (integer) 1redis 127.0.0.1:6379> ttl mykey_b (integer) 93redis 127.0.0.1:6379> ttl mykey_a (integer) -1redis 12 7.0.0.1:6379> rename mykey_b mykey_aokredis 127.0.0.1:6379> ttl mykey_b (integer) -1redis 127.0.0.1:6379> ttl Mykey_a (integer) 66
EXPIRE key seconds is applied to a key that has an expiration time set, the original expiration time will be updated to the new expiration time
Redis 127.0.0.1:6379> set MyKey ' test expire update ' Okredis 127.0.0.1:6379> expire MyKey (integer) 1redis 127.0.0 .1:6379> ttl mykey (integer) 95redis 127.0.0.1:6379> expire MyKey (integer) 1redis 127.0.0.1:6379> ttl MyKey ( Integer) 998
Appendix:
expiry time of key
Typically, when a Redis key is created, the expiration time is not automatically associated, and the key will persist until the deletion is displayed via a command such as Del. The expire command cluster can associate an expiration time for the specified key at the expense of a little extra memory overhead. When the key is set to expire, Redis guarantees that the key will be removed when it times out. The expiration time of key can be updated by the expire command or completely removed by the persist command.
accuracy of expiration time
Redis2.4 expire accuracy is not high, usually in 0-1 seconds, after Redis2.6 expire accuracy can be controlled within 0 to 1 milliseconds.
Expiration and persistence
The expiration information for key is stored in the form of an absolute UNIX timestamp (after Redis2.6 is stored in millisecond-level precision). This means that even if the Redis instance is not running, it will not affect the expiration time of the key.
In order for the expiration time to work more correctly, you must check the computer's time. If you move an RDB snapshot from one computer to another that is ahead of time or lags behind, something fun will happen, such as the data is just loaded and timed out. Even in the same instance of the same machine if the time of the computer is incorrect or has changed to cause problems, such as the expiration of a batch of data is set to 1000, then the computer time unexpectedly 2000 seconds faster, then these keys will expire immediately, instead of 1000 seconds after expiration.
how Redis makes key expire
Redis key expires in two ways:
Passive mode and active mode
When clients tries to access a key that has an expiration time and has expired, it is the active expiration method.
But this is not enough, thinking that there may be some key will never be accessed again, these settings expire the key also need to be deleted after expiration. As a result, redis periodically randomly tests a batch of keys that have an expiration time set and are processed. The expired key that was tested is deleted. In a typical way, Redis does the following steps 10 times per second:
1. Random test 100 Set the expiration time of the key
2. Delete all discovered expired keys
3. Repeat steps 1 if you delete more than 25 keys
This is a simple algorithm based on probability, the basic assumption is that the extracted sample can represent the entire key space, and Redis continuously cleans up the expired data until the percentage of the key that will expire drops below 25%. This also means that the amount of the key that has expired at any given moment but still occupies the memory space is up to the write operation amount per second divided by 4.
replication expiration processing in link and aof files
In order to get the correct behavior without causing a consistency problem, when a key expires, the Del operation is recorded in the AoF file and passed to all related slave. The expiration delete operation is uniformly carried out in the master instance and passed down, rather than each salve in its own hands. This will not result in inconsistent data. When the slave is connected to master, it does not immediately clean up the expired key (which needs to wait for the Del operation passed by master), Slave still needs to manage the expiration status in the data set to make it easier for slave to be promoted to master, which can be processed as independently as master.
Research on REDIS data expiration policy