Introduction of Redis expiration mechanism

Source: Internet
Author: User
Overview

In the actual development process will often encounter some time-sensitive data, such as limited time discount activities, caching or verification code, and so on. After a while, you need to delete this data. In a relational database, you typically add a field to record the expiration time of the data, and then periodically check for expired data and then delete it. Redis itself provides a good support for key expiration. Redis expiration mechanism

In Redis you can use the EXPIRE command to set the lifetime of a key (Ttl:time to live), over which time the key is automatically deleted, and the EXPIRE command is used as follows:

1
EXPIRE key TTL (per second)

A command return of 1 indicates that the TTL was set successfully, and 0 indicates that the key does not exist or the setting fails.

As an example:

1
2
3 4 5 6 7 8 9
Ten
11
127.0.0.1:6379> Set session +
OK
127.0.0.1:6379> EXPIRE session 5
(integer) # 1
127.0.0.1:6379> get session
"127.0.0.1:6379>"
127.0.0.1:6379> Get session
(nil)
127.0.0.1:6379>

The previous example shows that the value of the session is set to 100, and then set his TTL to 5s, and after several successive use of the GET command to obtain session,5s will not get a session, because the TTL time has arrived, the session is deleted.

If you want to know how long a key has been deleted, you can use the TTL command to view it, using the following methods:

1
TTL Key

The return value is the time remaining in the key, in seconds.

Like what:

1 2 3 4 5 6 7 8 9 A 
127.0.0.1:6379> Set session + OK 127.0.0.1:6379> EXPIRE session (integer) # 1 127.0.0.1:6379> TTL session (in Teger) 7 127.0.0.1:6379> ttl session (integer) 5 127.0.0.1:6379> ttl session (integer) 2 127.0.0.1:6379> ttl sess 
Ion (integer) 0 127.0.0.1:6379> ttl session (integer)-2 127.0.0.1:6379> ttl session (integer)-2 127.0.0.1:6379> 

Visible, the return value of the TTL will slowly decrease over time, the key will be deleted after 10s, the TTL will return when the key does not exist-2, and when no expiration time is set for the key, the remaining time to use the TTL fetch key will return-1, for example

1
2
3
4
5
127.0.0.1:6379> set URL http://qifuguang.me
OK
127.0.0.1:6379> ttl url
(integer)-1
127.0.0.1:6379>

If you want to cancel the expiration time of a key, you can use the PERSIST command as follows:

1
PERSIST Key

The purge successfully returns 1, and the failure returns 0.

For example:

1
2
3 4 5 6 7 8 9
Ten
11
127.0.0.1:6379> set title winwill2012
OK
127.0.0.1:6379> EXPIRE title
(integer) 1
127.0.0.1:6379> TTL title
(integer)
127.0.0.1:6379> PERSIST title
(integer) 1
127.0.0.1:6379> TTL title
(integer)-1
127.0.0.1:6379>

In addition to the persist command clears the expiration of the key, the Set,getset command clears the expiration of the key, but commands that operate on the key (such as Incr,lpush, and so on) do not erase the expiration of the key.

the unit of the expire command is seconds, and if you want more accurate expiration time, you can use the Pexpire command, which is in milliseconds, and you can use Pttl to see the remaining time accordingly.

if the watch command monitors a key that has an expiration time, if the key expires automatically during monitoring, watch does not think the key is changed use of the Redis expiration mechanism

With the expiration mechanism can achieve a lot of time-related functions, such as access frequency restrictions, as a cache, and so on, the specific details will not unfold, there are questions can leave a message. Statement

This article original, reprint please indicate the source, this article link: Http://qifuguang.me/2015/09/30/Redis Expiration mechanism Introduction/

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.