Redis has five data types

Keywords: Linux

catalogue

introduction

1, String data type

1,SET/GET/APPEND/STRLEN

2,INCR/DECR/INCRBY/DECRBY

3,GETSET

4,SETEX

5,SETNX

6,MSET/MGET/MSETNX

2, List data type

1,LPUSH/LPUSHX/ LRANGE

2,LPOP/LLEN

3,LREM/LSET/LINDEX/LTRIM

4,LINSERT

5,RPUSH/ RPUSHX/RPOP/RPOPLPUSH

3, Hash data type (hash type)

1,HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX

2,HINCRBY

3,HGETALL/HKEYS/HVALS/HMGET/HMSET

4, Set data type (unordered set)

1,SADD/SMEMBERS/SCARD/SISMEMBER

2,SPOP/SREM/SRANDMEMBER/SMOVE

5, Sorted Set data type (zset, ordered set)

1,ZADD/ZCARD/ZCOUNT/ZREM/ZINCRBY/ZSCORE/ZRANGE/ZRANK

2,ZRANGE BY SCORE/ZREM RANGE BY RANK/ZREM RANGE BY SCORE

3,ZREVRANGE/ZREVRANGEBYSCORE/ZREVRANK

summary

introduction

Redis supports five data types: string, hash, list, set and zset.

1, String data type

Overview: string is the most basic type of redis. It can store up to 512MB of data. String type is binary secure, that is, it can store any data, such as numbers, pictures, serialized objects, etc

1,SET/GET/APPEND/STRLEN

PS: APPEND
APPEND key value     #Append the key value and return the appended length (if the key does not exist, it is equivalent to creation)
exists home     #Judge whether the key exists, return 1 if it exists, otherwise return 0
append home "cat"     #The key does not exist, so the append command returns the length of the current Value
append home "dog"     #The key already exists, so the length of the appended Value is returned
get home     #Get the key through the get command to judge the result of append
[root@redis ~]# redis-cli
127.0.0.1:6379> exists mykey     #Judge whether the key exists, return 1 if it exists, otherwise return 0.
(integer) 0
127.0.0.1:6379> append mykey "hello"     #The key does not exist, so the append command returns the length of the current Value.
(integer) 5
127.0.0.1:6379> append mykey " world"     #The key already exists, so the total length of the appended Value is returned.
(integer) 11
127.0.0.1:6379> get mykey     #Obtain the key through the get command to judge the result of append.
"hello world"
127.0.0.1:6379> set mykey "this is a file"     #Set a new value for the key through the set command and overwrite the original value.
OK
127.0.0.1:6379> get mykey
"this is a file"
127.0.0.1:6379> strlen mykey     #Gets the character length of the specified Key.
(integer) 14

2,INCR/DECR/INCRBY/DECRBY

INCR key: key The value increases by 1( key Value must be an integer)
DECR key: key The value decreases by 1( key Value must be an integer)
127.0.0.1:6379> set mykey 20     #Set the value of Key to 20
OK
127.0.0.1:6379> incr mykey     #The value of the Key is incremented by 1
(integer) 21
127.0.0.1:6379> decr mykey     #The value of the Key is decremented by 1
(integer) 20
127.0.0.1:6379> del mykey     #Delete existing keys. Returning 1 indicates that the deletion was successful
(integer) 1
127.0.0.1:6379> decr mykey     #Decrement is performed on null values. The original value is set to 0 and the decremented value is - 1
(integer) -1
127.0.0.1:6379> del mykey
(integer) 1
127.0.0.1:6379> incr mykey     #The null value is incremented. The original value is set to 0 and the incremented value is 1
(integer) 1
127.0.0.1:6379> set mykey hello     #Set the Value of the key to an ordinary string that cannot be converted to an integer
OK
127.0.0.1:6379> incr mykey
(error) ERR value is not an integer or out of range
127.0.0.1:6379> set mykey 10
OK
127.0.0.1:6379> decrby mykey 5      #Decrements the specified integer
(integer) 5
127.0.0.1:6379> incrby mykey 10     #Increments the specified integer
(integer) 15

3,GETSET

GETSET key value: obtain key Value and return, and give key Set new value
127.0.0.1:6379> incr myconuter     #Increments the value of the counter atomically by 1
(integer) 1
127.0.0.1:6379> getset mycounter 0     #While obtaining the original value of the counter and setting it to a new value, the two operations are completed in an atomic way at the same time
127.0.0.1:6379> get mycounter      #View the result after setting
"0"

