Basic KEY operations of five common string types in Redis

redis string operation

1.redis string type

String string type, commonly used for plain text

List list type, which is often used in a warehouse to delete a data after it is taken away

hash type, which is commonly used to import mysql data into redis

Set set type, which is used to compare different sets

2. Common redis instructions

Keys * / / list all keys. Do not use them in the production environment. It is likely that using keys will fill the server memory, causing redis to hang up, because the production environment has tens of thousands of keys

type key / / check the type of key

del key / / deletes a key

flushdb / / clear cache

3.redis string type operation

3.1. String type common instructions

commandmeaningusage
setSet a keyset key value
getView a keyget key value
msetSet multiple key smset key1 value1 key2 value2
mgetView multiple key smget key1 key2
incrIncrement the value of an integer by one by defaultincr key
incrbyCustomize the value of an integer to increase the valueincrby key added value
decrReduce the value of an integer by 1 by defaultdecr key
decrbyThe value of an integer is user-defineddecr key reduced value
delDelete a keydel key
existsCheck whether a key existsexists key
ttlView the lifecycle of a keyttl key
expireSet the life cycle of the keyexpire key life cycle duration
persistSet the life cycle of the key to never expirepersist key

3.2. Create a key and view it

Syntax: set key name value value

It can be created either interactively or on the command line

1.Create an interactive key
[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> set k1 v1
OK

2.Create a command line key
[root@redis-1 ~]# redis-cli set k2 v2
OK

3.In interactive view key content
[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> get k1 
"v1"

4.View on the command line key content
[root@redis-1 ~]# redis-cli get k2
"v2"

3.3. Read the value from the file and create the key

redis supports that the content of value is Chinese. Prepare a Chinese file and read the file value when creating a key

After Chinese files are stored in redis, they are only displayed as garbled code. In fact, they are still in Chinese. You can export them to files to view Chinese, or add – raw to solve the problem of garbled code display

1.Prepare a Chinese file
[root@redis-1 ~]# cat > test.txt <<EOF
> When you can study, you study; When you can do something, you do it; When you can fall in love, you fall in love again; When you can get married, you can get married again. When the environment does not permit, it cannot be forced; When the time comes, don't give up. This is one's life philosophy.
> EOF

2.Create a key Read from file value
[root@redis-1 ~]# redis-cli set test `cat test.txt`
OK

3.see key Content of,You'll find it's garbled
[root@redis-1 ~]# redis-cli get test
"\xe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you will find that it is Chinese output
[root@redis-1 ~]# cat test2.txt
 When you can study, you study; When you can do something, you do it; When you can fall in love, you fall in love again; When you can get married, you can get married again. When the environment does not permit, it cannot be forced; When the time comes, don't give up. This is one's life philosophy.

5.solve redis Display Chinese garbled code
[root@redis-1 ~]# redis-cli --raw get test
 When you can study, you study; When you can do something, you do it; When you can fall in love, you fall in love again; When you can get married, you can get married again. When the environment does not permit, it cannot be forced; When the time comes, don't give up. This is one's life philosophy.

3.4. Increment the value of an integer

redis supports increasing and decreasing the value of some integers, provided that the value value must be an integer

Integer increment enables the INCR command to be used. INCR is incremented by 1 each time, and INCRBY can be increased according to its own needs

1.Create an integer key,Increase 100 to 103
[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> set num1 100
OK
127.0.0.1:6379> INCR num1
(integer) 101
127.0.0.1:6379> INCR num1
(integer) 102
127.0.0.1:6379> INCR num1
(integer) 103
127.0.0.1:6379> get num1
"103"


2.use INCRBY Custom added value, add 103 to 113
[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> get num1
"103
127.0.0.1:6379> INCRBY num1 10
(integer) 113
127.0.0.1:6379> get num1
"113"

3.5. Devalue the Value of an integer

DECR decrements by 1 each time, and DECRER can customize the value of each decrement

1.use DECR Minus 1 each time
[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> get num1
"113"
127.0.0.1:6379> DECR num1
(integer) 112
127.0.0.1:6379> DECR num1
(integer) 111


2.use DECR Minus 10 each time
[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> get num1
"111"
127.0.0.1:6379> DECRBY num1 11
(integer) 100
127.0.0.1:6379> get num1
"100"

3.6. View key type

127.0.0.1:6379> type k1					//Type is string
string


127.0.0.1:6379> type num1				//Even if the value is an integer, it belongs to string because it is a key created by set
string

Command line view
[root@redis-1 ~]# redis-cli type k1
string

3.7. Create multiple key s using mset

Syntax format: mset key1 value2 key2 value2

127.0.0.1:6379> MSET k3 v3 k4 v4 k5 v5 k6 v6
OK

127.0.0.1:6379> keys *
1) "test"
2) "k2"
3) "k4"
4) "num1"
5) "k1"
6) "num"
7) "k5"
8) "k3"
9) "k6"

Create on the command line
[root@redis-1 ~]# redis-cli MSET k3 v3 k4 v4 k5 v5 k6 v6
OK

3.8. Use mget to view the values of multiple key s

Syntax format: mget key1 key2 key3

127.0.0.1:6379> MGET k1 k2 k3 k4 k5 k6
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
6) "v6"

View on the command line
[root@redis-1 ~]# redis-cli MGET k1 k2 k3 k4 k5 k6
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
6) "v6"

3.9. Check whether a key exists

Syntax format exists key

A return result of 1 indicates existence, and a return result of 0 indicates nonexistence

[root@redis-1 ~]# redis-cli 
127.0.0.1:6379> EXISTS k10
(integer) 0
127.0.0.1:6379> EXISTS k6
(integer) 1

Command line creation
[root@redis-1 ~]# redis-cli  exists k6
(integer) 1

3.10. Delete a key

Syntax format: del key

A return result of 1 indicates that the deletion is successful, and a return result of 0 indicates that it does not exist

[root@redis-1 ~]# redis-cli
127.0.0.1:6379> del k6
(integer) 1
127.0.0.1:6379> del k6
(integer) 0

command line
[root@redis-1 ~]# redis-cli del k6
(integer) 0

Keywords: Redis

Added by apsomum on Wed, 05 Jan 2022 21:04:14 +0200