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.