The use of Yii redis Zset ordered set

Time:2021-2-24

 

Similar to hash which stores the mapping relationship between key and value, ordered set also stores the mapping between member and score, and provides score processing commands, as well as commands to fetch and scan member and score in order according to score. Here are some common commands and their use in Yii.

ZADD

ZADD:ZADD key-name score member [score member …]
Add a member with a given score to an ordered set.

ZREM

ZREM:ZREM key-name member [member …]
Removes a given member from an ordered collection and returns the number of removed members.

ZCARD

ZCARD:ZCARD key-name
Returns the number of members contained in an ordered collection.

ZINCRBY

ZINCRBY:ZINCRBY key-name increment member
Add increment to the member’s score.

ZCOUNT

ZCOUNT:ZCOUNT key-name min max
Returns the number of members with a score between min and max.

ZRANK

ZRANK:ZRANK key-name member
Returns the ranking of members in an ordered set. Ranking starts at 0.

ZSCORE

ZSCORE:ZSCORE key-name member
Returns the score of the member.

ZRANGE

ZRANGE:ZRANGE key-name start stop [WITHSCORES]
Returns the members ranking between start and stop in the ordered set. If the optional with scores option is given, the command will also return the score of the members.

ZREVRANK

ZREVRANK:ZREVRANK key-name member
Returns the ranking of members in an ordered set, and the members are sorted from large to small according to the score.

ZREVRANGE

ZREVRANGE:ZREVRANGE key-name start stop [WITHSCORES]
Returns the members within the given ranking range of the ordered set, and the members are sorted from large to small according to the score.

ZRANGEBYSCORE

ZRANGEBYSCORE:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
Returns all members in an ordered set with scores between min and max.

ZREVRANGEBYSCORE

ZREVRANGEBYSCORE:ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
Get all the members in the ordered set whose score is between min and Max, and return them in the order of score from large to small.

ZREMRANGEBYRANK

ZREMRANGEBYRANK:ZREMRANGEBYRANK key-name start stop
Remove all members in the ordered collection that rank between start and stop.

ZREMRANGEBYSCORE

ZREMRANGEBYSCORE:ZREMRANGEBYSCORE key-name min max
Remove all members in the ordered set with scores between min and max.

 

Examples in Yii:

//Add 3 group members and their scores
Yii::$app->redis->zadd('testZsetKey', 1, 'a', 2, 'b', 3, 'c');  // 3
//Return the number of members
Yii::$app->redis->zcard('testZsetKey');   // 3
//Increase the score of member C by 3
Yii::$app->redis->zincrby('testZsetKey', 3, 'c');   // 6
//Returns the score of the C member
Yii::$app->redis->zscore('testZsetKey', 'c');   // 6
//Get the ranking of C in the ordered set, starting from 0
Yii::$app->redis->zrank('testZsetKey', 'c');   // 2
//Returns the number of members with a score between 1 and 6
Yii::$app->redis->zcount('testZsetKey', 1, 6);   // 3
//Remove member B
Yii::$app->redis->zrem('testZsetKey', 'b');   // 1
//Returns all members and scores in an ordered set
Yii::$app->redis->zrange('testZsetKey', 0, -1, 'WITHSCORES');   // ["a","1","c","6"]

 

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]