predisHow can I use PHP and Redis to get a reverse range of scores?
Using PHP and Redis, you can get a reverse range of scores by using the ZREVRANGE
command. This command takes a start and end score as arguments and returns the elements in the sorted set stored at the key with a score between the start and end.
For example, if you have a Redis sorted set stored at the key scores
that contains the following elements:
scores
1) "bob"
2) "alice"
3) "jane"
You can use the following code to get a reverse range of scores between 0 and 10:
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->zRevRange('scores', 0, 10);
var_dump($result);
The output of the above code would be:
array(3) {
[0]=>
string(4) "jane"
[1]=>
string(5) "alice"
[2]=>
string(3) "bob"
}
The code consists of the following parts:
- Connecting to the Redis server:
$redis = new Redis(); $redis->connect('127.0.0.1', 6379);
- Executing the
ZREVRANGE
command:$result = $redis->zRevRange('scores', 0, 10);
- Printing the result:
var_dump($result);
Helpful links
More of Predis
- How can I use Predis with a cluster in PHP?
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How can I use PHP to increment values in Redis using ZINCRBY?
- How do I use the PHP Redis zrevrange command?
- How do I install and configure a PHP Redis DLL on a Windows machine?
- How can I use PHP and Redis to retrieve data from a sorted set using ZRANGEBYSCORE?
- How can I check the version of PHP and Redis I am using?
- How do I install PHP Redis on Ubuntu 20.04?
- How can I use the zscan command in PHP with Redis?
See more codes...