4,SETEX

setex key seconds value: Setting assignment key The expiration time of is seconds
127.0.0.1:6379> setex mykey 15 "hello"     #Set the expiration time of the specified Key to 15 seconds
OK
127.0.0.1:6379> ttl mykey     #Use the tt1 command to view the remaining lifetime (seconds) of the specified Key. 0 means it has expired and - 1 means it will never expire
(integer) 13
127.0.0.1:6379> get mykey     #We can still get the Value of the key during its lifetime
"hello"
127.0.0.1:6379> ttl mykey     #The return value of the ttl command shows that the Key has expired
(integer) -2
127.0.0.1:6379> get mykey     #Getting an expired Key will return nil
(nil)

Note: redis -1 often represents an upper limit, which means all- 2 indicates the expiration / expiration of the.

5,SETNX

SETNX key value:Execute if no key exists set Operation. If it exists, it will not be executed
127.0.0.1:6379> del mykey      #Delete this key to facilitate the following test verification
(integer) 0
127.0.0.1:6379> setnx mykey "hello"     #The key does not exist, so the setnx command was executed successfully
(integer) 1
127.0.0.1:6379> setnx mykey "world"     #This key already exists, so this setting has no effect
(integer) 0
127.0.0.1:6379> get mykey     #As can be seen from the results, the returned value is still the value set for the first time
"hello"

6,MSET/MGET/MSETNX

MSET key value [key value ...]: Batch setting key-Value pair
MGET key [key ...]: Batch get key value pairs
MSETNX key value [key value ...]: Batch setting key-If no value pair exists, execute and return 1; As long as one exists, it will not execute and return 0
127.0.0.1:6379> mset key1 "hello" key2 "world"     #key1 and key2 are set in batch
OK
127.0.0.1:6379> mget key1 key2     #The values of key1 and key2 are obtained in batch
1) "hello"
2) "world"
127.0.0.1:6379> msetnx key3 "li" key4 "si"     #The two keys key3 and key4 are set in batch. Because they did not exist before, the msetnx command executes successfully and returns 1
(integer) 1
127.0.0.1:6379> mget key3 key4
1) "li"
2) "si"
127.0.0.1:6379> msetnx key3 "zhang" key5 "san"     #key3 and key5 are set in batch, but key3 already exists, so the msetnx command fails and returns 0
(integer) 0
127.0.0.1:6379> mget key3 key5     #Get key3 and key5 in batch. Since key5 is not set successfully, nil is returned
1) "li"
2) (nil)

2, List data type

Overview: the element type of the list is string, sorted according to the insertion order, and elements are added at the head or tail of the list

1,LPUSH/LPUSHX/ LRANGE

LPUSH key value [value ...]Insert list elements in the header (left) in turn 
LPUSHX key value: The key must exist to execute. Insert the element value in the header and return the number of list elements
LRANGE key start stop: Fetch from location index start To location index stop All elements of (so start with 0)
127.0.0.1:6379> del mykey
(integer) 1
127.0.0.1:6379> lpush mykey a b c d
(integer) 4
127.0.0.1:6379> lrange mykey 0 2
1) "d"
2) "c"
3) "b"
127.0.0.1:6379> lrange mykey 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> lpushx mykey2 e
(integer) 0
127.0.0.1:6379> lrange mykey2 0 -1
(empty list or set)
127.0.0.1:6379> lpushx mykey e
(integer) 5
127.0.0.1:6379> lrange mykey 0 0
1) "e"

2,LPOP/LLEN

127.0.0.1:6379> del mykey
(integer) 1
127.0.0.1:6379> lpush mykey a b c d
(integer) 4
127.0.0.1:6379> lpop mykey     #Removes and returns the first element of the mykey key, the first from right to left
"d"
127.0.0.1:6379> lpop mykey
"c"
127.0.0.1:6379> llen mykey     #Get the number of elements in the list. After executing the lpop command twice, the two elements in the head of the linked list have been popped up. At this time, the number of elements in the linked list is 2
(integer) 2

3,LREM/LSET/LINDEX/LTRIM

