You have to work really hard to look effortless!

Wechat search public number [long Coding road], together From Zero To Hero!

preface

In the previous article, we learned the basic concepts of the Redis Sorted Set and some of its commands. There are many commands in this part, and it is estimated that two more articles will be needed to learn it. Each article has about 10 commands. You don’t need to remember all of these commands, just go through them yourself and get an impression. When you really need to use it, you can think of this operation and then go to the command.

ZINCRBY

Available version: >= 1.2.0

Time complexity: O(log(N)), where N is the number of elements in an ordered array

The command format

ZINCRBY key increment member
Copy the code

Command description

  • In an ordered arraymemberThe correspondingscoreValue, addincrement;
  • If this doesn’t exist in the ordered arraymemberElement, so add this element, don’t set itscoreA value ofincrement;
  • If the ordered array does not exist, create an ordered array and add this element.
  • scoreThe value can be an integer or a double, or it can be negative, equivalent to a subtraction;
  • ifkeyIf the corresponding data type is not an ordered array, return error;

The return value

String: new score value corresponding to member

The sample

127.0.0.1:6379> zadd myzset 1 one
(integer) 1

#Add 1
127.0.0.1:6379> zincrby myzset 1 one
"2"

#Minus 2127.0.0.1:6379> zincrby myzset-2 one "0"
#See the score
127.0.0.1:6379> zscore myzset one
"0"

Copy the code

ZRANK

Available versions: >= 2.0.0

Time complexity: O(log(N)), where N is the number of elements in an ordered array

The command format

ZRANK key member
Copy the code

Command description

  • Returns the ranking of elements in an ordered array in descending order of point value;
  • Rank (index) from0To begin with, the element with the lowest score is0;

The return value

Integer: Return rank if member is in an ordered collection

Nil: ordered array does not exist or member does not exist, return nil

The sample

127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1

#Check the ranking
127.0.0.1:6379> zrank myzset one
(integer) 0
127.0.0.1:6379> zrank myzset two
(integer) 1

Copy the code

ZREVRANK

Available versions: >= 2.0.0

Time complexity: O(log(N)), where N is the number of elements in an ordered array

The command format

ZREVRANK key member
Copy the code

Command description

  • withZRANKOn the contrary,ZREVRANKReturns the ranking of elements in an ordered array in descending order of point value;
  • Rank (index) from0To start, the element with the highest score is ranked as0;

The return value

Integer: Return rank if member is in an ordered collection

Nil: ordered array does not exist or member does not exist, return nil

The sample

127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1

#Check the ranking
127.0.0.1:6379> zrevrank myzset one
(integer) 1
127.0.0.1:6379> zrevrank myzset two
(integer) 0


Copy the code

ZREM

Available versions: >= 2.0.0

Time complexity: O(M*log(N)), N is the number of elements in the ordered array, M is the number of elements provided by the command

The command changes

Since version 2.4, you can remove multiple elements

The command format

ZREM key member [member ...]
Copy the code

Command description

  • Removes the specified element from an ordered array
  • If the element does not exist, it is ignored
  • If the key exists, but the corresponding type is not an ordered collection, error is returned

The return value

Integer: Number of elements to be removed (excluding non-existent elements)

The sample

127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1
127.0.0.1:6379> zadd myzset 3 three
(integer) 1

#Remove two existing and one non-existent elements
127.0.0.1:6379> zrem myzset one two notexists
(integer) 2

#Traverse the collection127.0.0.1:6379> zrange myzset 0-1 1) "three"Copy the code

ZREMRANGEBYRANK

Available versions: >= 2.0.0

Time complexity: O(M+log(N)), N is the number of elements in the ordered array, M is the number of elements to delete

The command format

ZREMRANGEBYRANK key start stop
Copy the code

Command description

  • Remove ordered array, sort atstartandstopBetween elements;
  • startandstopWhen the index is positive, it’s all from0To start, 0 is the first element with the lowest score, 1 is the second element, and so on;
  • ifstartandstopNegative,- 1Represents the last element with the largest score, -2 represents the penultimate element, and so on;

The return value

Integer: number of elements to be removed

The sample

127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1
127.0.0.1:6379> zadd myzset 3 three
(integer) 1

#Remove the second and third elements (indexes 1 and 2)127.0.0.1:6379> zremrangebyRank myzset 12 (INTEGER) 2 127.0.0.1:6379> zrange myzset 0-1 1) "oneCopy the code

ZREMRANGEBYSCORE

Available version: >= 1.2.0

Time complexity: O(M+log(N)), N is the number of elements in the ordered array, M is the number of elements to delete

The command format

ZREMRANGEBYSCORE key min max
Copy the code

