Set an Expiry on an Existing Key in Redis, But Only When the Key Already Has an Expiry

When setting a timeout for a key in Redis, we have the option of setting it only if the key already has an expiry. So, if the key doesn’t already have an associated timeout, the new timeout won’t apply. The new timeout will only apply if the key already has an existing timeout. In such cases the expiry will be reset to the new expiry.

We can do this with the XX option of the EXPIRE command. We can also use the XX option on commands such as EXPIREAT, PEXPIRE, and PEXPIREAT.

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