LREM key count value: Delete from head count Values are value And returns the actual number of deleted elements
LSET key index value: Index location as index Set new value for element value
LINDEX key index: Get index is index Element of
LTRIM key start stop: Keep index from location only start To index stop Element of
127.0.0.1:6379> del mykey
(integer) 1
127.0.0.1:6379> lpush mykey a b c d a c     #Prepare test data for the following examples
(integer) 6
127.0.0.1:6379> lrem mykey 2 a     #Delete two elements with a value equal to a from the left variable list to the right variable list, and the return value is the actual deleted quantity
(integer) 2
127.0.0.1:6379> lrange mykey 0 -1     #See all the elements in the linked list after deletion
1) "c"
2) "d"
3) "c"
4) "b"
127.0.0.1:6379> lindex mykey 1     #Gets the element value with an index value of 1 (the second element of the header)
"d"
127.0.0.1:6379> lset mykey 1 e     #Set the element value with an index value of 1 (the second element of the header) to the new value e
OK
127.0.0.1:6379> lindex mykey 1     #Check whether the setting is successful
"e"
127.0.0.1:6379> lindex mykey 6      #If the index value 6 exceeds the number of elements in the linked list, the command returns nil
(nil)
127.0.0.1:6379> lset mykey 6 hh     #The set index value 6 exceeds the number of elements in the linked list. The setting fails. The command returns an error message
(error) ERR index out of range
127.0.0.1:6379> ltrim mykey 0 2     #Only 3 elements with index values between 0 and 2 are reserved. Note that both the 0th and 2nd elements are reserved
OK
127.0.0.1:6379> lrange mykey 0 -1     #View the results after ltrim
1) "c"
2) "e"
3) "c"

4,LINSERT

LINSERT key BEFORE|AFTER pivot value: In element pivot Insert a new element before (left) or after (right) value
127.0.0.1:6379> del mykey      #Delete this key for later testing
(integer) 1
127.0.0.1:6379> lpush mykey a b c d e     #Prepare test data for the following examples
(integer) 5
127.0.0.1:6379> linsert mykey before a a1     #Insert a new element a1 before a
(integer) 6
127.0.0.1:6379> lrange mykey 0 -1     #Check whether the insertion is successful. The result shows that it has been inserted
1) "e"
2) "d"
3) "c"
4) "b"
5) "a1"
6) "a"
127.0.0.1:6379> linsert mykey after e e2     #Insert the new element e2 after e. from the returned result, it can be seen that the insertion has been successful
(integer) 7
127.0.0.1:6379> lindex mykey 1     #Check again to see if the insertion is successful
"e2"
127.0.0.1:6379> linsert mykey after k a     #When a new element is inserted before or after a non-existent element, the linsert command operation fails and returns - 1
(integer) -1
127.0.0.1:6379> linsert mykey1 after a a2      #Insert a new element for a nonexistent Key. The linsert command operation failed and returned 0
(integer) 0

5,RPUSH/ RPUSHX/RPOP/RPOPLPUSH

RPUSH key value [value ...]Insert at the end of the list value
RPUSHX key value: key Must exist to execute, will value Inserts from the tail and returns the number of all elements
RPOP key: Pop up (remove) an element at the tail and return it
RPOPLPUSH source destination: stay key1 An element pops up at the end of and returns, inserting it key2 Head of
127.0.0.1:6379> del mykey     #Delete this key for later testing
(integer) 1
127.0.0.1:6379> rpush mykey a b c d     #Insert the values given in the parameters from the tail of the linked list in the order from right to left
(integer) 4
127.0.0.1:6379> lrange mykey 0 -1     #You can learn the insertion order of rpush when inserting multiple values through the lrange command
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379> rpushx mykey e      #The key already exists and contains four elements. The rpushx command will execute successfully and insert element e into the tail of the linked list
(integer) 5
127.0.0.1:6379> lindex mykey 4     #From the lindex command, we can see that the previous rpushx command is indeed successful, because the element with index value of 4 is already a new element
"e"
127.0.0.1:6379> rpushx mykey2 e     #Since the mykey2 key does not exist, the rpushx command does not insert data, and its return value is 0
(integer) 0
127.0.0.1:6379> lrange mykey 0 -1     #Before executing the rpoplpush command, first look at the elements of the linked list in mykey and pay attention to their positional relationship
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> rpop mykey     #Remove and return the first element of the mykey key, taken from the right
"e"
127.0.0.1:6379> lrange mykey 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379> rpoplpush mykey mykey2     #Pop up the tail element e of MyKey and insert it into the head of mykey2 (atomically complete these two steps)
"d"
127.0.0.1:6379> lrange mykey 0 -1       #Use the lrange command to view the result of mykey after popping the tail element
1) "a"
2) "b"
3) "c"
127.0.0.1:6379> lrange mykey2 0 -1     #View the result of mykey2 after inserting the element through the lrange command
1) "d"
127.0.0.1:6379> rpoplpush mykey mykey      #Set source and destination to the same key and move the tail element in mykey to its head
"c"
127.0.0.1:6379> lrange mykey 0 -1     #View move results
1) "c"
2) "a"
3) "b"

