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

2 months 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 Redis SETRANGE command.

SETRANGE Command

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 the 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 :-

Redis SETRANGE

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.

<- String Commands