To add an expiry (time-to-live) to a key in Redis based on a Linux timestamp, you can use the EXPIREAT
command. The EXPIREAT
command sets the expiry time for a key as an absolute Unix timestamp (in seconds since January 1, 1970). Here are the steps to add an expiry to a key in Redis based on a Linux timestamp:
Connect to your Redis server using the
redis-cli
command-line interface.Use the
SET
command to set the value of the key you want to expire. For example:SET mykey "Hello Redis"
Use the
EXPIREAT
command to set the expiry time for the key as a Unix timestamp. For example, to set the keymykey
to expire at midnight on January 1, 2024 (which corresponds to the Unix timestamp1704067200
), you can run:EXPIREAT mykey 1704067200
This command sets the key
mykey
to expire at the Unix timestamp1704067200
.
Note that the EXPIREAT
command sets the expiry time for a key as an absolute Unix timestamp, so you need to convert the desired expiry time to a Unix timestamp before using the command. You can use the date
command in Linux to convert a date and time to a Unix timestamp. For example, to convert the date and time "January 1, 2024 00:00:00" to a Unix timestamp, you can run:
date -d "January 1, 2024 00:00:00" +%s
This command outputs the Unix timestamp 1704067200
, which you can use as the argument to the EXPIREAT
command.
Comments
Post a Comment