3, Hash data type (hash type)

Overview: hash is used to store objects. This naming method (hash format) can be adopted: the object category and ID constitute the key name, the field is used to represent the attribute of the object, and the field value stores the attribute value.

If the Hash contains few fields, this type of data will also take up very little disk space. Each Hash can store 4294967295 key value pairs.

1,HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX

127.0.0.1:6379> hset myhash field1 "zhang"     #Set the field to field1 and the value to zhang for the key with the key value of myhash
(integer) 1
127.0.0.1:6379> hget myhash field1      #Get the value whose key value is myhash and field is field1
"zhang"
127.0.0.1:6379> hget myhash field2      #The field2 field does not exist in the myhash key, so nil is returned
(nil)
127.0.0.1:6379> hset myhash field2 "san"     #Add a new field field2 to myhash with the value san
(integer) 1
127.0.0.1:6379> hlen myhash     #The hlen command gets the number of fields for the myhash key
(integer) 2
127.0.0.1:6379> hexists myhash field1     #Judge whether there is a field named field1 in the myhash key. Because it exists, the return value is 1
(integer) 1
127.0.0.1:6379> hdel myhash field1     #Delete the field named field1 in the myhash key, and 1 will be returned if the deletion is successful
(integer) 1
127.0.0.1:6379> hdel myhash field1     #Delete the field named field1 in the myhash key again. Since it has been deleted in the previous command, it returns 0 because it has not been deleted
(integer) 0
127.0.0.1:6379> hexists myhash field1     #Judge whether the field1 field exists in the myhash key. Since it has been deleted in the previous command, it returns 0
(integer) 0
127.0.0.1:6379> hsetnx myhash field1 zhang     #Use the hsetnx command to add a new field field1 to myhash with the value of zhang. Because the field has been deleted, the command adds successfully and returns 1
(integer) 1
127.0.0.1:6379> hsetnx myhash field1 zhang     #Since the field1 field of myhash has been successfully added through the previous command, this command returns 0 after doing nothing
(integer) 0

2,HINCRBY

127.0.0.1:6379> del myhash      #Delete this key to facilitate the test of the following example
(integer) 1
127.0.0.1:6379> hset myhash field 5     #Prepare the test data and set the field value of myhash to 5
(integer) 1
127.0.0.1:6379> hincrby myhash field 1      #The hincrby command adds 1 to the value of the field of myhash and returns the added result
(integer) 6
127.0.0.1:6379> hincrby myhash field -1      #The hincrby command adds - 1 to the value of the field of myhash and returns the added result
(integer) 5
127.0.0.1:6379> hincrby myhash field -10      #The hincrby command adds - 10 to the value of the field of myhash and returns the added result
(integer) -5

3,HGETALL/HKEYS/HVALS/HMGET/HMSET

127.0.0.1:6379> del myhash     #Delete this key for later example testing
(integer) 1
127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"     #The hmset command sets multiple fields for the key myhash at one time, field1="hello" and field2="world"
OK
127.0.0.1:6379> hmget myhash field1 field2 field3     #The hmget command obtains multiple fields of the myhash key, of which field3 does not exist because the value corresponding to this field in the returned result is nil
1) "hello"
2) "world"
3) (nil)
127.0.0.1:6379> hgetall myhash     #The hgetall command returns all the fields of the myhash key and their values. From the results, we can see that they are listed pair by pair
1) "field1"
2) "hello"
3) "field2"
4) "world"
127.0.0.1:6379> hkeys myhash     #The hkeys command only gets the names of all fields in the myhash key
1) "field1"
2) "field2"
127.0.0.1:6379> hvals myhash      #The hvals command only gets the values of all fields in the myhash key
1) "hello"
2) "world"

4, Set data type (unordered set)

Overview: unordered collection. The element type is string. The element is unique. Duplicate members are not allowed. Union, intersection and difference operations can be performed among multiple set types.

