Redis basic command

Time:2021-4-14

Review the old and learn the new. This paper mainly uses some simple examples to briefly describe the redis related data types and corresponding operation commands, which are only for learning and sharing. If there are any shortcomings, please correct them.

Redis data type

Redis supports the following five data types:

  1. String: string type is the most basic data type of redis. A key can store 512MB at most.
  2. Hash: hash is a set of key value (key = > value) pairs. It is a mapping table of field and value of string type. It is especially suitable for storing objects.
  3. List: a list is a simple list of strings, sorted in order of insertion. You can add an element to the top (left) or bottom (right) of the list
  4. Set: set is an unordered set of string type.
  5. Zset (sorted set): like set, Zset is also a set of string type elements, and duplicate members are not allowed. The difference is that each element is associated with a score of double type. Redis is to sort the members of a set from small to large by fractions. The members of Zset are unique, but the score can be repeated.

Redis common commands

The keys command is used to query and display the keys that meet the conditions, as follows:

 1 127.0.0.1:6379> Keys *
 2 1) "K2"
 3 2) "K3"
 4 3) "k1"
 5 4) "K4"
 6 5) "K5"
 7 127.0.0.1:6379> Keys *2
 8 1) "K2"
 9 127.0.0.1:6379> Keys K*
10 1) "K2"
11 2) "K3"
12 3) "K4"
13 4) "K5"

Randomkey is used to display a key randomly, as shown below:

 1 127.0.0.1:6379> RANDOMKEY
 2 "K3"
 3 127.0.0.1:6379> RANDOMKEY
 4 "K2"
 5 127.0.0.1:6379> RANDOMKEY
 6 "K2"
 7 127.0.0.1:6379> RANDOMKEY
 8 "K3"
 9 127.0.0.1:6379> RANDOMKEY
10 "K5"

The type command is used to display the data type of the value, as follows:

1 127.0.0.1:6379> type K5
2 string
3 127.0.0.1:6379> type K2
4 string

Del delete command is used to delete a key and return the number of deleted keys, as shown below:

1 127.0.0.1:6379> DEL K3
2 (integer) 1

The exists command is used to judge whether it exists or not. 0 means that it does not exist, and greater than 0 means that it exists, as shown below:

1 127.0.0.1:6379> EXISTS K4 K5
2 (integer) 2
3 127.0.0.1:6379> EXISTS K3
4 (integer) 0

TTL command is used to query whether the current key is valid or not, – 1 means it is permanently valid, – 2 means it is invalid, and greater than 0 means it is invalid after how many seconds, as shown below:

1 127.0.0.1:6379> TTL K3
2 (integer) -2
3 127.0.0.1:6379> TTL K4
4 (integer) -1
5 127.0.0.1:6379> TTL K2
6 (integer) -1

Expiration is used to set the effective time of the key, that is, how many seconds it will be invalid, as follows:

1 127.0.0.1:6379> EXPIRE K4 10
2 (integer) 1
3 127.0.0.1:6379> TTL K4
4 (integer) 2
5 127.0.0.1:6379> TTL K4
6 (integer) -2

The persist command is used to cancel the valid time of setting, as follows:

1 127.0.0.1:6379> EXPIRE K5 10
2 (integer) 1
3 127.0.0.1:6379> PERSIST K5
4 (integer) 1
5 127.0.0.1:6379> TTL K5
6 (integer) -1

The flushall command is used to clear all the contents, as follows:

1 127.0.0.1:6379> FLUSHALL 
2 OK
3 127.0.0.1:6379> kEYS *
4 (empty array)

 

String type related commands

The set and get commands are used to set or read the value of string type, as follows:

1 127.0.0.1:6379> SET AGE 10
2 OK
3 127.0.0.1:6379> KEYS *
4 1) "K2"
5 2) "AGE"
6 3) "K5"
7 127.0.0.1:6379> GET AGE
8 "10"

GetSet command to read the old value and set the new value as follows:

1 127.0.0.1:6379> GETSET AGE 15 
2 "10"
3 127.0.0.1:6379> GET AGE
4 "15"

The getrange command is used to obtain the value of the length within the specified range, as follows:

 1 127.0.0.1:6379> GETRANGE K2 0 2
 2 "V2"
 3 127.0.0.1:6379> GETRANGE K2 0 0
 4 "V"
 5 127.0.0.1:6379> GETRANGE K2 0 -1
 6 "V2"
 7 127.0.0.1:6379> GETRANGE K2 0 -2
 8 "V"
 9 127.0.0.1:6379> GETRANGE K2 0 -3
10 "V"

