How to Return the Expiry Time as a Unix Timestamp in Redis

In Redis, we have several options for returning the expiry time of a key. One option from Redis 7.0.0 and above is to return it as a Unix timestamp.

When returning the expiry as a Unix timestamp, we can have it returned in seconds or milliseconds. To return it in seconds, we can use the EXPIRETIME command. To return it in milliseconds, we can use the PEXPIRETIME command.

Continue reading

How to Update a Key and Return its Old Value in a Single Operation in Redis

When using the SET command to set a key in Redis, we can use the GET option to return its old value. This allows us to update the value of a key while returning the value that we’re replacing it with.

Note that this option was introduced in Redis 7.0.0, so if it doesn’t work, check your Redis version. If using an earlier version, try the GETSET command instead (which is deprecated from Redis version 6.2.0).

Continue reading