EXPIRE
EXPIRE Key seconds
For a given key
set time to Live, key
0
it will be automatically deleted when it expires (time to live).
In Redis, the time-to-live key
is referred to as "volatile" (volatile).
The time to live can be removed by using the DEL command key
, or by the SET and Getset commands (overwrite), which means that if a command simply modifies (alter) a value with a time-to-live key
instead of a new key
value Instead of it, the time to live is not changed.
For example, for an key
execute INCR command, a lpush command on a list, or a hset command on a hash table, such operations do not modify key
their own time-to-live.
On the other hand, if one is renamed with RENAME , the time to change is the key
key
same as before renaming.
Another possibility of the RENAME command is to attempt to rename a time-to-live with key
another time-to-live another_key
, when the old another_key
(and its time-to-live) is deleted, and the old one is key
renamed another_key
, so the new is the same as the original key
.
Use the PERSIST command to remove the time-to-live without deleting it key
key
, and to make it key
a "persistent" (persistent) again key
.
Update Time to Live
The newly specified time-to-live will replace the old time-to-live with an key
execution EXPIRE command that already has a time-to-live.
Accuracy of expiration Time
In the Redis 2.4 release, the expiration time is delayed by 1 seconds-that is, even if key
it has expired, it may be accessed within one second of expiration, and in the new Redis 2.6 release, the latency is reduced to 1 milliseconds.
What's different before Redis 2.1.3
In the previous version of Redis 2.1.3, modifying a time-to-live result in the key
entire key
deletion, which was made by the limitations of the then-duplicated (replication) layer, is now fixed.
-
Available versions:
-
>= 1.0.0
-
Complexity of Time:
-
O (1)
-
return value:
-
set to return successfully
1
.
key
returned if it does not exist or is unable to set the time-to-
key
live (for example, in Redis under 2.1.3, if you try to update
key
the lifetime)
0
.
Redis> set Cache_page "www.google.com" okredis> EXPIRE cache_page # Set expiration time is 30 seconds (integer) 1redis> TTL Cach E_page # View remaining time to Live (integer) 23redis> EXPIRE cache_page 30000 # Update expiration (integer) 1redis> TTL cache_page ( Integer) 29996
Mode: Navigation session
Suppose you have a Web service that intends to recommend items based on the N pages visited by the user recently, and assumes that the user has stopped viewing for more than 60 seconds, then clear the reading record (to reduce the amount of recommended calculations and keep the recommended items fresh).
These recently visited Page records, which we call "Navigation Sessions" (Navigation session), can be implemented in Redis with the INCR and Rpush commands: Whenever a user views a webpage, execute the following code:
MULTI rpush pagewviews.user:<userid>/http/... .. EXPIRE pagewviews.user:<userid> 60EXEC
If the user stops reading for more than 60 seconds, then its navigation session will be emptied, and when the user resumes viewing, the system will re-record the navigation session, continue to carry out the item recommendation.
Links: http://redisdoc.com/key/expire.html
Redis Command Reference "EXPIRE"