1. What are the supported data types
The data types supported by edis are very rich, including String, list, set, hash, zset, hyperLogLog, geo
Second, describe each data type in detail
1. String type
A string in redis is a sequence of bytes. Strings can store 3 types of data
byte string
integer
floating point number
Operations on Integers and Floating Points
Command INCR to increment the stored value by 1
127.0.0.1:6379> set s_incr 0
OK
127.0.0.1:6379> incr s_incr
(integer) 1
127.0.0.1:6379> incr s_incr
(integer) 2
Command DECR , decrement the stored value by 1
127.0.0.1:6379> set s_decr 10
OK
127.0.0.1:6379> decr s_decr
(integer) 9
127.0.0.1:6379> decr s_decr
(integer) 8
The command INCRBY adds an integer to the stored value
127.0.0.1:6379> set s_incrby 0
OK
127.0.0.1:6379> incrby s_incrby 5
(integer) 5
127.0.0.1:6379> incrby s_incrby 2
(integer) 7
Command DECRBY to subtract an integer from the stored value
127.0.0.1:6379> set s_decrby 10
OK
127.0.0.1:6379> decrby s_decrby 2
(integer) 8
127.0.0.1:6379> decrby s_decrby 3
(integer) 5
The command INCRBYFLOAT adds a floating point number to the stored value (used in version 2.6 and above)
127.0.0.1:6379> set s_incrbyfloat 2
OK
127.0.0.1:6379> incrbyfloat s_incrbyfloat 1.5
"3.5"
127.0.0.1:6379> incrbyfloat s_incrbyfloat 0.6
"4.1"
operations on strings
The command APPEND , appends the value value to the end of the value currently stored for the given key
127.0.0.1:6379> set s_append leo
OK
127.0.0.1:6379> append s_append _111
(integer) 7
127.0.0.1:6379> get s_append
"leo_111"
Command GETRANGE to get the substring composed of all characters in the range from start to end, including start and end (this command is renamed from the SUBSTR command, and the GETRANGE command is recommended for versions 2.6 and above)
127.0.0.1:6379> getrange s_append 1 3
"eo_"
command SETRANGE to set the substring starting at the start offset to the given value
127.0.0.1:6379> setrange s_append 4 222
(integer) 7
127.0.0.1:6379> get s_append
"leo_222"
Command STRLEN , get the length of the value stored by the key
127.0.0.1:6379> strlen s_append
(integer) 7
Command GET , get the value saved by the key
127.0.0.1:6379> get s_append
"leo_222"
Command GETSET , sets a value of character type and returns its previous value
127.0.0.1:6379> getset s_append leo_333
"leo_222"
127.0.0.1:6379> getset s_append1 leo_333(设置一个不存在的key会返回nil)
(nil)
127.0.0.1:6379> get s_append1
"leo_333"
The command MGET returns multiple values corresponding to the keys according to the multiple keys provided
127.0.0.1:6379> set s_append1 leo_444
OK
127.0.0.1:6379> mget s_append s_append1
1) "leo_333"
2) "leo_444"
Command MSET , set multiple key-value pairs
127.0.0.1:6379> mset s_append2 leo_555 s_append3 leo_666
OK
127.0.0.1:6379> mget s_append2 s_append3
1) "leo_555"
2) "leo_666"
Command MSETNX , set multiple key-value pairs, if the key does not exist, the setting is successful (if one of the multiple key-value pairs does not meet the requirements, all the settings are unsuccessful)
127.0.0.1:6379> msetnx s_append3 leo_777 s_append4 leo_888
(integer) 0
127.0.0.1:6379> mget s_append3 s_append4
1) "leo_666"
2) (nil)
127.0.0.1:6379> msetnx s_append5 leo_777 s_append4 leo_888
(integer) 1
127.0.0.1:6379> mget s_append5 s_append4
1) "leo_777"
2) "leo_888"
The command PSETEX , sets a value and expires in milliseconds for a key
127.0.0.1:6379> psetex s_ex 5000 test
OK
127.0.0.1:6379> get s_ex
"test"
127.0.0.1:6379> get s_ex
(nil)
Command SET , set a string value to a key, command format set key value [EX seconds] [PX milliseconds] [NX|XX]
127.0.0.1:6379> set s_set test1 EX 20
OK
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
"test1"
127.0.0.1:6379> get s_set
(nil)
127.0.0.1:6379> set s_setml test EX 10 PX 5000(PX和EX参数不能同时设置)
(error) ERR syntax error
127.0.0.1:6379> set s_setml test PX 5000(这里手慢了一点,不过确实生效了)
OK
127.0.0.1:6379> get s_setml
(nil)
127.0.0.1:6379> set s_nx test XX(该键如果存在的时候才会设置成功)
(nil)
127.0.0.1:6379> get s_nx
(nil)
127.0.0.1:6379> set s_nx test NX(该键如果不存在的时候才会设置成功)
OK
127.0.0.1:6379> set s_nx test NX
(nil)
Command SETEX , set value and expiration time to key
127.0.0.1:6379> setex s_ex 5 aaa
OK
127.0.0.1:6379> get s_ex
"aaa"
127.0.0.1:6379> get s_ex
(nil)
Command SETNX , set the value to the key, only if the key does not exist can be successful
127.0.0.1:6379> setnx s_nx test
(integer) 0
127.0.0.1:6379> setnx s_nx test
(integer) 0
127.0.0.1:6379> get s_nx
"test"
2. List type
A list structure in redis can store multiple strings in order and can be repeated.
Command RPUSH to push the given value to the right end of the list
127.0.0.1:6379> rpush l_rpush leo
(integer) 1
127.0.0.1:6379> rpush l_rpush leo1
(integer) 2
127.0.0.1:6379> rpush l_rpush leo
(integer) 3
Command LRANGE to get all values of a list over a given range
127.0.0.1:6379> lrange l_rpush 0 -1(0 -1代表获取列表中的所有值)
1) "leo"
2) "leo1"
3) "leo"
127.0.0.1:6379> lrange l_rpush 0 1
1) "leo"
2) "leo1"
Command LINDEX to get a single element of a list at a given position
127.0.0.1:6379> lindex l_rpush 1
"leo1"
127.0.0.1:6379> lindex l_rpush 2
"leo"
The command LPOP , pops a value from the left end of the list and returns the value that was popped
127.0.0.1:6379> lpop l_rpush
"leo"
127.0.0.1:6379> lrange l_rpush 0 -1
1) "leo1"
2) "leo"
The command BLPOP pops a value from the left end of the list and returns the value that was popped, blocking if the list is empty until there is a value in the list
127.0.0.1:6379> blpop l_bl 20(20为等待超时时间,如果超时会返回nil)
(nil)
execute first
127.0.0.1:6379> blpop b_bl 20
Then reopen a command line window and execute
127.0.0.1:6379> lpush b_bl 1
(integer) 1
You can see that the BLPOP command returns the data correctly
127.0.0.1:6379> blpop b_bl 20
1) "b_bl"
2) "1"
(3.63s)
The command BLPOP pops a value from the right end of the list and returns the value that was popped, blocking if the list is empty until there is a value in the list
The command BRPOPLPUSH pops the rightmost value from the original list and pushes it to the leftmost value of another list
127.0.0.1:6379> lpush l_source 1 2 3
(integer) 3
127.0.0.1:6379> lpush l_dest 4 5 6
(integer) 3
127.0.0.1:6379> brpoplpush l_source l_dest 20
"1"
127.0.0.1:6379> lrange l_dest 0 -1
1) "1"
2) "6"
3) "5"
4) "4"
Command LINDEX to get this element from an index in a list
127.0.0.1:6379> LINDex l_dest 2
"5"
Command LINSERT to insert an element before or after another element in a list
127.0.0.1:6379> linsert l_dest before 6 2
(integer) 5
127.0.0.1:6379> lrange l_dest 0 -1
1) "1"
2) "2"
3) "6"
4) "5"
5) "4"
Command LLEN to get the length of the list
127.0.0.1:6379> llen l_dest
(integer) 5
Command LPOP to pop an element from the left of the list
127.0.0.1:6379> lpop l_dest
"1"
Command LPUSH to push an element from the left of the list
127.0.0.1:6379> lpush l_dest 0
(integer) 5
127.0.0.1:6379> lrange l_dest 0 -1
1) "0"
2) "2"
3) "6"
4) "5"
5) "4"
The command LPUSHX pushes an element from the left if the key exists
127.0.0.1:6379> lpushx l_dest1 123
(integer) 0
127.0.0.1:6379> lpushx l_dest 123
(integer) 6
127.0.0.1:6379> lrange l_dest 0 -1
1) "123"
2) "0"
3) "2"
4) "6"
5) "5"
6) "4"
The command LREM removes a given number of elements from the left of the list
127.0.0.1:6379> lrange l_dest 0 -1
1) "4"
2) "0"
3) "123"
4) "0"
5) "2"
6) "6"
7) "5"
8) "4"
127.0.0.1:6379> lrange l_dest 0 -1(最左边的0被移除)
1) "4"
2) "123"
3) "0"
4) "2"
5) "6"
6) "5"
7) "4"
127.0.0.1:6379> lpush l_dest 4
(integer) 8
127.0.0.1:6379> lrem l_dest 2 4(移除了左边的两个4)
(integer) 2
127.0.0.1:6379> lrange l_dest 0 -1
1) "123"
2) "0"
3) "2"
4) "6"
5) "5"
6) "4"
Command LSET , set the value of the element at the first index of a list
127.0.0.1:6379> lset l_dest 1 000
OK
127.0.0.1:6379> lrange l_dest 0 -1
1) "123"
2) "000"
3) "2"
4) "6"
5) "5"
6) "4"
Command LTRIM , trim list
127.0.0.1:6379> lrange l_dest 0 -1
1) "123"
2) "000"
3) "2"
4) "6"
5) "5"
6) "4"
127.0.0.1:6379> ltrim l_dest 1 2(只保留index1到index2之间的元素)
OK
127.0.0.1:6379> lrange l_dest 0 -1
1) "000"
2) "2"
Command RPOP to pop elements from the right
127.0.0.1:6379> lrange l_dest 0 -1
1) "000"
2) "2"
127.0.0.1:6379> rpop l_dest
"2"
127.0.0.1:6379> lrange l_dest 0 -1
1) "000"
The command RPOPLPUSH pops elements from the right side of the original list and adds them to the left side of the given list
127.0.0.1:6379> rpoplpush l_dest l_source
"000"
127.0.0.1:6379> lrange l_dest 0 -1
(empty list or set)
127.0.0.1:6379> lrange l_source 0 -1
1) "000"
2) "3"
3) "2"
Command RPUSHX to push elements from the right if the list exists
127.0.0.1:6379> rpushx l_dest1 haha
(integer) 0
127.0.0.1:6379> rpushx l_source haha
(integer) 4