The XREVRANGE
command in Redis is used to fetch data from a stream in a reverse order. Common use cases include:
Here's an example using the Predis library in PHP:
<?php require "vendor/autoload.php"; $predis = new Predis\Client(); // Add messages to 'mystream' $predis->xadd('mystream', '*', 'field1', 'Hello'); // Fetch last 10 messages $messages = $predis->xrevrange('mystream', '+', '-', ['count' => 10]); foreach ($messages as $messageId => $message) { echo "ID: $messageId\n"; foreach($message as $key => $value){ echo "Field: $key, Value: $value\n"; } } ?>
In this code, we first create a connection with Redis using Predis and then add a message to our stream called 'mystream'. We then use XREVRANGE
to fetch the last 10 messages from the stream.
XREAD
instead of XREVRANGE
if you’re primarily interested in new entries and want to avoid scanning through older ones.Is XREVRANGE
available in all versions of Redis?
No, XREVRANGE
is only available in Redis 5.0 and newer versions.
Can I use XREVRANGE
to fetch data in original order?
No, you should use XRANGE
for that purpose. XREVRANGE
retrieves data in reverse order.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.