The Mset and mget commands are used for batch setting and obtaining key values, as follows:

 1 127.0.0.1:6379> MSET NAME HEX AGE 20 SET MALE
 2 OK
 3 127.0.0.1:6379> kEYS *
 4 1) "AGE"
 5 2) "NAME"
 6 3) "SET"
 7 127.0.0.1:6379> MGET NAME AGE SET
 8 1) "HEX"
 9 2) "20"
10 3) "MALE"
11 127.0.0.1:6379> MGET NAME AGE
12 1) "HEX"
13 2) "20"

Setnx command. If it does not exist, it will be set. If it does exist, it will not be set. The number of affected items will be returned, as shown below:

1 127.0.0.1:6379> SETNX NAME LL
2 (integer) 0
3 127.0.0.1:6379> SETNX SEX FEMALE
4 (integer) 1
5 127.0.0.1:6379> kEYS *
6 1) "SEX"
7 2) "AGE"
8 3) "NAME"
9 4) "SET"

The setrange command is used to replace the string. The second parameter is the offset, starting from 0, and the third parameter is the new content to be replaced, as follows:

1 127.0.0.1:6379> SETRANGE NAME 2 AAAAAAAA
2 (integer) 10
3 127.0.0.1:6379> GET NAME
4 "HEAAAAAAAA"
5 127.0.0.1:6379> SETRANGE AGE 3 BBBBB
6 (integer) 8
7 127.0.0.1:6379> GET AGE
8 "20\x00BBBBB"

The incr and decr commands are mainly used to increment and decrement the value of a numeric class. If it cannot be converted to an integer type, an error will be reported, as follows:

 1 127.0.0.1:6379> SET SCORE 20
 2 OK
 3 127.0.0.1:6379> INCR SCORE
 4 (integer) 21
 5 127.0.0.1:6379> INCR SCORE
 6 (integer) 22
 7 127.0.0.1:6379> GET SCORE
 8 "22"
 9 127.0.0.1:6379> DECR SCORE 
10 (integer) 21
11 127.0.0.1:6379> DECR SCORE 
12 (integer) 20
13 127.0.0.1:6379> GET SCORE
14 "20"
15 127.0.0.1:6379> INCR NAME
16 (error) ERR value is not an integer or out of range

Incrby, decrby command, according to the specified step to increase, reduce operation. The second parameter is step, as follows:

 1 127.0.0.1:6379> INCRBY SCORE 5
 2 (integer) 25
 3 127.0.0.1:6379> INCRBY SCORE 5
 4 (integer) 30
 5 127.0.0.1:6379> GET SCORE
 6 "30"
 7 127.0.0.1:6379> DECRBY SCORE 3
 8 (integer) 27
 9 127.0.0.1:6379> DECRBY SCORE 3
10 (integer) 24

Incrbyfloat command, increment by floating-point number, as follows:

1 127.0.0.1:6379> INCRBYFLOAT SCORE 0.2
2 "24.2"
3 127.0.0.1:6379> INCRBYFLOAT SCORE 0.2
4 "24.4"
5 127.0.0.1:6379> GET SCORE
6 "24.4"

The append command is used to append content as follows:

1 127.0.0.1:6379> APPEND NAME ALANHSIANG
2 (integer) 20
3 127.0.0.1:6379> GET NAME
4 "HEAAAAAAAAALANHSIANG"

The strlen command is used to get the length of the value value, as follows:

1 127.0.0.1:6379> STRLEN NAME
2 (integer) 20

The object encoding command is used to obtain the object encoding type, as follows:

1 127.0.0.1:6379> OBJECT ENCODING  NAME
2 "raw"
3 127.0.0.1:6379> OBJECT ENCODING  AGE
4 "raw"
5 127.0.0.1:6379> OBJECT ENCODING  SCORE
6 "embstr"

List type related commands

Lpush, lrange command, first in, last out command, that is, insert from the head. Lrange command, used to display the content of the specified location. As follows:

1 127.0.0.1:6379> lpush fruit apple banana pear orange
2 (integer) 4
3 127.0.0.1:6379> lrange fruit 0 -1
4 1) "orange"
5 2) "pear"
6 3) "banana"
7 4) "apple"

Rpush command, first in first out command, that is, insert from the tail, as follows:

1 127.0.0.1:6379> rpush ruit apple banana pear orange
2 (integer) 4
3 127.0.0.1:6379> lrange ruit 0 -1
4 1) "apple"
5 2) "banana"
6 3) "pear"
7 4) "orange"

The lpop and rpop commands are used to pop up the elements in the list. After the pop-up, the elements in the list disappear. As follows:

