Redis sets the key expiration time

Source: Internet
Author: User
Redis is a widely used key-value memory database. Like memecached, keys can be set to a life cycle. Setting the key expiration time in redis is very simple: SETEXmykey10Hello sets a 10-Second Life Cycle for mykey. However, when you use other redis data structures, such as LIST and HMAP

Redis is a widely used key-value memory database. Like memecached, keys can be set to a life cycle. Setting the key expiration time in redis is very simple: SETEX mykey 10 Hello so that a 10-Second Life Cycle is set for mykey. However, when you use other redis data structures, such as LIST and HMAP

Redis is a widely used key-value memory database. Like memecached, keys can be set to a life cycle.

Redis sets the key expiration time very easily:

SETEX mykey 10 "Hello"

In this way, a 10-Second Life Cycle is set for mykey.

However, when you use other redis data structures, such as LIST and HMAP, there are no ready-to-use commands. However, in redis, the above commands are equivalent to the following two commands.

SET mykey value
EXPIRE mykey 10

EXPIRE can set the expiration time for any type of keys in redis. For example, you can use
HSET mykey hkey "hello"EXPIRE mykey 10 

We can store two commands as a MULTI/EXEC stored procedure, or use a redis client that supports pipline.

Next we will talk about the mechanism for deleting expired redis keys:

Unlike the LAZY mode of memcached, redis uses the passive mode and the active mode for keys with an expiration time:

Passive Mode: Same as memcached. When an expired key is accessed, redis will delete it from the active mode. The active mode is a loop triggered by redis actively at an interval of 10 seconds: randomly test the expiration time of 100 keys to delete all expired keys of the 100 keys. If more than 25 keys are deleted, redo them immediately from step 1.

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.