1, String type
String is the most basic type of redis. It can store up to 512MB of data. String is binary safe, that is, it can store any data, such as numbers, pictures, serialized objects, etc
SET/GET/APPEND/STRLEN
127.0.0.1:6379> set d1 1 #Create a key value OK 127.0.0.1:6379> get d1 #Gets the value of the key "1" 127.0.0.1:6379> set d2 2 OK 127.0.0.1:6379> keys * #See which keys can be combined d? Or d * use 1) "d2" 2) "d1" 127.0.0.1:6379> set d1 12 #The existing will be overwritten OK 127.0.0.1:6379> get d1 "12" 127.0.0.1:6379> append d1 1 #Append key value (integer) 3 127.0.0.1:6379> get d1 "121" 127.0.0.1:6379> append d3 32 #For keys that do not exist, a (integer) 2 127.0.0.1:6379> get d3 "32" 127.0.0.1:6379> exists d1 #Determine whether the key exists (integer) 1 127.0.0.1:6379> exists d4 (integer) 0 127.0.0.1:6379> strlen d1 #Judge key length (integer) 3
INCR/DECR/INCRBY/DECRBY
incr key : key The value is incremented by 1( key Value must be an integer) decr key: key The value decreases by 1( key Value must be an integer) decrby key 5 : key The value is incremented by 5( key Value must be an integer) incrby key 5 : key The value decreases by 5( key Value must be an integer)
GETSET
getset key value: obtain key And return, and give key Set new value 127.0.0.1:6379> get d1 "122" 127.0.0.1:6379> getset d1 0 #First return the old value and set the new value "122" 127.0.0.1:6379> get d1 "0" 127.0.0.1:6379> getset d5 1 #For keys that do not, it also creates (nil) 127.0.0.1:6379> get d5 "1"
SETEX
setex key seconds value: Set the specified key How many seconds does it expire 127.0.0.1:6379> setex d2 10 hello #Set d2 expiration time to 10 seconds OK 127.0.0.1:6379> get d2 #Available within the time frame "hello" 127.0.0.1:6379> ttl d2 #View d2's lifecycle (integer) 5 127.0.0.1:6379> ttl d2 (integer) -2 #When it is - 2, it means it has expired, and - 1 means it will never expire 127.0.0.1:6379> get d2 (nil) #If it is expired, there will be no key value, and it cannot be obtained
SETNX
setnx key value : Execute if the key does not exist set Operation, if it exists, it will not be executed 127.0.0.1:6379> keys * 1) "d3" 2) "d1" 3) "d5" 127.0.0.1:6379> get d3 "32" 127.0.0.1:6379> setnx d2 10 #No key, created successfully (integer) 1 127.0.0.1:6379> get d2 "10" 127.0.0.1:6379> setnx d3 10 #Some keys failed to create (integer) 0 127.0.0.1:6379> get d3 "32"
MSET/MGET/MSETNX
mset key value [key value...] : Batch setting key value pairs mget key [key ...] : Batch get key value pairs msetnx key value [key value ...] : Set key value pairs in batch. If none of them exist, execute and return 1; As long as one exists, it will not execute and return 0 127.0.0.1:6379> keys * 1) "d2" 2) "d3" 3) "d1" 4) "d5" 127.0.0.1:6379> mset d4 4 d5 5 d6 6 #Support batch creation and overwrite OK 127.0.0.1:6379> mget d4 d5 d6 #Batch acquisition 1) "4" 2) "5" 3) "6" 127.0.0.1:6379> msetnx d6 7 d7 7 d8 8 #msetnx if there are existing keys, the creation will not be performed, and the existing keys will be judged (integer) 0 127.0.0.1:6379> get d7 (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
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 (index starts from 0) 127.0.0.1:6379> lpush mykey a b c d e f g Insert elements in turn on the far left (integer) 7 127.0.0.1:6379> lrange mykey 0 -1 Show all from element 0 to the following -1 Represents the maximum value 1) "g" 2) "f" 3) "e" 4) "d" 5) "c" 6) "b" 7) "a" 127.0.0.1:6379> rpush dkey a b c d e f g #Insert from far right (integer) 7 127.0.0.1:6379> lrange dkey 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 7) "g" 127.0.0.1:6379> type mykey #Type displays the data type list 127.0.0.1:6379> lpushx mykey2 2 #For a key without, the insertion fails (integer) 0 127.0.0.1:6379> lpushx mykey z #The existing key will be inserted successfully and inserted in the first key (integer) 8 127.0.0.1:6379> lrange mykey 0 -1 1) "z" 2) "g" 3) "f" 4) "e" 5) "d" 6) "c" 7) "b" 8) "a"
LPOP/LLEN
lpop key : Remove and return key The first element of the key, from right to left llen key: Get the number of elements in the table, and execute lpop After two commands, the two elements at the head of the linked list are ready to pop up. At this time, the number of elements in the linked list is 2 127.0.0.1:6379> lrange mykey 0 -1 1) "z" 2) "g" 3) "f" 4) "e" 5) "d" 6) "c" 7) "b" 8) "a" 127.0.0.1:6379> lpop mykey #Pop up the first element "z" 127.0.0.1:6379> lpop mykey "g" 127.0.0.1:6379> llen mykey #You can see that after two elements pop up, there are still six elements left (integer) 6
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> lrange mykey 0 -1 1) "c" 2) "b" 3) "a" 4) "f" 5) "e" 6) "d" 7) "c" 8) "b" 9) "a" 127.0.0.1:6379> lrem mykey 2 a #Delete 2 a's from scratch (integer) 2 127.0.0.1:6379> lrange mykey 0 -1 1) "c" 2) "b" 3) "f" 4) "e" 5) "d" 6) "c" 7) "b" 127.0.0.1:6379> lrem mykey 3 b #If the number of elements exceeds, it does not affect (integer) 2 #Actually deleted 2 b's because there are only 2 b's 127.0.0.1:6379> lrange mykey 0 -1 1) "c" 2) "f" 3) "e" 4) "d" 5) "c" 127.0.0.1:6379> lset mykey 2 c #Change the position of index 2 to c OK 127.0.0.1:6379> lrange mykey 0 -1 1) "c" 2) "f" 3) "c" 4) "d" 5) "c" 127.0.0.1:6379> lindex mykey 2 #Specify the value of 2 in the table below the view index "c" 127.0.0.1:6379> lset mykey 5 a #You cannot add an element where there is no index (error) ERR index out of range 127.0.0.1:6379> ltrim mykey 0 2 #Only the elements between mykey 0-2 will be retained OK 127.0.0.1:6379> lrange mykey 0 -1 1) "c" 2) "f" 3) "c"
LINSERT
linsert key before|after pivot value : In element pivot Insert a new element before (left) or after (right) the value 127.0.0.1:6379> lrange mykey 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 7) "g" 127.0.0.1:6379> linsert mykey before a 1 #Specifies to insert 1 on the left (front) side of the first a element of mykey (integer) 8 127.0.0.1:6379> lrange mykey 0 -1 1) "1" 2) "a" 3) "b" 4) "c" 5) "d" 6) "e" 7) "f" 8) "g" 127.0.0.1:6379> linsert mykey after g 2 #Insert 2 after the first g element in mykey (integer) 9 127.0.0.1:6379> lrange mykey 0 -1 1) "1" 2) "a" 3) "b" 4) "c" 5) "d" 6) "e" 7) "f" 8) "g" 9) "2"
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 an element at the end 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> lrange mykey 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 127.0.0.1:6379> rpushx mykey g #Insert g to the far right of the mykey key (integer) 7 127.0.0.1:6379> lrange mykey 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 7) "g" 127.0.0.1:6379> rpushx mykey2 g #Do not execute for keys without (integer) 0 127.0.0.1:6379> rpop mykey #Pop up an element from the left and right sides of the table "g" 127.0.0.1:6379> lrange mykey 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 127.0.0.1:6379> rpoplpush mykey mykey #Pop up the last element of mykey to the left of the inserted mykey "f" 127.0.0.1:6379> lrange mykey 0 -1 1) "f" 2) "a" 3) "b" 4) "c" 5) "d" 6) "e"
3, Hash data type (hash type)
Overview: hash is used to store objects. This naming method (hash 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.
/HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX
127.0.0.1:6379> hset myhash field1 "ding" #Set the field to field1 and the value to ding 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 "ding" 127.0.0.1:6379> hset myhash field2 "yu" (integer) 1 127.0.0.1:6379> hget myhash field2 "yu" 127.0.0.1:6379> hlen myhash #hlen gets the number of fields for the myhash key (integer) 2 127.0.0.1:6379> hexists myhash field1 #Determine whether the field1 field exists in the myhash key (integer) 1 127.0.0.1:6379> hexists myhash field3 (integer) 0 127.0.0.1:6379> hdel myhash field1 #Delete the field of field1 in the myhash key (integer) 1 127.0.0.1:6379> hsetnx myhash field1 ding #Judge whether there is field1 in the myhash key. If not, create it and the value is ding. If there is this field, no operation will be performed (integer) 1 127.0.0.1:6379> hsetnx myhash field1 fei (integer) 0 del myhash #Delete the myhash key hdel myhash field1 #Delete the field1 field in the myhash key
HINCRBY
127.0.0.1:6379> hset myhash field 5 (integer) 1 127.0.0.1:6379> hincrby myhash field -1 #Use hincrby to increase or decrease the fields in the myhash key, and set the amplitude yourself (integer) 4 127.0.0.1:6379> hincrby myhash field -1 (integer) 3 127.0.0.1:6379> hincrby myhash field 2 (integer) 5 127.0.0.1:6379> hincrby myhash field 2 (integer) 7
HGETALL/HKEYS/HVALS/HMGET/HMSET
127.0.0.1:6379> hmset myhash field1 ding field2 yu field3 fei #Batch setting of fields and field values in myhash OK 127.0.0.1:6379> hmget myhash field1 field2 field3 #Get the field values of field1, field2 and field3 in the myhash key in batch 1) "ding" 2) "yu" 3) "fei" 127.0.0.1:6379> hgetall myhash #Get the fields in the myhash key and the corresponding field values in batch 1) "field1" 2) "ding" 3) "field2" 4) "yu" 5) "field3" 6) "fei" 127.0.0.1:6379> hkeys myhash #See which fields are in the myhash key 1) "field1" 2) "field2" 3) "field3" 127.0.0.1:6379> hvals myhash #See which field values are in the myhash key 1) "ding" 2) "yu" 3) "fei"
4, Set data type (unordered set)
Overview: unordered set. The element type is string type. The element is unique. Duplicate members are not allowed. Union, intersection and difference operations can be performed between 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 the customer ID for purchasing another electronic product is 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 advantages of convenience and efficiency.
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 d e (integer) 2 127.0.0.1:6379> sadd myset a f g #Since a already exists, only f and g members can be added (integer) 2 127.0.0.1:6379> sismember myset a #Judge whether a already exists. The return value is 1 for existence and 0 for nonexistence (integer) 1 127.0.0.1:6379> sismember myset h (integer) 0 127.0.0.1:6379> smembers myset #See which members are in the myset key (unordered) 1) "c" 2) "f" 3) "a" 4) "e" 5) "b" 6) "g" 7) "d" 127.0.0.1:6379> scard myset #View the number of members of myset (integer) 7
SPOP/SREM/SRANDMEMBER/SMOVE
127.0.0.1:6379> sadd myset a b c d (integer) 4 127.0.0.1:6379> srandmember myset #Returns a random member from myset "b" 127.0.0.1:6379> spop myset #Randomly remove and return a member in myset (you can specify the number of pop-up members) "a" 127.0.0.1:6379> smembers myset 1) "c" 2) "d" 3) "b" 127.0.0.1:6379> srem myset a d f #Remove a, d and f from myset. If a and f do not exist, only d is removed, and 1.0 is returned (integer) 1 127.0.0.1:6379> smembers myset 1) "c" 2) "b" 127.0.0.1:6379> smove myset myset2 a #Move a from myset to myset2 (integer) 1 127.0.0.1:6379> smembers myset 1) "c" 2) "f" 3) "e" 4) "b" 5) "g" 6) "d" 127.0.0.1:6379> smembers myset2 1) "a"
5, Sorted Set data type (zset, ordered set)
Overview: 1. Ordered collection. The element type is String. The element is unique and cannot be repeated.
2. 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.
Application scope:
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.
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 (integer) 2 127.0.0.1:6379> zrange myzset 0 -1 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> zrange myzset 0 -1 withscores #0 represents the first member, - 1 represents the last member. The WithCores option means that the result of fan hi contains each member and score, 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 three (integer) 2 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> zrange myzset 0 -1 1) "three" 127.0.0.1:6379> zscore myzset three # Gets the score of the member three. The return value is of string type "3" 127.0.0.1:6379> zscore myzset two #nil is returned because the member two does not exist (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 the initial score is 0, increase the score of the member one by 2, and return the updated score of the member "2" 127.0.0.1:6379> zincrby myzset -1 one # One already exists. Increase the score of member one by - 1 and return the updated score of the member "1"
ZRANGEBYSCORE/ZREMRANGEBYRANK/ZREMRANGEBYSCORE
127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 7) "four" 8) "4" 127.0.0.1:6379> zrangebyscore myzset 1 2 #Get members with score 1 < = score < = 2 1) "one" 2) "two" 127.0.0.1:6379> zrangebyscore myzset (1 2 #Get members with a score of 1 < score < = 2 1) "two" 127.0.0.1:6379> zrangebyscore myzset -inf +inf limit 2 3 1) "three" 2) "four" # -Inf indicates the first member (the one with the lowest index value, i.e. 0), + inf indicates the last member (the one with the highest location index value), the parameters after limit are used to limit the value of the returned member, and 2 indicates starting from the member with the location index equal to 2 and going to the next three members 127.0.0.1:6379> zrangebyscore myzset 1 4 limit 1 2 1) "two" 2) "three" #Start with index value 1, and start with the last two digits of index value 2 127.0.0.1:6379> zremrangebyscore myzset 1 2 #Delete members with score 1 < = score < = 2 and return the number of deleted members (integer) 2 127.0.0.1:6379> zrange myzset 0 -1 1) "three" 2) "four" 127.0.0.1:6379> zremrangebyrank myzset 0 1 #Delete members with index value 0 < = rank < 1 and return the number of deleted members (integer) 2 127.0.0.1:6379> zrange myzset 0 -1 (empty list or set)
ZREVRANGE/ZREVRANGEBYSCORE/ZREVRANK
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 #Sort from top to bottom and de index members with values from 1 to 3 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> zrevrank myzset one #Sort from high to low, and take the index value of the one member (integer) 3 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 high to low 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 is equivalent to the option in zrangebycore. Knowledge is calculated and obtained in the reverse order when calculating the location 1) "three" 2) "two" 127.0.0.1:6379> zrevrangebyscore myzset +inf -inf limit 1 3 1) "three" 2) "two" 3) "one"
summary
① list query
lrange key start stop
lrem key values numbers
lpush key values
linsert key before|after value
② String string
Get (single get)
mget (batch acquisition)
③ Hash hash type
The hget query specifies the value of the specified field of the hash hash type
hmget batch specifies the query fields and corresponding values of hash hash type
hgetall queries all fields and values of the specified hash hash type
hkeys query refers to the field name of hash hash type
The hvals query specifies the value of the hash hash type
④ Set unordered set
smembers key
⑤ zset ordered set
zrange key start stop (0 -1)