1 127.0.0.1:6379> lpop ruit 
2 "apple"
3 127.0.0.1:6379> rpop ruit
4 "orange"
5 127.0.0.1:6379> lrange ruit 0 -1
6 1) "banana"
7 2) "pear"
8 127.0.0.1:6379> llen ruit
9 (integer) 2

Allen command, used to get the length of the list, that is, the number of elements.

1 127.0.0.1:6379> llen ruit
2 (integer) 2
3 127.0.0.1:6379> llen fruit
4 (integer) 4

The list element is allowed to repeat as follows:

 1 127.0.0.1:6379> lpush mylist l1 l2 l3 l1 l4 l5 l1 l6 l7 l8 l1
 2 (integer) 11
 3 127.0.0.1:6379> lrange mylist  0 -1
 4  1) "l1"
 5  2) "l8"
 6  3) "l7"
 7  4) "l6"
 8  5) "l1"
 9  6) "l5"
10  7) "l4"
11  8) "l1"
12  9) "l3"
13 10) "l2"
14 11) "l1"

Lrem command, to delete the elements in the list. The second parameter indicates the number, equal to 0 means to delete all qualified elements, and the third parameter indicates the content to be deleted, as follows:

 1 127.0.0.1:6379> lrem  mylist 0 l1
 2 (integer) 4
 3 127.0.0.1:6379> lrange mylist  0 -1
 4 1) "l8"
 5 2) "l7"
 6 3) "l6"
 7 4) "l5"
 8 5) "l4"
 9 6) "l3"
10 7) "l2"

Lrem command, the second parameter is greater than 0 to delete from the beginning to the end, delete the specified number of elements, less than 0 to delete from the end to the end, the absolute value is the number to delete. As follows:

 1 127.0.0.1:6379> lpush mylist2 l1 l2 l3 l1 l4 l5 l1 l6 l7 l8 l1
 2 (integer) 11
 3 127.0.0.1:6379> lrem mylist2 -1 l1
 4 (integer) 1
 5 127.0.0.1:6379> lrange mylist2 0 -1
 6  1) "l1"
 7  2) "l8"
 8  3) "l7"
 9  4) "l6"
10  5) "l1"
11  6) "l5"
12  7) "l4"
13  8) "l1"
14  9) "l3"
15 10) "l2"
16 127.0.0.1:6379> lrem mylist2 -1 l1
17 (integer) 1
18 127.0.0.1:6379> lrange mylist2 0 -1
19 1) "l1"
20 2) "l8"
21 3) "l7"
22 4) "l6"
23 5) "l1"
24 6) "l5"
25 7) "l4"
26 8) "l3"
27 9) "l2"

Lindex command to obtain the value of the specified index in the list, as follows:

1 127.0.0.1:6379> lindex mylist2 2
2 "l7"

Lset set the value of the specified location in the list as follows:

 1 127.0.0.1:6379> lset mylist2 2 l7777
 2 OK
 3 127.0.0.1:6379> lrange mylist2 0 -1
 4 1) "l1"
 5 2) "l8"
 6 3) "l7777"
 7 4) "l6"
 8 5) "l1"
 9 6) "l5"
10 7) "l4"
11 8) "l3"
12 9) "l2"

Ltrim command to crop the list and keep the contents of the specified area, as follows:

1 127.0.0.1:6379> ltrim mylist2 0 4
2 OK
3 127.0.0.1:6379> lrange mylist2 0 -1
4 1) "l1"
5 2) "l8"
6 3) "l7777"
7 4) "l6"
8 5) "l1"

Linsert specifies the insertion value at the position, and the second parameter represents before and after, respectively, as follows:

 1 127.0.0.1:6379> linsert mylist2 before l6 l99
 2 (integer) 6
 3 127.0.0.1:6379> lrange mylist2 0 -1
 4 1) "l1"
 5 2) "l8"
 6 3) "l7777"
 7 4) "l99"
 8 5) "l6"
 9 6) "l1"
10 127.0.0.1:6379> linsert mylist2 after l6 l100
11 (integer) 7
12 127.0.0.1:6379> lrange mylist2 0 -1
13 1) "l1"
14 2) "l8"
15 3) "l7777"
16 4) "l99"
17 5) "l6"
18 6) "l100"
19 7) "l1"

The rpoplbush command pops up an element to another list, as follows:

 1 127.0.0.1:6379> lpush list1 l1 l2 l3
 2 (integer) 3
 3 127.0.0.1:6379> lpush list3 14 l5 l6
 4 (integer) 3
 5 127.0.0.1:6379> rpoplpush list1 list3
 6 "l1"
 7 127.0.0.1:6379> lrange list1 0 -1
 8 1) "l3"
 9 2) "l2"
