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

Redis SUBSTR Equivalent

Many SQL databases have a SUBSTR() or equivalent function that returns a substring from a specified string.

Redis has a SUBSTR command, but it was deprecated in Redis 2.0.0.

However, Redis also has the GETRANGE command, that basically does the same thing. Basically, anything that was possible with the Redis SUBSTR command, is now possible with the GETRANGE command.

And the GETRANGE command does basically the same thing that most of the SUBSTR() and SUBSTRING() functions do in the SQL world – gets a substring from a string.

Continue reading