Application scope: (1) Redis's Set data type can be used to track some unique data, such as the unique IP address information of accessing a blog. For this scenario, we only need to store the visitor's IP in Redis every time we visit the blog, and the Set data type will automatically ensure the uniqueness of the IP address; (2) Making full use of the convenient and efficient characteristics of Set type server aggregation operation, it can be used to maintain the association relationship between data objects. For example, all customer IDs for purchasing an electronic device are stored in a specified Set, while customer IDs for purchasing another electronic product are stored in another Set. If we want to obtain which customers have purchased these two products at the same time, the intersection command of Set can give full play to its convenience and efficiency.

1,SADD/SMEMBERS/SCARD/SISMEMBER

127.0.0.1:6379> sadd myset a b c     #Insert test data. Since the key myset does not exist before, all three members in the parameter are inserted normally
(integer) 3
127.0.0.1:6379> sadd myset a d e     #Since a in the parameter already exists in myset, only two new members d and e are inserted in this operation
(integer) 2
127.0.0.1:6379> sismember myset a     #Judge whether a already exists. A return value of 1 indicates existence
(integer) 1
127.0.0.1:6379> sismember myset f     #Judge whether f already exists. If the return value is 0, it means it does not exist
(integer) 0
127.0.0.1:6379> smembers myset     #View the insertion results through the smembers command. From the results, it can be seen that the output order is independent of the insertion order
1) "b"
2) "a"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> scard myset     #Gets the number of elements in the Set collection
(integer) 5

2,SPOP/SREM/SRANDMEMBER/SMOVE

127.0.0.1:6379> del myset     #Delete this key for later testing
(integer) 1
127.0.0.1:6379> sadd myset a b c d      #Prepare test data for the following examples
(integer) 4
127.0.0.1:6379> smembers myset     #View the location of members in a Set
1) "c"
2) "d"
3) "b"
4) "a"
127.0.0.1:6379> srandmember myset     #It can be seen from the results that the command does return a member randomly
"b"
127.0.0.1:6379> spop myset     #Randomly remove and return a member of the Set
"c"
127.0.0.1:6379> smembers myset     #View the member information of set after moving out
1) "d"
2) "b"
3) "a"
127.0.0.1:6379> srem myset a d f     #Remove three members a, d and F from myseet, where f does not exist, so only two members a and d are removed and returned as 2
(integer) 2
127.0.0.1:6379> smembers myset      #View the output results after removal
1) "b"
127.0.0.1:6379> sadd myset a b     #Prepare data for subsequent smove commands
(integer) 1
127.0.0.1:6379> sadd myset2 c d
(integer) 2
127.0.0.1:6379> smove myset myset2 a     #Move a from myset to myset2. The result shows that the move is successful
(integer) 1
127.0.0.1:6379> smove myset myset2 a     #Move a from myset to myset2 again. Since a is no longer a member of myset, the move fails and returns 0
(integer) 0
127.0.0.1:6379> smembers myset      #Check the members of myset and myset2 respectively to confirm whether the move is really successful
1) "b"
127.0.0.1:6379> smembers myset2
1) "c"
2) "d"
3) "a"

5, Sorted Set data type (zset, ordered set)

Overview: ordered set. The element type is Sting. The element is unique and cannot be repeated; Each element is associated with a score of double type (representing weight), which can be sorted by the size of weight, and the scores of elements can be the same.

Scope of application: it can be used for the leaderboard of a large online game. Whenever the player's score changes, you can execute the ZADD command to update the player's score, and then obtain the user information of the score TOP10 through the ZRANGE command. Of course, we can also use the ZRANK command to obtain the ranking information of players through username. Finally, we will combine the ZRANGE and ZRANK commands to quickly obtain the information of other users with similar points to a player.

The sorted set type can also be used to build index data.

1,ZADD/ZCARD/ZCOUNT/ZREM/ZINCRBY/ZSCORE/ZRANGE/ZRANK

