GETRANGE and SETRANGE let you read or change parts of a text stored in Redis without touching the whole text.
GETRANGE and SETRANGE in Redis
GETRANGE key start end SETRANGE key offset value
GETRANGE returns the substring from start to end (inclusive).
SETRANGE overwrites part of the string starting at offset with value. If the string is shorter, it is extended with zero-bytes.
mykey.GETRANGE mykey 0 4
mykey.SETRANGE mykey 6 "world"
mykey. Negative indexes count from the end.GETRANGE mykey -5 -1
This example first sets the string "Hello Redis!" to mykey. Then it gets the first 5 characters, which is "Hello". Next, it replaces the substring starting at position 6 with "World", changing the string to "Hello World!". Finally, it gets the entire updated string.
SET mykey "Hello Redis!" GETRANGE mykey 0 4 SETRANGE mykey 6 "World" GETRANGE mykey 0 -1
Indexes in GETRANGE are inclusive and zero-based.
SETRANGE returns the length of the string after the change.
If SETRANGE extends the string, the new bytes between old end and offset are zero-bytes (\x00).
GETRANGE reads a part of a string by specifying start and end positions.
SETRANGE replaces part of a string starting at a given offset with new text.
Both commands help work with substrings without rewriting the whole string.