10 127.0.0.1:6379> lrange list3 0 -1
11 1) "l1"
12 2) "l6"
13 3) "l5"
14 4) "14"

Hash type related commands

 

The hset and hget commands are mainly used to set and obtain the contents of hash data types, as follows:

 1 127.0.0.1:6379> hset user1 id 1
 2 (integer) 1
 3 127.0.0.1:6379> hset user1 name Alan
 4 (integer) 1
 5 127.0.0.1:6379> hset user1 age 20
 6 (integer) 1
 7 127.0.0.1:6379> hget user1 id
 8 "1"
 9 127.0.0.1:6379> hget user1 name
10 "Alan"
11 127.0.0.1:6379> hget user1 age
12 "20"
13 127.0.0.1:6379> hget user1
14 (error) ERR wrong number of arguments for 'hget' command

The hmset command can set multiple contents at one time, as follows:

1 127.0.0.1:6379> hmset user1 id 2 name hsiang age 25
2 OK
3 127.0.0.1:6379> hget user1 id
4 "2"

The hgetall command is used to get all the contents corresponding to the key, as follows:

1 127.0.0.1:6379> HGETALL user1
2 1) "id"
3 2) "2"
4 3) "name"
5 4) "hsiang"
6 5) "age"
7 6) "25"

The hexists command is used to determine whether the attribute content in the key exists. If it is greater than 0, it means it exists. If it is equal to 0, it means it does not exist, as shown below:

1 127.0.0.1:6379> HEXISTS user1 addr
2 (integer) 0
3 127.0.0.1:6379> HEXISTS user1 id
4 (integer) 1

Hsetnx command, set if it does not exist, and do not set if it exists, as follows:

 1 127.0.0.1:6379> HSETNX user1 addr beijing
 2 (integer) 1
 3 127.0.0.1:6379> HSETNX user1 addr beijing
 4 (integer) 0
 5 127.0.0.1:6379> hgetall user1
 6 1) "id"
 7 2) "2"
 8 3) "name"
 9 4) "hsiang"
10 5) "age"
11 6) "25"
12 7) "addr"
13 8) "beijing"

Hincrby command, the value increment command of field corresponding to the key is as follows:

1 127.0.0.1:6379> HINCRBY user1 age 1
2 (integer) 26
3 127.0.0.1:6379> HINCRBY user1 age 1
4 (integer) 27
5 127.0.0.1:6379> HINCRBY user1 age 1
6 (integer) 28

The hkeys, hvals command is used to obtain the attributes or contents corresponding to the key, as follows:

 1 127.0.0.1:6379> HKEYS user1
 2 1) "id"
 3 2) "name"
 4 3) "age"
 5 4) "addr"
 6 127.0.0.1:6379> HVALS user1
 7 1) "2"
 8 2) "hsiang"
 9 3) "28"
10 4) "beijing"

HLEN, used to obtain the number of fields corresponding to the key, as follows:

1 127.0.0.1:6379> hlen user1
2 (integer) 4

Set type related commands

The Sadd command is used to add a set dataset. Data of set type will be de duplicated, as shown below:

1 127.0.0.1:6379> sadd myset s1 s2 s1 s2 s3 s3
2 (integer) 3

The smembers command is used to display all the contents of a set, as follows:

1 127.0.0.1:6379> SMEMBERS myset
2 1) "s2"
3 2) "s1"
4 3) "s3"

SREM command to delete the specified elements in the specified list, as follows:

1 127.0.0.1:6379> srem myset s1
2 (integer) 1
3 127.0.0.1:6379> SMEMBERS myset
4 1) "s2"
5 2) "s3"

The sismember command is used to determine whether an element exists in the set, as follows:

1 127.0.0.1:6379> SISMEMBER myset s2
2 (integer) 1
3 127.0.0.1:6379> SISMEMBER myset s1
4 (integer) 0

The scar command is used to obtain the number of members in the collection, as follows:

1 127.0.0.1:6379> SCARD myset
2 (integer) 2

The srandmember command is used to randomly return the members in the collection, as follows:

 1 127.0.0.1:6379> SRANDMEMBER myset 
 2 "s2"
 3 127.0.0.1:6379> SRANDMEMBER myset 
 4 "s2"
 5 127.0.0.1:6379> SRANDMEMBER myset 
 6 "s3"
 7 127.0.0.1:6379> SRANDMEMBER myset 
 8 "s2"
 9 127.0.0.1:6379> SRANDMEMBER myset 
10 "s3"