127.0.0.1:6379> zadd myzset 1 "one"     #Add a member with a score of 1
(integer) 1
127.0.0.1:6379> zadd myzset 2 "two" 3 "three"     #Add two members whose scores are 2 and 3 respectively
(integer) 2
127.0.0.1:6379> zrange myzset 0 -1 withscores     #0 represents the first member, - 1 represents the last member. The with scores option means that each member and its score are included in the returned result. Otherwise, only members are returned
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
127.0.0.1:6379> zrank myzset one     #Gets the position index value of the member one in the sorted set. 0 indicates the first position
(integer) 0
127.0.0.1:6379> zrank myzset four     #The member four does not exist, so nil is returned
(nil)
127.0.0.1:6379> zcard myzset     #Gets the number of members in the myzset key
(integer) 3
127.0.0.1:6379> zcount myzset 1 2     #zcount key min max, the number of members whose score satisfies the expression 1 < = score < = 2
(integer) 2
127.0.0.1:6379> zrem myzset one two     #Delete members one and two, and return the actual number of deleted members
(integer) 2
127.0.0.1:6379> zcard myzset     #Check whether the deletion is successful
(integer) 1
127.0.0.1:6379> zscore myzset three     #Gets the score of the member three. The return value is in string form
"3"
127.0.0.1:6379> zscore myzset two     #Since the member two has been deleted, the command returns nil
(nil)
127.0.0.1:6379> zincrby myzset 2 one      #If the member one does not exist, the zincrby command will add the member and assume that its initial score is 0, increase the score of member one by 2, and return the updated score of the member
"2"
127.0.0.1:6379> zincrby myzset -1 one     #Increase the score of member one by - 1 and return the updated score of the member
"1"
127.0.0.1:6379> zrange myzset 0 -1 withscores     #Check to see if the member's score is correct after updating
1) "one"
2) "1"
3) "three"
4) "3"

2,ZRANGE BY SCORE/ZREM RANGE BY RANK/ZREM RANGE BY SCORE

127.0.0.1:6379> del myzset
(integer) 1
127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
(integer) 4
127.0.0.1:6379> zrangebyscore myzset 1 2     #zrangebyscore key min max, get the member whose score satisfies expression 1 < = score < = 2
1) "one"
2) "two"
127.0.0.1:6379> zrangebyscore myzset (1 2     #Gets the member whose score satisfies the expression 1 < score < = 2
1) "two"
127.0.0.1:6379> zrangebyscore myzset -inf +inf limit 2 3      #-Inf indicates the first member (the member with the lowest location index value, i.e. 0), + inf indicates the last member (the member with the highest location index value). The parameters after limit are used to limit the value of the returned member. 2 indicates that it starts from the member with the location index equal to 2 and takes the next three members.
1) "three"
2) "four"
127.0.0.1:6379> zrangebyscore myzset 0 4 limit 2 3
1) "three"
2) "four"
127.0.0.1:6379> zremrangebyscore myzset 1 2     #Delete members whose scores meet the expression 1 < = score < = 2, and return the actual deleted quantity
(integer) 2
127.0.0.1:6379> zrange myzset 0 -1     #Check whether the above deletion is successful
1) "three"
2) "four"
127.0.0.1:6379> zremrangebyrank myzset 0 1      #Delete members whose location index satisfies the expression 0 < = rank < = 1
(integer) 2
127.0.0.1:6379> zcard myzset     #Check whether the last command was deleted successfully
(integer) 0

3,ZREVRANGE/ZREVRANGEBYSCORE/ZREVRANK

127.0.0.1:6379> del myzset     #Prepare test data for the following examples
(integer) 0
127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
(integer) 4
127.0.0.1:6379> zrevrange myzset 0 -1 withscores     #Get and return the members in this interval from high to low by location index
1) "four"
2) "4"
3) "three"
4) "3"
5) "two"
6) "2"
7) "one"
8) "1"
127.0.0.1:6379> zrevrange myzset 1 3      #Since it is sorted from high to low, the position equal to 0 is four, 1 is three, and so on
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> zrevrank myzset one     #Because it is sorted from high to low, the position / index subscript of one is 3
(integer) 3
127.0.0.1:6379> zrevrank myzset four     #Because it is sorted from high to low, the position of four is 0
(integer) 0
127.0.0.1:6379> zrevrangebyscore myzset 3 0      #zrevrangebyscore key max min, obtain the members whose scores meet the expression 3 > = score > = 0, and output them in the order from high to bottom
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> zrevrangebyscore myzset 4 0 limit 1 2     #The zrevrangebyscore command supports the limit option, which has the same meaning as the option in zrangebycore. It only calculates and obtains the location in the reverse order
1) "three"
2) "two"
127.0.0.1:6379> zrevrangebyscore myzset +inf -inf limit 1 3
1) "three"
2) "two"
3) "one"

summary

1. String string type: get, mget

2. list: lrange, lindex

3. hash types: hget, hmget, hgetall, hkeys, hvals

4. Set unordered set: smember

5. set order: zrange

6. - 1 means: permanent- 2 means: expired

7. setex: set expiration time; setnx: judge whether the target key exists

Posted by maest on Wed, 10 Nov 2021 00:56:44 -0800