The Redis XRANGE
command is used to query a range of messages from a stream by specifying the minimum and maximum IDs. In PHP, you might use this when you want to fetch historical data from a stream for processing or analysis. It's also useful when you’re building an application that needs to process stream data in batches.
<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); // Assume we have a stream named 'mystream' $minId = '-'; $maxId = '+'; $count = 100; // limit the number of messages returned $result = $redis->xRange('mystream', $minId, $maxId, $count); print_r($result); ?>
This script will connect to your local Redis server, then retrieve and print up to 100 messages from mystream
. Note that "-"
and "+"
are special ID values representing the smallest and largest IDs respectively.
<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $minId = '1637622832060-0'; // an example starting id $maxId = '1637625832060-0'; // an example ending id $result = $redis->xRange('mystream', $minId, $maxId); print_r($result); ?>
In this example, only messages with IDs between 1637622832060-0
and 1637625832060-0
will be retrieved.
XRANGE
request to prevent blocking your PHP application. Use pagination by specifying the maximum number of items to retrieve using the $count
parameter.Q: How to handle large streams with XRANGE in PHP?
A: For large streams, you can use the $count
parameter to limit the number of entries returned by XRANGE. You can then increment the $minId
based on the last ID returned and repeat the process until all desired data has been fetched.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.