This is the 10th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Redis expired keys

Set the key lifetime or expiration time

Through the EXPIRE command or PEXPIRE command, the client can read the TTL (Time To Live) of a certain key in seconds or milliseconds. After the TTL expires, the server automatically deletes the key whose lifetime is 0

Setting expiration Time

EXPIRE: The number of seconds the lifetime is set to

PEXPIRE: How many milliseconds is the lifetime set to

EXPIREAT: The expiration time of the key is set to the timestamp of the specified number of seconds

PEXPIREAT: The expiration time of the key is set to the millisecond timestamp specified by timestamp

EXPIRE PEXPIRE EXPIREAT PEXPIREAT to set the expiration time of the key, EXPIRE can be converted to PEXPIRE, PEXPIRE and EXPIREAT can be converted to PEXPIREAT, EXPIRE PEXPIRE EXPIREAT is implemented using the PEXPIREAT command

Save expiration time

The Expires dictionary of the redisDb structure holds the expiration time of all the keys in the database. This dictionary is an expired dictionary.

The key of the expired dictionary is a pointer to the key object of the key space, and the value is an integer of type long long, which is the expiration time

Remove expiration time

The PERSIST command is the reverse of the PEXPIREAT command: the PERSIST command looks up the given key in the expired dictionary and disassociates the key and value in the expired dictionary

Calculates and returns the remaining survival time

The TTL command returns the remaining lifetime of the key in seconds, and the PTTL command returns the remaining lifetime of the key in milliseconds, both by calculating the difference between the expiration time of the key and the current time

Expired keys

Check if the given key exists in the expiration dictionary, if there is an expiration time for the retrieve key, if the current UNIX timestamp is greater than the expiration time for the key, then it is expired, otherwise it is not expired

You can also run the TTL or PTTL command. If the value returned by the command is greater than or equal to 0, the key is not expired

The first method is actually used because accessing the dictionary directly is faster than executing the TTL command.

conclusion

The expiration key is stored in the expiration dictionary. To remove the expiration key is to remove the association between the key and the value in the expiration dictionary. We can use the TTL or PTTL command to check when the specified key expires. Redis uses checks to see if the given key exists in the expiration dictionary, if there is an expiration time for the retrieve key, if the current UNIX timestamp is greater than the expiration time for the key, then it is expired, and otherwise it is not.

After understanding and mastering the expiration key of Redis, we can operate the expiration key. In the following articles, we will discuss the deletion strategy of the expiration key of Redis, which is often used in our work or interview.