Redis provides us with quite a few options for setting keys and any timeouts associated with those keys. Sometimes we can do it with a single command, other times we need two commands.
Here are four ways to set a key and its expiry in Redis.
The SET Command
Let’s start with the SET command. This command allows us to set a string and any associated timeout all in one go. If you’re working with a string, this command is likely all you’ll need for setting the string and its expiry.
We can specify the expiry in seconds, milliseconds, as a Unix timestamp in seconds, or as a Unix timestamp in milliseconds.
Example:
SET coupon "24 Hour Sale" EX 86400
Result:
OK
Now that we’ve set the string and its associated expiry, let’s use the TTL command to check its current time to live:
TTL coupon
Result:
(integer) 86390
In this case, we used the EX argument, which allows us to specify the timeout in seconds. We can alternatively specify it in milliseconds. To do this, we’d use PX instead of EX:
SET coupon "24 Hour Sale" PX 86400000
Result:
OK
And in keeping with the milliseconds theme, we can use the PTTL command to get the current time to live, in milliseconds:
PTTL coupon
Result:
(integer) 86351300
We can alternatively use the EXAT or PXAT option to set the expiry as a Unix timestamp. EXAT specifies the Unix timestamp in seconds, whereas PXAT specifies it in milliseconds.
Here’s an example of setting the expiry as a Unix timestamp in seconds:
SET coupon "24 Hour Sale" EXAT 1659407790
Result:
OK
We can use the EXPIRETIME command to return this value:
EXPIRETIME coupon
Result:
(integer) 1659407790
As mentioned, we can use the PXAT option to set the expiry as a Unix timestamp in milliseconds instead of seconds:
SET coupon "24 Hour Sale" PXAT 1659410231994
Result:
OK
We can use the PEXPIRETIME command to return this value:
PEXPIRETIME coupon
Result:
(integer) 1659410231994
The SETEX Command
The SETEX command is a more specific command that is specifically designed to set a key and its expiry at once:
SETEX coupon 86400 "24 Hour Sale"
Result:
OK
It’s possible that this command could be deprecated at some stage, given its functionality can be provided by the SET command.
The PSETEX Command
The PSETEX command is basically the same as the SETEX command, except that it sets the expiry in milliseconds:
PSETEX coupon 86400000 "24 Hour Sale"
Result:
OK
As with SETEX, it’s possible that PSET could be deprecated at some stage, given that its functionality can be provided by the SET command.
Set the Expiry Separately
We can also set the key and its expiry in two separate operations. In some cases, this could be your only option, for example if you’ve already set the key, or if you’re setting a different data type.
Here’s an example of creating a list and setting its expiry in two separate operations:
LPUSH scores 10 12 75
Result:
(integer) 3
That created the list (assuming the list didn’t already exist).
Now let’s use EXPIRE to set an expiry for the list:
EXPIRE scores 86400
Result:
(integer) 1
And we can now check its current time to live:
TTL scores
Result:
(integer) 86321
We can alternatively use the PEXPIRE command to specify the amount in milliseconds.
We can also use the EXPIREAT command to specify a Unix timestamp in seconds, or the PEXPIREAT command to specify it in milliseconds.
Here’s an example that uses the EXPIREAT command:
EXPIREAT scores 1659412496
Result:
(integer) 1