w3resource

Redis String: SETRANGE

Redis SETRANGE Command

Redis SETRANGE command is used to overwrite part of a string at key starting at the specified offset for the entire length of the value. If the offset is larger than the current length of the string at the key, the string is padded with zero-bytes to make offset fit. Non-existing keys are considered as empty strings.

The maximum offset that you can set is 229-1 (536870911), as Redis Strings are limited to 512 megabytes.

Here are some common uses and scenarios for the "SETRANGE" command:

  • String Manipulation: Update parts of a string stored at a key without replacing the entire value.
  • Efficient Updates: Modify specific segments of a string efficiently.
  • Padding: Append or prepend data to an existing string at a specific offset.
  • Custom Protocols: Implement and manage custom binary protocols by updating specific ranges.

Syntax:

SETRANGE KEY_NAME OFFSET VALUE

Available since

2.2.0.

Return Value

Integer replies, the length of the string after it was modified by the command.

Return Value Type

Integer

Example: Redis SETRANGE

redis 127.0.0.1:6379> SET key1 "Hello World"
OK
redis 127.0.0.1:6379> SETRANGE key1 6 "Redis"
(integer) 11
redis 127.0.0.1:6379> GET key1
"Hello Redis"

Previous: SETNX
Next: STRLEN



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-setrange-key-offset-value.php