Redis command o...login
Redis command operation Chinese manual
author:php.cn  update time:2022-04-12 14:07:28

Redis ordered set (sorted set)


Redis ordered set, like a set, is also a collection of string type elements and does not allow duplicate members.

The difference is that each element is associated with a double type score. Redis uses scores to sort the members of the collection from small to large.

The members of an ordered set are unique, but the scores can be repeated.

Sets are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1). The maximum number of members in a collection is 232 - 1 (4294967295, each collection can store more than 4 billion members).

Example

redis 127.0.0.1:6379> ZADD w3ckey 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD w3ckey 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD w3ckey 3 mysql
(integer) 1
redis 127.0.0.1:6379> ZADD w3ckey 3 mysql
(integer) 0
redis 127.0.0.1:6379> ZADD w3ckey 4 mysql
(integer) 0
redis 127.0.0.1:6379> ZRANGE w3ckey 0 10 WITHSCORES

1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"

In the above example, we added three values ​​​​to the ordered collection of redis and associated the score through the command ZADD.


Redis ordered set commands

The following table lists the basic commands of redis ordered set:

##5ZINTERSTORE destination numkeys key [key ...] 6 ZLEXCOUNT key min max 7ZRANGE key start stop [WITHSCORES ] 8ZRANGEBYLEX key min max [LIMIT offset count] 9ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 10ZRANK key member 11ZREM key member [member ...] 12ZREMRANGEBYLEX key min max 13ZREMRANGEBYRANK key start stop 14ZREMRANGEBYSCORE key min max 15ZREVRANGE key start stop [WITHSCORES] 16ZREVRANGEBYSCORE key max min [WITHSCORES] 17ZREVRANK key member 18ZSCORE key member ##19Calculate the union of one or more given ordered sets and store it in the new key20Iterate over the elements in the ordered set (including element members and element scores)
Serial numberCommand and description
1ZADD key score1 member1 [score2 member2]
Add one or more members to the ordered set, or update the score of an existing member
2ZCARD key
Get the members of the ordered set Number
3ZCOUNT key min max
Calculate the number of members with a specified interval score in the ordered set
4ZINCRBY key increment member
The score of the specified member in the ordered set plus the increment
Calculate the intersection of one or more given ordered sets and store the result set in the new ordered set key
Calculate the number of members in the specified dictionary range in an ordered set
Return an ordered set through the index range to form members in the specified range
Through dictionary The interval returns the members of the ordered set
Returns the members in the specified interval of the ordered set through scores
Returns the index of the specified member in the ordered set
Remove one or more members from the ordered set
Remove all members of the given dictionary range from the ordered set
Remove the given dictionary range from the ordered set All members of a given ranking interval
Remove all members of a given score interval from the ordered set
Returns the members in the specified interval in the ordered set, through the index, the score is from high to low
Returns the members within the specified score range in the ordered set, sorted from high to low
Returns the ranking of the specified member in the ordered set. The members of the ordered set are sorted by decreasing score (from large to small)
Returns the score value of the member in the ordered set
ZUNIONSTORE destination numkeys key [key ... ]
ZSCAN key cursor [MATCH pattern] [COUNT count]
#