How to set expiration time in Redis?

You can set an expiration time in Redis using either the EXPIRE command or the EXPIREAT command.

  1. Come to an end.
EXPIRE key seconds

In this case, the key is the name of the key to set the expiration time for, and seconds is the expiration time in seconds. For example, setting the key named mykey to expire in 10 seconds.

EXPIRE mykey 10
  1. EXPIREAT means the expiry date or time.
EXPIREAT key timestamp

One example is setting an expiration time for a key named “mykey” on January 1, 2023.

EXPIREAT mykey 1672531200

It should be noted that after setting an expiration time for a key, Redis will automatically delete that key when the expiration time is reached.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds