This is the 14th day of my participation in the August More Text Challenge
1. Operations on string types
Set Sets the KV, validity period, and check whether the key exists
setThe key value [ex seconds] | / px milliseconds (nx) | [xx]When setting kv, you can also set the validity period and determine whether the key exists
# ex and px should not be written at the same time
# nx indicates that the operation is performed if the key does not exist
# xx indicates that the operation is performed if the key exists
# sample127.0.0.1:6379 >setThe name zhangsan OK 127.0.0.1:6379 >setName Zhangsan EX 100 OK 127.0.0.1:6379> TTL name (integer6379 > 78 127.0.0.1) :setName lisi nx (nil) 127.0.0.1:6379> get name"zhangsan"
Copy the code
Mset input multiple kV at one time
mset key1 value1 key2 value2......
Enter multiple key-values at once
# sample
127.0.0.1:6379> mset x 1 y 2 z 3
OK
127.0.0.1:6379> keys *
1) "y"
2) "z"
3) "x"
Copy the code
Setrange Changes the offset byte value to value
setrange key offset value
# change the offset byte of the string to value
If the offset is greater than the length of the character, the middle character is automatically supplemented by 0x00
# sample127.0.0.1:6379 >setName Zhangsan OK 127.0.0.1:6379> setrange name 5 *** (integer) 8
127.0.0.1:6379> get name
"zhang***"
Copy the code
Append appends the string to the value of key
append key value
# append the value string to the value of key
# sample127.0.0.1:6379 >setName Zhangsan OK 127.0.0.1:6379> Append name"@126.com"
(integer) 16
127.0.0.1:6379> get name
"[email protected]"
Copy the code
Getrange obtains part of the key value
getrange key start stop
Get a portion of the key value
# start indicates the starting position
# stop indicates the end position, which can be negative, indicating the beginning of the last count
# start>length Empty string
# stop>length is cut to the end
# sample127.0.0.1:6379 >set title "hello world"
OK
127.0.0.1:6379> getrange title 6 11
"world"127.0.0.1:6379> getrange title 0-7"hello"
Copy the code
Getset sets the new value to return the old value
getset key newvalue
Set the new value and return the old value
# sample127.0.0.1:6379 >setLogin on OK 127.0.0.1:6379> get login"on"127.0.0.1:6379 > getset login off"on"127.0.0.1:6379 > get login"off"
Copy the code
Incr /decr Specifies the value of the key plus/minus 1
incr/decr key
Add/subtract key by 1
If # key does not exist, add and subtract 1 automatically
# key is invalid if the value is a string
# sample127.0.0.1:6379 >setNum 100 OK 127.0.0.1:6379> incr num (integer) 101
127.0.0.1:6379> decr num
(integer) 100
Copy the code
Incrby /decrby Specifies the key value plus/minus number
incrby/decrby key number
# specify the key value plus or minus the number size
# sample127.0.0.1:6379 >setNum 100 OK 127.0.0.1:6379> incrby num 50 (integer) 150
127.0.0.1:6379> decrby num 100
(integer) 50
Copy the code
Incrbyfloat Specifies the key value plus the floating point number
incrbyfloat key floatnumber
# Specifies the value of key plus a floating point number
# sample127.0.0.1:6379 >setNum 10 OK 127.0.0.1:6379> incrByfloat num 0.5"10.5"127.0.0.1:6379 > incrbyfloat num - 1.5"9"
Copy the code
Setbit Sets the value on the binary bit
setbit key offset value
Set offset to the value of the binary bit
# return the old value of the transpose
# If offset is too large, 0 will be filled in between
The maximum value of offset is 2^32-1, that is, 512 MB
# sample127.0.0.1:6379 >setLetter A OK 127.0.0.1:6379> setbit letter 2 1 (integer) 0
127.0.0.1:6379> get letter
"a"
0100 0001(65) 0110 0001(97
Copy the code
Getbit Gets the value of the binary bit
getbit key offset
Get the value of the corresponding bit of binary offset
# sample127.0.0.1:6379 >setLetter A OK 127.0.0.1:6379> getbitinteger) 0
127.0.0.1:6379> getbit letter 1
(integer) 1
127.0.0.1:6379> getbit letter 7
(integer1)Copy the code
Bitop operates logically on multiple keys
bitop operation destkey key1 [key2 ......]
Key1 key2 keyN = destkey
# operation has AND, OR, NOT, XOR
# sample
127.0.0.1:6379> setbit lower 2 1
(integer) 0
127.0.0.1:6379> setbit upper 2 0
(integer) 0 127.0.0.1:6379 >setLetter A OK 127.0.0.1:6379> bitop or letter lower (integer) 1
127.0.0.1:6379> get letter
"a"
Copy the code
2. Operations on linked list types
Lpush /rpush adds a member to the start/end of the list
lpush/rpush key value
# Add a member to the beginning/end of the list, return the number of list members
# sample
127.0.0.1:6379> lpush letters A
(integer) 1
127.0.0.1:6379> rpush letters B
(integer) 2
127.0.0.1:6379> rpush letters C
(integer) 3
127.0.0.1:6379> rpush letters D
(integer4)Copy the code
Lrange gets the list members
lrange key start stop
Return the members of the [start,stop] range in the list
# rule: Left numbers start from 0, right numbers start from -1
# sample127.0.0.1:6379> Lrange Letters 0-1 1)"A"
2) "B"
3) "C"
4) "D"127.0.0.1:6379> Lrange Letters 12 1)"B"
2) "C"
Copy the code
Lpop/rPOP pops the head/tail member of the linked list
lpop/rpop key
Popup the first/last member of the list
# sample127.0.0.1:6379> Lrange Letters 0-1 1)"A"
2) "B"
3) "C"
4) "D"127.0.0.1:6379 > lpop letters"A"127.0.0.1:6379 > rpop letters"D"127.0.0.1:6379> Lrange Letters 0-1 1)"B"
2) "C"
Copy the code
Lrem deletes linked list members
lrem key count value
Delete value from key list
# delete the absolute value of count
# count>0 is removed from the table header
Drop # count<0 from the end of the table
# sample127.0.0.1:6379> rpush letters A B C D A B C D A B C Dinteger) 12
127.0.0.1:6379> lrem letters 2 A
(integer) 2 127.0.1:6379 > Lrange Letters 0-1 1)"B"
2) "C"
3) "D"
4) "B"
5) "C"
6) "D"
7) "A"
8) "B"
9) "C"
10) "D"
127.0.0.1:6379> lrem letters -3 D
(integer) 3 127.0.1:6379 > Lrange Letters 0-1 1)"B"
2) "C"
3) "B"
4) "C"
5) "A"
6) "B"
7) "C"
Copy the code
Lindex gets the value of the linked list index
lindex key index
Select * from index
# sample
127.0.0.1:6379> rpush letters A B C D
(integer) 4
127.0.0.1:6379> lindex letters 1
"B"127.0.0.1:6379 > lindex letters. 2"C"
Copy the code
Llen key Obtains the number of linked list members
BASHllen key
Get the number of linked list members
# sample
127.0.0.1:6379> rpush letters A B C D
(integer) 4
127.0.0.1:6379> llen letters
(integer4)Copy the code
Linsert inserts a member at the specified position in the list
linsert key after|before search value
# in the table to find the key chain "search", and insert the value before | after the search value
If no value is found, no value is inserted
# If a search is found, the command ends, so multiple values are not inserted
# sample
127.0.0.1:6379> rpush id 1 3 5 7
(integer) 4
127.0.0.1:6379> linsert id before 3 2
(integer) 5 127.0.0.1:6379> lrange id 0-1 1)"1"
2) "2"
3) "3"
4) "5"
5) "Seven"
127.0.0.1:6379> linsert id after 5 6
(integer) 6 127.0.0.1:6379> lrange id 0-1 1)"1"
2) "2"
3) "3"
4) "5"
5) "6"
6) "Seven"
Copy the code
Blpop/BRPOP is always waiting to pop the head/tail members
blpop/brpop key timeout
Wait for the first/last member of the key to pop up
# Timeout indicates the waiting Timeout time
If timeout is 0, wait
# Application S scenario: long polling Ajax, online chat, can be used
# sample
# first terminal operation:
127.0.0.1:6379> brpop chat 0
1) "chat"
2) "hello"(40.97 s)# second terminal operation:127.0.0.1:6379 > rpush chat"hello"
(integer1)Copy the code
3. Operations on unordered sets
Sadd adds members to the collection
sadd key value1 value2 ...
Add members to the set key
# only one member will be added when adding the same member.
# sample
127.0.0.1:6379> sadd names zhangsan lisi
(integer) 2 127.0.0.1:6379> sadd names wangwu wangwuinteger1)Copy the code
Srem deletes a collection member
srem key value1 value2 ...
Value1 value2... Members of the
# return the number of deleted members (excluding non-existent members)
# sample
127.0.0.1:6379> sadd names zhangsan lisi wangwu
(integer) 3
127.0.0.1:6379> srem names zhangsan lisi
(integer) 2
127.0.0.1:6379> smembers names
1) "wangwu"
Copy the code
Spop randomly removes a member of the collection
spop key
Delete a member of set key randomly
# Application scenario: The winner of the lottery has already been excluded and cannot be selected again
# sampleSadd letters A B C D E F (integer) 6
127.0.0.1:6379> spop letters
"A"127.0.0.1:6379 > spop letters"F"127.0.0.1:6379 > spop letters"B"127.0.0.1:6379 > spop letters"D"
Copy the code
Srandmember Randomly retrieves collection members
srandmember key [count]
Select count (1) from key
# sample127.0.0.1:6379 > srandmember letters"C"
127.0.0.1:6379> srandmember letters 2
1) "E"
2) "B"
127.0.0.1:6379> srandmember letters 3
1) "D"
2) "C"
3) "E"
Copy the code
Smembers Gets all the members of the collection
smembers key
Return all members of the collection
The order in which values are returned is not necessarily the order in which members are added.
# sample
127.0.0.1:6379> sadd names zhangsan lisi wangwu
(integer) 3
127.0.0.1:6379> smembers names
1) "lisi"
2) "wangwu"
3) "zhangsan"
Copy the code
Sismember determines whether a member exists in the set
sismember key value
Select * from key where value = 1
# sample
127.0.0.1:6379> sadd names zhangsan lisi wangwu
(integer) 3
127.0.0.1:6379> sismember names lisi
(integer1 127.0.0.1:6379> sismember names zhaoliu (integer) 0
Copy the code
Scard gets the number of collection members
scard key
Get the number of members of the set
# sample127.0.1:6379 > sadd letters A B C Dinteger) 4
127.0.0.1:6379> sadd letters E F
(integer) 2
127.0.0.1:6379> scard letters
(integer6)Copy the code
Smove moves a member of a collection to another collection
smove <source> <dest> value
Select * from 'source' where 'value' = 'dest'
# sample
127.0.0.1:6379> sadd letters A B C
(integer) 3 127.0.0.1:6379> sadd num 12 3 (integer) 3
127.0.0.1:6379> smove letters num A
(integer) 1
127.0.0.1:6379> smembers letters
1) "C"
2) "B"127.0.0.1:6379 > smembers num 1)"3"
2) "1"
3) "A"
4) "2"
Copy the code
Sunion gets the union of multiple collections
sunion key1 key2 ...
Get the union of multiple sets
# sample
127.0.0.1:6379> sadd zhangsan A E G
(integer) 3
127.0.0.1:6379> sadd lisi B E F
(integer) 3
127.0.0.1:6379> sadd wangwu C D E
(integer) 3
127.0.0.1:6379> sunion zhangsan lisi wangwu
1) "B"
2) "G"
3) "D"
4) "C"
5) "E"
6) "F"
7) "A"
Copy the code
Sdiff gets the difference set of multiple sets
sdiff key1 key2 ...
Get key1 and key2... The difference between the set
# the key1 key2 -... (Key1 has members that no other set has)
# sample
127.0.0.1:6379> sadd zhangsan A B C
(integer) 3
127.0.0.1:6379> sadd lisi B D E
(integer) 3 127.0.0.1:6379> sadd C E F (integer) 3
127.0.0.1:6379> sdiff zhangsan lisi wangwu
1) "A"
Copy the code
Sinterstore takes the intersection of multiple collections and stores them
sinterstore dest key1 key2 ...
Key1 key2... Set, and assigned to dest
# sample127.0.0.1:6379> sadd Zhangsan A C Dinteger) 3
127.0.0.1:6379> sadd lisi B D E
(integer) 3
127.0.0.1:6379> sadd wangwu D E G
(integer) 3
127.0.0.1:6379> sinterstore class zhangsan lisi wangwu
(integer) 1
127.0.0.1:6379> smembers class
1) "D"
Copy the code
4. Operations on ordered sets
Zadd adds members to an ordered collection
zadd key score1 key2 score2 key2 ...
Add a member to the ordered set key
# sample
127.0.0.1:6379> zadd ages 28 zhangsan 24 lisi 26 wangwu
(integer) 0 127.0.0.1:6379> zrange ages 0-1 1)"lisi"
2) "wangwu"
3) "zhangsan"
Copy the code
Zrange selects members by rank
zrange key start stop [WITHSCORES]
Select * from start,stop; select * from start
Withscores print out score as well
# sample127.0.0.1:6379> zrange ages 0-1 withscores 1)"lisi"
2) "24"
3) "wangwu"
4) "26"
5) "zhangsan"
6) "28"
Copy the code
Zrangebyscore takes members byscore
zrangebyscore key min max [withscores] limit offset N
Select N members from [min, Max] where score is in [min, Max], skip offset, select N members by score
# sample
127.0.0.1:6379> zadd ages 28 zhangsan 24 lisi 26 wangwu
(integer) 3
127.0.0.1:6379> zrangebyscore ages 25 30
1) "wangwu"
2) "zhangsan"
127.0.0.1:6379> zrangebyscore ages 25 30 limit 1 1
1) "zhangsan"
Copy the code
Zscore gets the score of a specified member
ZSCORE key member
Get the score of the specified member
# sample
127.0.0.1:6379> zadd height 175 zhangsan 167 lisi 185 wangwu
(integer) 3
127.0.0.1:6379> zscore height lisi
"167"
Copy the code
Zcount counts the number of fractional interval members
zcount key min max
# Count the number of members in [min, Max]
# sample
127.0.0.1:6379> zadd height 175 zhangsan 167 lisi 185 wangwu
(integer) 3
127.0.0.1:6379> zcount height 170 180
(integer1)Copy the code
Zrank/zRevRank gets the ranking of members in ascending/descending order
zrank/zrevrank key member
Select * from 'member' where 'member' = '0'
# sample
127.0.0.1:6379> zadd ages 28 zhangsan 24 lisi 26 wangwu
(integer) 0 127.0.0.1:6379> zrange ages 0-1 1)"lisi"
2) "wangwu"
3) "zhangsan"
127.0.0.1:6379> zrank ages zhangsan
(integer) 2
127.0.0.1:6379> zrevrank ages zhangsan
(integer) 0
Copy the code
Zrem deletes ordered set members
zrem key value1 value2 ..
Delete a member from the collection
# sample
127.0.0.1:6379> zrem ages wangwu
(integer) 1 127.0.0.1:6379> Zrange ages 0-1 1)"lisi"
2) "zhangsan"
Copy the code
Zremrangebyrank deletes members byrank
zremrangebyrank key start end
Select * from 'start' where 'end' is located
# sample
127.0.0.1:6379> zadd height 175 zhangsan 167 lisi 185 wangwu 178 zhaoliu
(integer) 1
127.0.0.1:6379> zremrangebyrank height 0 1
(integer) 2
127.0.0.1:6379> zrange height 0 -1
1) "zhaoliu"
2) "wangwu"
Copy the code
Zremrangebyscore deletes members byscore
zremrangebyscore key min max
Select * from socre where score is between min and Max
# sample
127.0.0.1:6379> zadd height 175 zhangsan 167 lisi 185 wangwu 178 zhaoliu
(integer) 2
127.0.0.1:6379> zremrangebyscore height 170 180
(integer) 2
127.0.0.1:6379> zrange height 0 -1
1) "lisi"
2) "wangwu"
Copy the code
Zinterstore calculates the intersection again
zinterstore destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]Key1, key2... The intersection of key1, key2... Weight1, weight2...
# polymerization method with: sum | min | Max
The result of the aggregation is stored in the Destination collection
# sample
127.0.0.1:6379> zadd zhangsan 5 iphone6s 7 galaxyS7 6 huaweiP9
(integer) 3
127.0.0.1:6379> zadd lisi 3 iphone6s 9 galaxyS7 4 huaweiP9 2 HTC10
(integer) 4
127.0.0.1:6379> zinterstore result 2 zhangsan lisi
(integer) 3 127.0.0.1:6379> zrange result 0-1 withscores 1)"iphone6s"
2) "8"
3) "huaweiP9"
4) "10"
5) "galaxyS7"
6) "16"
127.0.0.1:6379> zinterstore result 2 zhangsan lisi aggregate max
(integer) 3 127.0.0.1:6379> zrange result 0-1 withscores 1)"iphone6s"
2) "5"
3) "huaweiP9"
4) "6"
5) "galaxyS7"
6) "9"
Copy the code
Zunionstore computes the set
zunionstore destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]Key1, key2... The union of key1, key2... Weight1, weight2...
# polymerization method with: sum | min | Max
The result of the aggregation is stored in the Destination collection
# sample
127.0.0.1:6379> zadd zhangsan 4 iphone6s 6 huaweiP9 8 xiaomi5
(integer) 3
127.0.0.1:6379> zadd lisi 2 iphone6s 8 galaxS7 5 meizu6
(integer) 3
127.0.0.1:6379> zunionstore result 2 zhangsan lisi
(integer) 5 127.0.0.1:6379> zrange result 0-1 withscores 1)"meizu6"
2) "5"
3) "huaweiP9"
4) "6"
5) "iphone6s"
6) "6"
7) "galaxS7"
8) "8"
9) "xiaomi5"
10) "8"
127.0.0.1:6379> zunionstore result 2 zhangsan lisi aggregate max
(integer) 5 127.0.0.1:6379> zrange result 0-1 withscores 1)"iphone6s"
2) "4"
3) "meizu6"
4) "5"
5) "huaweiP9"
6) "6"
7) "galaxS7"
8) "8"
9) "xiaomi5"
10) "8"
Copy the code
5, the hash type of data operation
Hset Sets the value of the hash field
hset key field value
Step = field; step = field; step = field
# Note: If there is no field, add it directly, if there is, overwrite the value of the original field
# sample
127.0.0.1:6379> hset user name zhangsan
(integer) 1
127.0.0.1:6379> hset user age 25
(integer) 1
127.0.0.1:6379> hset user gender male
(integer1)Copy the code
Hmset sets the hash value of multiple field fields
hmset key field1 value1 [field2 value2 field3 value3 ......fieldn valuen]
# set multiple fields and corresponding values at once
# sample
127.0.0.1:6379> hmset user name lisi age 26 gender male
OK
Copy the code
Hget gets the value of the field
hget key field
Get the value of the field
# sample
127.0.0.1:6379> hmset user name lisi age 26 gender male
OK
127.0.0.1:6379> hget user age
26"Copy the code
Hmget gets the values of multiple field fields
hget key field
Get the values of multiple fields
# sample
127.0.0.1:6379> hmset user name lisi age 26 gender male
OK
127.0.0.1:6379> hmget user name age
1) "lisi"
2) "26"
Copy the code
Hgetall retrieves all field fields and values
hgetall key
Get all fields and values of the hash key
# sample
127.0.0.1:6379> hmset user name lisi age 26 gender male
127.0.0.1:6379> hgetall user
1) "name"
2) "lisi"
3) "age"
4) "26"
5) "gender"
6) "male"
Copy the code
Hlen gets the number of fields
hlen key
Get the number of fields
# sample
127.0.0.1:6379> hmset user name lisi age 26 gender male
127.0.0.1:6379> hlen user
(integer) 3
Copy the code
Hdel Deletes the field
hdel key field
# delete field from key
# sample
127.0.0.1:6379> hmset user name lisi age 26 gender male
127.0.0.1:6379> hdel user age
(integer) 1
127.0.0.1:6379> hgetall user
1) "name"
2) "lisi"
3) "gender"
4) "male"
Copy the code
Hexists Determines whether the field exists
hexists key field
# check whether there is a field in the key
# sample
127.0.0.1:6379> hmset user name lisi age 26 gender male
OK
127.0.0.1:6379> hexists user age
(integer) 1
127.0.0.1:6379> hexists user height
(integer) 0
Copy the code
Hincrby adds an integer to the value of the field
hincrby key field value
# add the value of the field field in key to the integer value
# sample
127.0.0.1:6379> hmset user name zhangsan height 158
OK
127.0.0.1:6379> hincrby user height 2
(integer) 160
Copy the code
Hincrbyfloat adds a floating point number to the value of the field
hincrbyfloat key field value
# add the value of the field field in key to the floating point value
# sample
127.0.0.1:6379> hmset user name zhangsan height 158
OK
127.0.0.1:6379> hincrbyfloat user height 5.5
"165.5"
Copy the code
Hkeys gets the names of all fields
hkeys key
Get all fields in the key
# sample
127.0.0.1:6379> hmset user name zhangsan age 25 gender male
OK
127.0.0.1:6379> hkeys user
1) "name"
2) "age"
3) "gender"
Copy the code
Kvals retrieves the values of all fields
kvals key
# return all values in key
# sample
127.0.0.1:6379> hmset user name zhangsan age 25 gender male
OK
127.0.0.1:6379> hvals user
1) "zhangsan"
2) "25"
3) "male"
Copy the code