Key expiration for redis additional features

Source: Internet
Author: User

I. commands related to the key expiration Function

Ii. Set the survival time

Redis provides two commands to set the TTL (time to live) of keys, which are:

If the given key does not exist, both expire and pexpire return 0, indicating that the setting failed. If the command returns 1, the setting is successful.
After a key is set to survive, its survival time will be reduced with the passage of time: the last millisecond, the key survival time will be reduced by one millisecond; in the past one second, the key survival time is reduced by one second, and so on.
WhenWhen the survival time of the key is reduced to less than 0, redis will automatically delete the key.

Expire example:

Redis> set MSG "Hello World" okredis> expire MSG 5 # Set the survival time (integer) 1 redis> exists MSG # still exist (integer) 1 redis> exists MSG # automatically deleted (integer) 0

 

Strictly speaking, if you set the survival time to 5, the key will be stored for 6 seconds.

Pexpire example:

redis> PEXPIRE number 5500(integer) 1redis> EXISTS number(integer) 1redis> EXISTS number(integer) 0

 

Note 1: One second = 1000 milliseconds
NOTE 2: redis checks whether the key expires every 100 milliseconds by default. Therefore, the table above is also listed in every 100 milliseconds.

3. Set the expiration time

Redis provides two commands to set the key expiration time (expire Time ):

If the given key does not exist, both expireat and pexpireat will return 0, indicating that the setting failed. If the command returns 1, the setting is successful.
For keys with an expiration time set, whenWhen the key expiration time is earlier than the current time, redis will automatically delete the key.

Expireat example:

Redis> set MSG "Time to go" okredis> expireat MSG 1000000005 (integer) 1 redis> exists MSG # Before 1000000005 (integer) 1 redis> exists MSG # After 1000000005 (integer) 0

 

Pexpireat example:

Redis> set song "last night, good night" okredis> pexpireat song 1000000005500 (integer) 1 redis> exists song #1000000005500 (integer) 1 redis> exists song # After 1000000005500 (integer) 0

 

Iv. Differences between survival time and expiration time

Setting the survival time and setting the expiration time allow redis to automatically delete the specified key. The difference is thatThe setting of "when the key is to be deleted" is different.:

Expire and pexire are used to delete keys after n seconds or N milliseconds.
Expireat and pexpireat are used to delete a key after the specified UNIX time is reached.

A key with a life time is like a countdown timer, which is counted down to 5, 4, 3, 2, 1, and 0, and then deleted.
A key with an expiration time is like a timer, which will be deleted after the specified time comes.

5. Remaining survival time of the Return key

After setting the expiration time or survival time for a key, you can use the TTL command or the pttl command to view the remaining survival time of the key, so as to know how long the key will be deleted by redis:

TTL and pttl may return three different values:

Example of TTL and pttl:

redis> SET msg "hello"OKredis> EXPIRE msg 10086(integer) 1redis> TTL msg(integer) 10083redis> PTTL msg(integer) 10079336

 

Redis> set number 10086 okredis> expireat number 1408498480 (integer) 1 redis> TTL number # distance 14... 80 seconds (integer) 15 redis> pttl number # distance 14... 80 in milliseconds (integer) 11651

Note:Redis does not provide the command to view the expiration time of a key, so for a key with an expiration time set, we can only use TTL and pttl to view its remaining survival time.

6. expiration time or survival time of the remove key

PERSIST key

 

Remove the expiration time or survival time set for the key so that it will not be automatically deleted by redis.
If the removal is successful, the command returns 1. If the command does not set the expiration time or survival time, the command returns 0.

Persist example:

redis> SET msg "hello"OKredis> EXPIRE msg 30(integer) 1redis> TTL msg(integer) 28redis> PERSIST msg(integer) 1redis> TTL msg(integer) -1

 

redis> SET number 10086OKredis> EXPIREAT number 1408499100(integer) 1redis> TTL number(integer) 25redis> PERSIST number(integer) 1redis> TTL number(integer) -1

 

Key expiration for redis additional features

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.