Redis SETRANGE – How to update a part of the string value in redis

1 month ago Lalit Bhagtani 0

In this tutorial, we will learn about how to update a part of the string value stored at a specified key in redis datastore. For this we will use a COMMAND – SETRANGE in redis-cli. This command takes start ( inclusive ) offset, which determined the start index of the update part of the string value. If start offset is greater than the length of the string value, then string value is padded with zero-bytes to make start offset fit. The index is zero based, so 0 means the first element, 1 means the second element and so on.

If key doesn’t exist in redis datastore, it is first created and set to empty string before performing the operation. The syntax of redis SETRANGE command is as follows :-

Syntax :-

redis host:post> SETRANGE <keyname> <start> <value>

Output :- 

(integer) value, representing the number of characters in the string.
Error, if key exist and value stored at the key is not a string.

Example :-


References :-

  1. SETRANGE Command Docs

That’s all for how to update a part of the string value stored at a key in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.