Partial string reads
Read a fixed-width prefix or suffix from a stored string without retrieving the entire value.
Redis · Module
Returns a substring of the Redis string stored at a key, using inclusive zero-based start and end offsets. Negative offsets count backward from the string's end.
GETRANGE reads part of the string stored at a key. The start and end offsets are zero-based and inclusive; negative offsets count backward from the string's end, and bounds outside the value are clipped to its actual length.
GETRANGE key start end| Name | Type | Description |
|---|---|---|
key | key | The name of the key. |
start | integer | The zero-based starting offset; negative values count from the string's end. |
end | integer | The zero-based inclusive ending offset; negative values count from the string's end. |
Bulk string reply containing the substring selected by the inclusive start and end offsets.
key argument identifies the string to read.start begins at character position zero for the first character.end identifies the final included position and uses zero-based indexing.import { createClient } from 'redis';
const client = createClient();
await client.connect();
await client.set('mykey', 'This is a string');
console.log(await client.getRange('mykey', 0, 3));
console.log(await client.getRange('mykey', -3, -1));
console.log(await client.getRange('mykey', 0, -1));
await client.quit();Stores a string, then retrieves an inclusive prefix, suffix, and the complete value using zero-based and negative offsets.
Read a fixed-width prefix or suffix from a stored string without retrieving the entire value.
Use negative offsets to inspect the tail of a string, such as its final characters.
Retrieve bounded sections of serialized or packed string data using known offsets.
GETRANGE returns a substring from the string value at a key. Its syntax is GETRANGE key start end, and both offsets are zero-based and inclusive.
Follow-up: How does it behave with negative indexes? Negative offsets count backward from the end: -1 is the last character and -2 is the penultimate.
A negative start or end counts from the string's end. For example, -3 -1 selects the final three characters.
Follow-up: What happens if the requested range exceeds the value? Redis limits the range to the actual string length.
The command returns a bulk string reply containing the selected substring. If the key does not contain a string value, the command is not applicable to that data type.
Follow-up: What is the performance characteristic? The work is O(N), where N is the length of the returned substring.
GETSETRANGEGETBITSETBIT