Latest web development tutorials

Redis ordered set (sorted set)

Redis ordered set and also set the same collection type string elements, and does not allow duplicate members.

The difference is that are associated with each type of element of a double score. It is through redis scores for small to large order of collection members.

Members of the ordered set is unique, but the score (score) it can be repeated.

Set by the hash table to achieve, so add, delete, find the complexity is O (1). Set the maximum number of members 232--1 (4294967295 Each set can store more than 40 million members).

Examples

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

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

In the example above, weZADD by adding commands to the ordered set redis of the three values and the associated scores.


Redis command ordered set

The following table lists the basic commands redis ordered sets:

No. Command and description
1 ZADD key score1 member1 [score2 member2]
Adding to the ordered set of one or more members, or update an existing member Score
2 ZCARD key
Gets the number of members of an ordered collection of
3 ZCOUNT key min max
Calculates the number of members in the interval score ordered set
4 ZINCRBY key increment member
Ordered set of specified members plus score increment increment
5 ZINTERSTORE destination numkeys key [key ...]
Calculation given one or more of the ordered set of the intersection and the result set is stored in a new key in the ordered set
6 ZLEXCOUNT key min max
Calculates the number of members in the interval dictionary ordered set
7 ZRANGE key start stop [WITHSCORES]
By index interval returned an ordered set of members within a specified range synthesis
8 ZRANGEBYLEX key min max [LIMIT offset count ]
Back ordered set by a member of the dictionary section
9 ZRANGEBYSCORE key min max [WITHSCORES] [ LIMIT]
Score returned by a member of an ordered set within the specified range
10 ZRANK key member
Returns an ordered collection at the specified index of members
11 ZREM key member [member ...]
Remove ordered set of one or more members
12 ZREMRANGEBYLEX key min max
Remove all members of a given ordered set interval dictionary
13 ZREMRANGEBYRANK key start stop
Remove all the members of an ordered set of rankings given interval
14 ZREMRANGEBYSCORE key min max
Remove all the members of an ordered set of given score range
15 ZREVRANGE key start stop [WITHSCORES]
Returns an ordered set of members within the specified range, through an index score from high in the end
16 ZREVRANGEBYSCORE key max min [WITHSCORES]
Returns an ordered set of members in the specified interval scores, scores from highest to lowest
17 ZREVRANK key member
Returns an ordered collection of members specified rank ordered sets by scores of members of the decrement (descending) Sort
18 ZSCORE key member
Returns an ordered set, members of the fractional value
19 ZUNIONSTORE destination numkeys key [key ...]
Calculation given one or more ordered sets and set and stored in the new key in
20 ZSCAN key cursor [MATCH pattern] [ COUNT count]
Iterative ordered set of elements (including elements and element members scores)