Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Redis Ordered Collection (sorted set)


May 16, 2021 Redis


Table of contents


Redis Ordered Collection (sorted set)

Redis ordered collections, like collections, are collections of string type elements and do not allow duplicate members.

The difference is that each element is associated with a double type score. I t is through fractions that redis sorts members of a collection from small to large.

The members of the ordered collection are unique, but the score can be repeated.

Collections are implemented through hash tables, so adding, deleting, and finding are all O(1). T he largest number of members in a collection is 2 32 - 1 (4294967295, each of which can store more than 4 billion members).

Instance

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 by commanding ZADD and associated the score.


Redis ordered collection commands

The following table lists the basic commands for the redis ordered collection:

Serial number Commands and descriptions
1 ZADD key score1 member1 [score2 member2]
Add one or more members to an ordered collection, or update the scores of existing members
2 ZCARD key
Gets the number of members of an ordered collection
3 ZCOUNT key min max
Calculates the number of members who specify interval fractions in an ordered collection
4 ZINCRBY key increment member
Add incremental increments to the scores of the specified members in the ordered collection
5 ZINTERSTORE destination numkeys key [key ...]
Calculates the intersection of one or more ordered sets given and stores the result set in the new ordered collection key
6 ZLEXCOUNT key min max
The number of members in the specified dictionary interval is calculated in an ordered collection
7 ZRANGE key start stop [WITHSCORES]
Returns an ordered collection of members within a specified interval through an index interval
8 ZRANGEBYLEX key min max [LIMIT offset count]
Returns members of an ordered collection through dictionary intervals
9 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]
Returns the members within the ordered collection specified by the score
10 ZRANK key member
Returns the index of the specified members in the ordered collection
11 ZREM key member [member ...]
Remove one or more members from an ordered collection
12 ZREMRANGEBYLEX key min max
Removes all members of a given dictionary interval in an ordered collection
13 ZREMRANGEBYRANK key start stop
Removes all members of a given rank interval in an ordered collection
14 ZREMRANGEBYSCORE key min max
Removes all members of a given score interval in an ordered collection
15 ZREVRANGE key start stop [WITHSCORES]
Returns the members within the specified interval in an ordered set, and through the index, the score is from high to the end
16 ZREVRANGEBYSCORE key max min [WITHSCORES]
Returns an ordered set of specified members within the score range, with scores sorted from highest to thing
17 ZREVRANK key member
Returns the ranking of the specified members in the ordered collection, sorted by the decreasing number of points (large to small).
18 ZSCORE key member
Returns the ordered set, the score value of the member
19 ZUNIONSTORE destination numkeys key [key ...]
Calculates the collection of one or more ordered sets given and stores them in the new key
20 ZSCAN key cursor [MATCH pattern] [COUNT count]
Iterative elements in an ordered collection ,including element members and element values)