Command description

  • Remove an ordered array whose score value is betweenminandmaxThe default is the closed interval [min, Max];
  • -inf,+infIt’s also a legal input, minus infinity and plus infinity
  • If you want to use an open interval, you can do it inminormaxI put a ‘before it.(
  • The interval value problem can refer to the last articleZRANGEBYSCORE

The return value

Integer: number of elements to be removed

The sample

127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1
127.0.0.1:6379> zadd myzset 3 three
(integer) 1
127.0.0.1:6379> zadd myzset 4 four
(integer) 1

#Delete the elements between the score values [1,2]127.0.0.1:6379> zremrAngeByScore myZSet 12 (INTEGER) 2
#Delete the elements between minus infinity and 4 (open interval)
127.0.0.1:6379> zremrangebyscore myzset -inf (4
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1
1) "four"
Copy the code

ZREMRANGEBYLEX

Available version: >= 2.8.9

Time complexity: O(M+log(N)), N is the number of elements in the ordered array, M is the number of elements to delete

The command format

ZREMRANGEBYLEX key min max
Copy the code

Command description

  • When all elements in an ordered collection have the same Score value, use string lexicographical order to sort and deletemintomaxBetween elements
  • Min, Max here, and in the previous articleZRANGEBYLEXThe same concept
  • If you use the sameminandmax.ZREMRANGEBYLEXDeleted elements andZREMRANGEBYLEXThe returned elements are the same

The return value

Integer: number of elements to be removed

The sample

127.0.0.1:6379> ZADD myzset 0 aaaa 0 b 0 c 0 d 0 e (INTEGER) 5 127.0.0.1:6379> ZADD myzset 0 foo 0 zap 0 zip 0 ALPHA 0 Alpha (INTEGER) 5 127.0.0.1:6379> ZRANGE myzset 0-1 1) "alpha" 2) "aaaa" 3) "alpha" 4) "b" 5) "c" 6) "d" 7) "e" 8) "foo" 9) "zap" 10) "zip"
#Remove elements127.0.0.1:6379> ZREMRANGEBYLEX myzset [alpha [omega (integer) 6 127.0.0.1:6379> ZRANGE myzset 0-1 1) "alpha" 2) "aaaa" 3) "zap"Copy the code

ZLEXCOUNT

Available version: >= 2.8.9

Time complexity: O(log(N)), where N is the number of elements in an ordered array

The command format

ZLEXCOUNT key min max
Copy the code

Command description

  • Returns a string lexicographically sorted when all elements in an ordered collection have the same SCORE valuemintomaxBetween elements
  • Min, Max here, and in the previous articleZRANGEBYLEXThe same concept

The return value

Integer: The number of elements in the lexicographical range

The sample

127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e
(integer) 5
127.0.0.1:6379> ZADD myzset 0 f 0 g
(integer) 2

#Full range
127.0.0.1:6379> ZLEXCOUNT myzset - +
(integer) 7

#[b, f] range
127.0.0.1:6379> ZLEXCOUNT myzset [b [f
(integer) 5
Copy the code

ZPOPMAX

Available versions: >= 5.0.0

Time complexity: O(log(N)*M), N is the number of elements in the ordered array, M is the number of elements popup

The command format

ZPOPMAX key [count]
Copy the code

Command description

  • Remove and return beforecountascoreHigher element
  • If the count value is not specified, the default value is 1
  • If count exceeds the collection length, no error is reported
  • In the return result, the element with the largest score is the first, with the score

The return value

List: A list of elements and their points

The sample

127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZADD myzset 3 "three"
(integer) 1

#By default, one is returned
127.0.0.1:6379> ZPOPMAX myzset
1) "three"
2) "3"

#When the list length exceeds, all elements are returned127.0.0.1:6379> ZPOPMAX myzset 1) "one" 2) "one" 4) "1"Copy the code

ZPOPMIN

Available versions: >= 5.0.0

Time complexity: O(log(N)*M), N is the number of elements in the ordered array, M is the number of elements popup

The command format

ZPOPMIN key [count]
Copy the code

Command description

  • Remove and return beforecountascorealowThe elements of the
  • If the count value is not specified, the default value is 1
  • If count exceeds the collection length, no error is reported
  • In the return result, the element with the lowest score is the first and carries the score

The return value

List: A list of elements and their points

The sample

127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZADD myzset 3 "three"
(integer) 1

#By default, one is returned
127.0.0.1:6379> zpopmin myzset
1) "one"
2) "1"

#When the list length exceeds, all elements are returned
127.0.0.1:6379> zpopmin myzset 3
1) "two"
2) "2"
3) "three"
4) "3"
Copy the code

ZMSCORE

Available version: >= 6.2.0

Time complexity: O(N), where N is the number of elements supplied by the command

The command format

ZMSCORE key member [member ...]
Copy the code

Command description

  • Returns the score for multiple elements
  • If the element does not exist in the ordered array, return nil

The return value

Array: a list of points for all supplied elements

The sample

127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZMSCORE myzset "one" "two" "nofield"
1) "1"
2) "2"
3) (nil)
Copy the code

conclusion

This article describes some of the commands associated with ordered collections, including

  • ZINCRBY: Element score plus a value
  • ZRANK, ZREVRANK: Gets the rank of an element in an ordered array
  • ZREM: Remove elements
  • ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREMRANGEBYLEX: deletes elements from a given interval
  • ZLEXCOUNT: Returns the number of elements in the specified lexicographical order
  • ZPOPMAX, ZPOPMIN: remove count before the first/last element
  • ZMSCORE: Returns the score of multiple elements

More and more

Personal blog: lifelmy.github. IO /

Wechat official Account: Long Coding road