If the second parameter is not as follows, a variable will be returned randomly by default. If the second parameter is negative, the number of members with negative absolute value will be returned randomly. If the number of members is insufficient, repetition will occur. If the second parameter is positive, the specified number of members will be returned randomly. If the number of members is insufficient, the maximum member will be returned, as follows:

 1 127.0.0.1:6379> SRANDMEMBER myset  -6
 2 1) "s3"
 3 2) "s3"
 4 3) "s2"
 5 4) "s2"
 6 5) "s3"
 7 6) "s2"
 8 127.0.0.1:6379> SRANDMEMBER myset  6
 9 1) "s2"
10 2) "s3"

The sdiff command takes the difference set between the two, that is, in the first and not in the second, as follows:

 1 127.0.0.1:6379> sadd myset1 s1 s2 s3 s4
 2 (integer) 4
 3 127.0.0.1:6379> sadd myset2 s3 s4 s5 s6
 4 (integer) 4
 5 127.0.0.1:6379> sadd myset3 s5 s6 s7 s8
 6 (integer) 4
 7 127.0.0.1:6379> SDIFF myset1 myset2
 8 1) "s2"
 9 2) "s1"
10 127.0.0.1:6379> SDIFF myset1 myset2 myset3
11 1) "s2"
12 2) "s1"
13 127.0.0.1:6379> SDIFF myset2 myset1
14 1) "s6"
15 2) "s5"

The union command takes the union between two sets as follows:

1 127.0.0.1:6379> SUNION myset1 myset2
2 1) "s2"
3 2) "s4"
4 3) "s3"
5 4) "s5"
6 5) "s6"
7 6) "s1"

The sinter command takes the intersection between sets. If there is no intersection, it returns an empty array, as follows:

1 127.0.0.1:6379> SINTER myset1 myset2
2 1) "s4"
3 2) "s3"
4 127.0.0.1:6379> SINTER myset2 myset3
5 1) "s6"
6 2) "s5"
7 127.0.0.1:6379> SINTER myset1 myset2 myset3
8 (empty array)

Zset (ordered set) type related commands

The zadd command is used to add elements to the ordered set. If an element already exists, it will not add new elements, but only update the score, as shown below:

 1 127.0.0.1:6379> zadd myset4 1 z1
 2 (integer) 1
 3 127.0.0.1:6379> zadd myset4 2 z2
 4 (integer) 1
 5 127.0.0.1:6379> zadd myset4 3 z3
 6 (integer) 1
 7 127.0.0.1:6379> zadd myset4 4 z4
 8 (integer) 1
 9 127.0.0.1:6379> zadd myset4 5 z4
10 (integer) 0

The zscore command displays the score as follows:

1 127.0.0.1:6379> ZSCORE myset4 z4
2 "5"

The zincrby command is used to increase the score as follows:

1 127.0.0.1:6379> ZINCRBY myset4 1  z4
2 "6"
3 127.0.0.1:6379> ZSCORE myset4 z4
4 "6"

The zrange command returns the contents of the specified range in the collection, as follows:

 1 127.0.0.1:6379> ZRANGE myset4 0 -1 withscores
 2 1) "z1"
 3 2) "1"
 4 3) "z2"
 5 4) "2"
 6 5) "z3"
 7 6) "3"
 8 7) "z4"
 9 8) "6"
10 127.0.0.1:6379> ZRANGE myset4 0 -1
11 1) "z1"
12 2) "z2"
13 3) "z3"
14 4) "z4"

Zrangebyscore command, query the content through the score, as follows:

 1 127.0.0.1:6379> ZRANGEBYSCORE myset4 2 6 withscores 
 2 1) "z2"
 3 2) "2"
 4 3) "z3"
 5 4) "3"
 6 5) "z4"
 7 6) "6"
 8 127.0.0.1:6379> ZRANGEBYSCORE myset4 2 6
 9 1) "z2"
10 2) "z3"
11 3) "z4"

The zcard command is used to obtain the number of elements in the collection, as follows:

1 127.0.0.1:6379> zcard myset4
2 (integer) 4

Zcount command to get the number of members in the score range, as follows:

1 127.0.0.1:6379> zcount myset4 2 4
2 (integer) 2
3 127.0.0.1:6379> zcount myset4 1 6
4 (integer) 4

Zrem command to delete the specified elements in the collection, as follows:

1 127.0.0.1:6379> zrem myset4 z3
2 (integer) 1
3 127.0.0.1:6379> zrange myset4 0 -1
4 1) "z1"
5 2) "z2"
6 3) "z4"

About redis learning, this article is mainly to throw a brick to attract jade, more commands can refer toRookie course

remarks

Confucius said, “if you review the old and know the new, you can be a teacher.”.
Confucius said: learning without thinking is useless, thinking without learning is dangerous.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]