This is the 26th day of my participation in the More Text Challenge. For more details, see more text Challenge

Redis A command that sets the expiration time of the key

/** * Set the value of the key to value and the expiration time of the key to seconds. * Applies only to strings */
SETEX key seconds value 

/** * This command is similar to the SETEX command, but it sets the lifetime of the key in milliseconds, * instead of seconds as SETEX does. * Applies only to strings */
PSETEX key milliseconds value

// Set the lifetime of the key to seconds
EXPIRE key seconds    
    
// Set the lifetime of the key to milliseconds
PEXPIRE key milliseconds 
    
// Set the expiration time of the key to the timestamp in seconds specified by timestemp
EXPIREAT key timestemp 
    
// Set the expiration time of the key to milliseconds-timestemp
PEXPIREPAT key milliseconds-timestemp 
Copy the code

The SETEX and PSETEX commands apply only to string objects. The other four commands apply to all key types. Although there are many different commands to set key expiration, eventually other commands are converted to the PEXPIREPAT command.

Expiration key Deletes the policy

By setting the expiration time of a key, you can determine whether a key expires at a certain point in time. For expired keys, you also need to delete them. There are three deletion policies:

  • Timed delete: When you set the expiration time of a key, you create a timer to delete the key immediately when it expires.
    • Advantage: It is memory friendly and should be deleted
    • Disadvantages: The timer is not CPU friendly. If there are many expiration keys, the timer occupies too many CPU resources
  • Lazy delete: Leave expired keys alone. Every time a key is retrieved from key space, it checks whether the retrieved key is expired, deletes it and returns itnilReturns the key if the key has not expired.
    • Advantages: CPU-friendly, expiration is judged only when the key is used
    • Disadvantages: memory unfriendly, if an expired key is not used again, it will remain in memory
  • Periodic deletion: Every once in a while, the program checks the database to remove the expired key.
    • Integrate and compromise between scheduled deletes and scheduled deletes while reducing CPU and memory consumption
    • Difficulty: How to determine the duration and frequency of the deletion operation? If the deletion operation is performed too frequently or for a long time, it will degenerate into periodic deletion; if the deletion operation is performed too little, it will degenerate into lazy deletion

Redis Expiration key delete policy

The strategy of lazy deletion and periodic deletion is adopted in Redis to achieve a balance between reasonable use of CPU time and avoiding wasting memory space.

In Redis, the expireNeeded function is called each time a key is read or written to determine whether the key has expired.

The lazy deletion policy modes are as follows:

Periodically Deleting a Policy

  • Every once in a while, Redis will take a certain number of keys from a specified database in a specified period of time to check whether they are expired
  • usecurrent_dbThe database that records the current check (a Redis server can have more than one database, passselectCommand to specify the database currently in use.)
  • When all the databases have been checked,current_dbIs set to 0, and the next check starts from database 0