Redis sorted set (ordered set) related commands

Redis ordered set is a collection of string type element, the element must be unique.

It is associated with a numerical score for each element of the ordered set. It is to redis from small to large order of collection of member passing score.

A member of an ordered set of unique (non-repeating), but the score (score) can be repeated.

 

 

1, zadd key score1 value1 score2 value2 ..... // add one or more elements to the ordered set. score is a numeric type, supports integer, floating point, support a negative number. z represent sorted set.

Example:  Zadd Students. . 1 zhangsan 2 Lisi . 3 wangwu     // returns the number of elements added. score just additional information, element true or value1, value2, ......

Description: hash, list, set, sorted set, in previous versions of Reids2.4, the primary support only add an element, does not support the once add multiple elements.

 

 

2, zcard key // returns the number of elements

 

 

3, zcount key minScore maxScore // returns the number of elements in the fractional values ​​[minScore, maxScore] of

 

 

4, zlexcount key minValue maxValue // returns the number of elements in the element values ​​[minValue, maxValue] a.

Element value can not be used directly, there are three kinds of writing:

  •  [Value represents contains this element
  • (Value expressed does not contain this element
  • - represents the first element (inclusive), + represents the last element (inclusive)

Example:

 zadd set1 1 a 2 b 3 c 4 d 

 setl zlexcount - +     // Returns the total number of elements 4

 setl zlexcount [A [B     // 2. I.e. the number of elements on [a, b]

 zlexcount set1 (a [b    //1。(a,b]

 You can not default [or (

 

 

 

 

 

5, zrange key minIndex maxIndex [withscores] // Returns the index of all elements in [minIndex, maxIndex] a.

Index starting from 0, the negative support, -1 represents the inverse of the first element, -2 is the penultimate element.

withscores is an optional parameter, fractional band is also displayed, without displaying only the element value.

 

6、zrangebyscore  key  minScore  maxScore  [withscores]

Returns all elements on the score values ​​[minScore, maxScore] a.

Default [, closed interval. You may be specified as the display [or (.

withscores Alternatively, the score will be displayed when the belt elements, is not displayed when the score is not tape.

Example:  zrangebyscore setl [ . 1 ( 2      // Get score in [1,2) on all the elements

 

7, zrank key elementValue // returns the index of the element values. The default ascending order by score. Index starts at 0.

 

 

 

Three or more orders are sorted by score in ascending order. The following three commands:

  • zrevrange
  • zrevrangebyscore
  • zrevrank

Prefix and not z, but zrev. 3 and the correspondence command before usage is identical, but is calculated by score in descending order.

 

 

 

 

8, zscore key elementValue // return the score value corresponding to the value of the element

 

9, zincrby key increment elementValue // increment is the increment, the score of the specified element increase increment. increment support negative, that is, minus.

 

 

 

 

10, zrem key value1 value2 ..... // remove one or more elements

 

11, zremrangebyrank key startIndex endIndex // remove all of the elements in the index [startIndex, endIndex] a. zremrangebyrank i.e. z remove range by rank

 

12, zremrangebyscore key minScore maxScore // remove all elements in the score [minScore, maxScore] of

 

13, zremrangebylex key value1 value2 // remove all of the elements between the two element values.

Can not directly write element value, it can be - + expressed, or added to the front element value [(

 

 

 

 

14、zscan  key  cursor  [match  pattern]    [count  num]    //迭代

 

 

sorted set also provides the computing intersection, and command set, here is no longer introduced one by one.

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11079970.html