A, redis three expiration policy
1. regularly delete
the key expiration time set at the same time, create a timer for the key, so that the timer comes, the key to delete the expiration of key
advantages: to ensure that memory is freed as soon as possible
drawbacks: 1) if the key expired many, delete the key will take up a lot of CPU time, CPU time in a stressful situation, can not put all the CPU time used to make critical thing, also need to take the time to delete the key.
2) Create a timer time-consuming, if create a timer to set the expiration time of each key (there will be plenty of timer generates), severely affect performance
3) no one with
2 inert delete
key when not expired delete, every acquisition key from the database to check whether the time has expired, if expired, deleted, returns null.
Advantages: deletion occurred only in the key removed from the database occurs when, and only delete the current key, so the occupation of the CPU time is relatively small, and at this time do not delete that reached a point where.
Cons: If a large number of the key after exceeding the timeout, within a long period of time, have not been acquired, the memory leak may occur (unwanted junk take up a lot of memory)
3. deleted regularly
from time to time perform a delete operation expired key
advantages: 1) delete operation by limiting the duration and frequency, to reduce the occupation of the CPU deletes time - process "Timing deleted" disadvantages
disadvantages 2) periodically delete expired key-- process "inert deleted"
disadvantages: 1) memory friendliness, as "regular delete"
2) in terms of CPU time and friendly, not as good as "inert delete"
Two, redis adopted expiration policy
Remove + inert periodically delete
inactive deletion process: during operations such as get or setnx, first check whether the key expired, if expired, delete key, and then perform the appropriate action; if not expired, directly perform actions
on a regular basis to delete process: through each database, checks a specified number of the current library key (default key for each library checker 20, note that the loop is executed is equivalent to 20 times, when the loop described below), if the library is not currently a key set an expiration time, direct execution traverse under a library,
randomly get a set of key expiration time, check that the key has expired, if expired, delete key, judge periodically delete operation is specified length of time has been reached, have already been reached, exit periodically delete .