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 do I install PHP Redis on Ubuntu 20.04?
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How do I use yum to install php-redis?
- How can I use PHP to increment values in Redis using ZINCRBY?
- How can I use PHP and Redis to retrieve data from a sorted set using ZRANGEBYSCORE?
- How do I use PHP and Redis together to create a transaction?
- How can I install and configure Redis on an Ubuntu server running PHP?
- How can I configure a PHP application to use Redis with a specific timeout?
- How can I set a timeout for a Redis connection using PHP?
- How can I use Redis to rate limit requests in PHP?
See more codes...