catalogue
4, Set data type (unordered set)
5, Sorted Set data type (zset, ordered set)
1, String data type
Overview: string is the most basic type of redis, which can store up to 512MB of data. String type is binary safe, that is, it can store any data, such as numbers, pictures, serialized objects, etc
1. SET/GET/APPEND/STRLEN: redis 127.0.0.1:6379> exists mykey #Judge whether the key exists. If it exists, return 1; otherwise, return 0. (integer) 0 redis 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 redis 127.0.0.1:6379> append mykey " world" #The key already exists, so the length of the appended Value is returned. (integer) 11 redis 127.0.0.1:6379> get mykey #Get the key through the get command to judge the result of append. "hello world" redis 127.0.0.1:6379> set mykey "this is a test" #Set a new value for the key through the set command and overwrite the original value. OK redis 127.0.0.1:6379> get mykey "this is a test" redis 127.0.0.1:6379> strlen mykey #Gets the character length of the specified Key. (integer) 14 2. INCR/DECR/INCRBY/DECRBY: redis 127.0.0.1:6379> set mykey 20 #Set the value of Key to 20 OK redis 127.0.0.1:6379> incr mykey #The value of this Key is incremented by 1 (integer) 21 redis 127.0.0.1:6379> decr mykey #The value of this Key decreases by 1 (integer) 20 redis 127.0.0.1:6379> del mykey #Delete existing keys. (integer) 1 redis 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 redis 127.0.0.1:6379> del mykey (integer) 1 redis 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 redis 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 redis 127.0.0.1:6379> incr mykey (error) ERR value is not an integer or out of range redis 127.0.0.1:6379> set mykey 10 OK redis 127.0.0.1:6379> decrby mykey 5 #Decrements the specified integer (integer) 5 redis 127.0.0.1:6379> incrby mykey 10 #Increment the specified integer (integer) 15 3. GETSET: redis 127.0.0.1:6379> incr mycounter #Increments the value of the counter atomically by 1 (integer) 1 redis 127.0.0.1:6379> getset mycounter 0 #While obtaining the original value of the counter and setting it to the new value, the two operations are completed atomically at the same time. "1" redis 127.0.0.1:6379> get mycounter #View the results after setting. "0" 4. SETEX: redis 127.0.0.1:6379> setex mykey 15 "hello" #Set the expiration time of the specified Key to 10 seconds. OK redis 127.0.0.1:6379> ttl mykey #Check the remaining survival time (seconds) of the specified Key through the ttl command, - 2 means it has expired and - 1 means it will never expire. (integer) 4 redis 127.0.0.1:6379> get mykey #During the lifetime of the key, we can still get its Value. "hello" redis 127.0.0.1:6379> ttl mykey #The return value of the ttl command shows that the Key has expired. (integer) -2 redis 127.0.0.1:6379> get mykey #Getting an expired Key will return nil. (nil) 5. SETNX: redis 127.0.0.1:6379> del mykey #Delete this key to facilitate the following test verification. (integer) 1 redis 127.0.0.1:6379> setnx mykey "hello" #The key does not exist, so the setnx command was executed successfully. (integer) 1 redis 127.0.0.1:6379> setnx mykey "world" #This key already exists, so this setting has no effect. (integer) 0 redis 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: redis 127.0.0.1:6379> mset key1 "hello" key2 "world" #key1 and key2 are set in batch. OK redis 127.0.0.1:6379> mget key1 key2 #The values of key1 and key2 are obtained in batch. 1) "hello" 2) "world" redis 127.0.0.1:6379> msetnx key3 "zhang" key4 "san" #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 redis 127.0.0.1:6379> mget key3 key4 1) "zhang" 2) "san" redis 127.0.0.1:6379> msetnx key3 "hello" key5 "world" #key3 and key5 are set in batch, but key3 already exists, so the msetnx command fails and returns 0. (integer) 0 redis 127.0.0.1:6379> mget key3 key5 #Get key3 and key5 in batch. Since key5 is not set successfully, nil is returned. 1) "zhang" 2) (nil)
2, List data type
Overview: the element type of the list is string, which is sorted according to the insertion order. Add elements at the head or tail of the list
1. LPUSH/LPUSHX/LRANGE: redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> lpush mykey a b c d #The mykey key does not exist. The command will create the key and its associated List, and then insert the values in the parameter from left to right. (integer) 4 redis 127.0.0.1:6379> lrange mykey 0 2 #Take 3 elements from position 0 to position 2. 1) "d" 2) "c" 3) "b" redis 127.0.0.1:6379> lrange mykey 0 -1 #Take all the elements in the linked list, where 0 represents the first element and - 1 represents the last element. 1) "d" 2) "c" 3) "b" 4) "a" redis 127.0.0.1:6379> lpushx mykey2 e #The mykey2 key does not exist at this time, so the lpushx command will not do anything, and its return value is 0. (integer) 0 redis 127.0.0.1:6379> lrange mykey2 0 -1 #You can see that mykey2 is not associated with any List Value. (empty list or set) redis 127.0.0.1:6379> lpushx mykey e #The mykey key already exists at this time, so the lpushx command inserts successfully and returns the number of current elements in the linked list. (integer) 5 redis 127.0.0.1:6379> lrange mykey 0 0 #Gets the header element of the List Value of the key. 1) "e" 2. LPOP/LLEN: redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> lpush mykey a b c d (integer) 4 redis 127.0.0.1:6379> lpop mykey #Remove and return the first element of the mykey key, taken from the left "d" redis 127.0.0.1:6379> lpop mykey "c" redis 127.0.0.1:6379> llen mykey #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: redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> lpush mykey a b c d a c #Prepare test data for the following examples. (integer) 6 redis 127.0.0.1:6379> lrem mykey 2 a #From the left to the right variable linked list, delete two elements with a value equal to a, and the return value is the actual number deleted. (integer) 2 redis 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" redis 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" redis 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 redis 127.0.0.1:6379> lindex mykey 1 #Check whether the setting is successful. "e" redis 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) redis 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 redis 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 redis 127.0.0.1:6379> lrange mykey 0 -1 #View the results after trim. 1) "c" 2) "e" 3) "c" 4. LINSERT: redis 127.0.0.1:6379> del mykey #Delete this key for later testing. (integer) 1 redis 127.0.0.1:6379> lpush mykey a b c d e #Prepare test data for the following examples. (integer) 5 redis 127.0.0.1:6379> linsert mykey before a a1 #Insert a new element a1 before a. (integer) 6. redis 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" redis 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 redis 127.0.0.1:6379> lindex mykey 1 #Check again to see if the insertion is successful. "e2" redis 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 redis 127.0.0.1:6379> linsert mykey1 after a a2 #Insert a new element for a nonexistent Key. The linsert command operation fails and returns 0. (integer) 0 5. RPUSH/RPUSHX/RPOP/RPOPLPUSH: redis 127.0.0.1:6379> del mykey #Delete this key for later testing. (integer) 1 redis 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 redis 127.0.0.1:6379> lrange mykey 0 -1 #You can know the insertion order of rpush when inserting multiple values through lrange command. 1) "a" 2) "b" 3) "c" 4) "d" redis 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 redis 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" redis 127.0.0.1:6379> rpushx mykey2 e #Since the mykey2 key does not exist, the rpushx command will not insert data and its return value is 0. (integer) 0 redis 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" redis 127.0.0.1:6379> rpoplpush mykey mykey2 #Pop up the tail element e of MyKey and insert it into the head of mykey2 at the same time (atomically complete these two steps). "d" redis 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" redis 127.0.0.1:6379> lrange mykey2 0 -1 #View the result of mykey2 after inserting the element through the lrange command. 1) "d" redis 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" redis 127.0.0.1:6379> lrange mykey 0 -1 #View the move results. 1) "c" 2) "a" 3) "b"
3, Hash data type (hash type)
Overview: hash is used to store objects. This naming method 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. For example: store the car object with ID 2.
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: redis 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 redis 127.0.0.1:6379> hget myhash field1 #Get the value whose key value is myhash and field is field1. "zhang" redis 127.0.0.1:6379> hget myhash field2 #The field2 field does not exist in the myhash key, so nil is returned. (nil) redis 127.0.0.1:6379> hset myhash field2 "san" #Add a new field field2 to myhash with the value san. (integer) 1 redis 127.0.0.1:6379> hlen myhash #The hlen command gets the number of fields for the myhash key. (integer) 2 redis 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 redis 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 redis 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 redis 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 redis 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 redis 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: redis 127.0.0.1:6379> del myhash #Delete this key to facilitate the test of the following example. (integer) 1 redis 127.0.0.1:6379> hset myhash field 5 #Prepare the test data, and set the field value of myhash to 5. (integer) 1 redis 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 redis 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 redis 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: redis 127.0.0.1:6379> del myhash #Delete this key to facilitate the following example test. (integer) 1 redis 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 redis 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) redis 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" redis 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" redis 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. Make full use of the convenient and efficient characteristics of Set type server aggregation operation, which 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 the customer ID for purchasing another electronic product is stored in another Set. If we want to obtain which customers have purchased these two goods at the same time, the intersection command of Set can give full play to its convenience and efficiency.
1. SADD/SMEMBERS/SCARD/SISMEMBER: redis 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 redis 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 redis 127.0.0.1:6379> sismember myset a #Judge whether a already exists. A return value of 1 indicates existence. (integer) 1 redis 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 redis 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) "c" 2) "d" 3) "a" 4) "b" 5) "e" redis 127.0.0.1:6379> scard myset #Gets the number of elements in the Set collection. (integer) 5 2. SPOP/SREM/SRANDMEMBER/SMOVE: redis 127.0.0.1:6379> del myset #Delete this key for later testing. (integer) 1 redis 127.0.0.1:6379> sadd myset a b c d #Prepare test data for the following examples. (integer) 4 redis 127.0.0.1:6379> smembers myset #View the location of members in the Set. 1) "c" 2) "d" 3) "a" 4) "b" redis 127.0.0.1:6379> srandmember myset #It can be seen from the results that the command does return a member randomly. "c" redis 127.0.0.1:6379> spop myset #Randomly remove and return a member of the Set. "b" redis 127.0.0.1:6379> smembers myset #View the member information of Set after moving out. 1) "c" 2) "d" 3) "a" redis 127.0.0.1:6379> srem myset a d f #Remove three members a, d and F from the Set, where f does not exist, so only two members a and d are removed and returned as 2. (integer) 2 redis 127.0.0.1:6379> smembers myset #View the output results after removal. 1) "c" redis 127.0.0.1:6379> sadd myset a b #Prepare the data for the following smove command. (integer) 2 redis 127.0.0.1:6379> sadd myset2 c d (integer) 2 redis 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 redis 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 redis 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" redis 127.0.0.1:6379> smembers myset2 1) "c" 2) "d" 3) "a"
5, Sorted Set data type (zset, ordered set)
Overview: a. ordered set. The element type is Sting. The element is unique and cannot be repeated.
b. Each element will be associated with a score of double type (representing weight), which can be sorted by the size of weight, and the score of elements can be the same.
Application scope:
1) 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 top 10 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.
2) The sorted set type can also be used to build index data.
1. ZADD/ZCARD/ZCOUNT/ZREM/ZINCRBY/ZSCORE/ZRANGE/ZRANK: redis 127.0.0.1:6379> zadd myzset 1 "one" #Add a member with a score of 1. (integer) 1 redis 127.0.0.1:6379> zadd myzset 2 "two" 3 "three" #Add two members whose scores are 2 and 3 respectively. (integer) 2 redis 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" redis 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 redis 127.0.0.1:6379> zrank myzset four #The member four does not exist, so nil is returned. (nil) redis 127.0.0.1:6379> zcard myzset #Gets the number of members in the myzset key. (integer) 3 redis 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 redis 127.0.0.1:6379> zrem myzset one two #Delete members one and two, and return the actual number of deleted members. (integer) 2 redis 127.0.0.1:6379> zcard myzset #Check whether the deletion is successful. (integer) 1 redis 127.0.0.1:6379> zscore myzset three #Get the score of member three. The return value is in string form. "3" redis 127.0.0.1:6379> zscore myzset two #Since the member two has been deleted, the command returns nil. (nil) redis 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" redis 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" redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES #Check to see if it is correct after updating the member's score. 1) "one" 2) "1" 3) "three" 4) "3" 2. ZRANGEBYSCORE/ZREMRANGEBYRANK/ZREMRANGEBYSCORE redis 127.0.0.1:6379> del myzset (integer) 1 redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four (integer) 4 redis 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" redis 127.0.0.1:6379> zrangebyscore myzset (1 2 #Gets the member whose score satisfies the expression 1 < score < = 2. 1) "two" redis 127.0.0.1:6379> zrangebyscore myzset -inf +inf limit 2 3 #-Inf refers to the first member (the member with the lowest location index value, i.e. 0), + inf refers to 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 refers to starting from the member with the location index equal to 2 and taking the next three members. 1) "three" 2) "four" redis 127.0.0.1:6379> zrangebyscore myzset 0 4 limit 2 3 redis 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 redis 127.0.0.1:6379> zrange myzset 0 -1 #See if the deletion above is successful. 1) "three" 2) "four" redis 127.0.0.1:6379> zremrangebyrank myzset 0 1 #Delete members whose location index satisfies the expression 0 < = rank < = 1. (integer) 2 redis 127.0.0.1:6379> zcard myzset #Check whether the previous command was deleted successfully. (integer) 0 3. ZREVRANGE/ZREVRANGEBYSCORE/ZREVRANK: redis 127.0.0.1:6379> del myzset #Prepare test data for the following examples. (integer) 0 redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four (integer) 4 redis 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" redis 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" redis 127.0.0.1:6379> zrevrank myzset one #Because it is sorted from high to low, the position of one is 3. (integer) 3 redis 127.0.0.1:6379> zrevrank myzset four #Because it is sorted from high to low, the position of four is 0. (integer) 0 redis 127.0.0.1:6379> zrevrangebyscore myzset 3 0 #zrevrangebyscore key max min, get the members whose scores meet the expression 3 > = score > = 0, and output them in the order from top to bottom. 1) "three" 2) "two" 3) "one" redis 127.0.0.1:6379> zrevrangebyscore myzset 4 0 limit 1 2 #The zrevrangebyscore command supports the limit option, which is equivalent to the option in zrangebyscore, but is calculated and obtained in the reverse order when calculating the location. 1) "three" 2) "two" 192.168.80.10:6379> zrevrangebyscore myzset +inf -inf limit 1 3