Use the expire command in redis to set the expiration time of a key. Redis will automatically delete him when the time comes.
expert key seconds among seconds Parameter indicates the expiration time of the key.
127.0.0.1:6379> EXPIRE key 10 //Set the key expiration time to 10s (integer) 1 127.0.0.1:6379> ttl key // ttl view expiration time of key (integer) -2 //Returns - 2 when the key does not exist and never expires to - 1 127.0.0.1:6379> expire The command will reset the expiration time of the key Other commands that do this only for keys( incr lpush hset zrem)
Note: the unit in expire key seconds can only be an integer, and the minimum unit is 1s
pexpire: more accurate than expire, in milliseconds
Key points: implement access frequency restriction
Redis implements interface access frequency restrictions
Why restrict access frequency
When making a service interface, you usually need to use the request frequency limit Rate limiting. For example, a user can be limited to 100 times within one minute
It is mainly used to ensure service performance and protect data security
If no restrictions are imposed, service callers can access the service at will. If they want to adjust it several times, it will cause great pressure on the service and reduce performance. For example, some interfaces need to verify the identity of the caller. If no access restrictions are imposed, callers can have a violent try
Redis is used to implement
Redis can easily realize the frequency limit function. Here are two good methods
(1) Scenario 1 - Lua script
Encapsulating the restriction logic into a Lua script, calls only need to be passed in: key, the number of constraints, and the expiration date. The result of the call will indicate whether or not to run the access.
The code is as follows:
local notexists = redis.call(\"set\", KEYS, 1, \"NX\", \"EX\", tonumber(ARGV)) if (notexists) then return 1 end local current = tonumber(redis.call(\"get\", KEYS)) if (current == nil) then local result = redis.call(\"incr\", KEYS) redis.call(\"expire\", KEYS, tonumber(ARGV)) return result end if (current >= tonumber(ARGV)) then error(\"too many requests\") end local result = redis.call(\"incr\", KEYS) return result
Using eval calls
eval script 1 key parameter - maximum number of times allowed parameter - expiration time
(2) Option 2 - expansion module
Redis4 opens the module system. You can develop your own modules and insert them into redis. Redis officials have recommended an access restriction module redis cell, which can meet the requirements with only one command
CL.THROTTLE user123 15 30 60 ● user123 yes key ● 15 Is the maximum quota quantity ● 30 Is the number of times that can be accessed ● 60 Is the time period in seconds
Taken together, the maximum resource quota of user123 is 15, and it can be accessed up to 30 times in 60 seconds
- (integer) 0 # 0 allow; 1 reject 2) (integer) 16 # total quota 3) (integer) 15 # remaining quota 4) (integer) -1 # retry in a few seconds, - 1 indicates no restriction. When the first item is 0, here is - 1 5) (integer) 2 # restore the maximum value in a few seconds
Each time this command is executed, the remaining quota will be reduced by 1. When the quota is insufficient or the number of accesses exceeds the limit, it will be rejected
There are many ways to implement frequency restriction. For example, both Nginx and Haproxy have restriction modules, and Guava can be used in Java. Redis is also a common way to implement frequency restriction
At present, scheme 1 represents the mainstream usage. For example, large cloud service providers Heroku and online payment Stripe all use the scheme of Redis+Lua script
The module system based on Redis4 in scheme 2 is not applicable yet, but it can be used happily when Redis4 becomes a stable version
 Key elimination algorithm supported by redis
Redis common elimination algorithms:
FIFO: First In First Out，The first in first out algorithm judges the storage time, and the data farthest from the current will be eliminated first LRU: Least Recently Used，The least recently used algorithm is used to judge the most recently used time, and the farthest data is eliminated first; LRU: Least Frequently Used，The least frequently used algorithm. In a period of time, the data used the least times will be eliminated first.
 Redis obsolescence strategy:
noeviction: When the memory limit is reached, no one will delete it and an error is returned. allkeys-lru: Try to recycle the least used keys to make room for the newly added data; volatile-lru: Try to recycle the least used keys so that the newly added data can be stored, but only the keys in the expired collection; allkey-random: Reclaim random keys to make space for newly added data; volatile-random: Reclaim the random keys so that the newly added data has space to store, but only the keys of the expired collection; volatile-ttl: Recycle the keys in the expired collection, and give priority to the keys with short expiration time, so that the newly added data can be stored.