Redis basic commands

Posted by tomash on Mon, 24 Jan 2022 20:25:15 +0100

typora-root-url: D:\software\dev\Typora\locales

Installing Redis under Linux

1. Download the installation package! redis-6.2.4.tar.gz

$ cd /home/wlk
$ wget http://download.redis.io/releases/redis-6.0.6.tar.gz

2. Unzip the installation package to / opt, and you can see redis Conf file

$ tar xzf redis-6.0.6.tar.gz -C /opt
$ cd redis-6.0.6
 perhaps
cp redis-6.0.6.tar.gz /opt
tar -zxvf redis-6.0.6.tar.gz

3. Modifying the image source of Linux

cp /etc/apt/sources.list /etc/apt/sources.list.bak
vi /etc/apt/sources.list

see win10 Subsystem Ubuntu Version name of lsb_release -c
"eoan"representative ubuntu19.10,"xenial"representative ubuntu16.04,"bionic"representative ubuntu18.04,"disco"representative ubuntu19.04,"focal"representative ubuntu20.04

deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

After replacing the mirror source:
sudo apt update  perhaps apt-get update
sudo apt upgrade perhaps apt-get upgrade --fix-missing

4. Install gcc environment

install gcc environment
sudo apt-get  install  build-essential
 implement make
 implement make test

You need tcl 8.5 or newer in order to run the Redis test.
install Redis,function make test Error during:
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] Error 1

install tcl Just:
Copy code
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
sudo tar xzvf tcl8.6.1-src.tar.gz -C /usr/local/
cd /usr/local/tcl8.6.1/unix/
sudo ./configure
sudo make
sudo make install

5. Use the make command to install redis

$ cd redis-6.0.6
$ make
$ make install #This command checks whether the make command is successful after execution
## The default installation path is / usr/local/bin

root@LAPTOP-Q063DEUM:/usr/local# cd bin/
root@LAPTOP-Q063DEUM:/usr/local/bin# ll
total 52628
drwxr-xr-x 1 root root     4096 Jun 20 13:38 ./
drwxr-xr-x 1 root root     4096 Jun 20 10:55 ../
-rwxr-xr-x 1 root root  7100584 Jun 20 13:38 redis-benchmark*
-rwxr-xr-x 1 root root 11863856 Jun 20 13:38 redis-check-aof*
-rwxr-xr-x 1 root root 11863856 Jun 20 13:38 redis-check-rdb*
-rwxr-xr-x 1 root root  7103192 Jun 20 13:38 redis-cli*
lrwxrwxrwx 1 root root       12 Jun 20 13:38 redis-sentinel -> redis-server*
-rwxr-xr-x 1 root root 11863856 Jun 20 13:38 redis-server*
-rwxr-xr-x 1 root root    16888 Jun 20 11:03 tclsh8.6*
root@LAPTOP-Q063DEUM:/usr/local/bin# pwd
/usr/local/bin

6. Backup redis Conf file to the redis installation directory, / usr/local/bin

$ cd /opt/redis-6.0.6
$ cp redis.conf /usr/local/bin/wlkconfig/

7. reids is not started in the background by default. Modify the configuration file redis conf

daemonize yes

8. Start redis service

redis-server wlkconfig/redis.conf starts by specifying a configuration file

root@LAPTOP-Q063DEUM:/usr/local/bin# redis-server wlkconfig/redis.conf
18616:C 20 Jun 2021 14:05:45.681 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18616:C 20 Jun 2021 14:05:45.681 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=18616, just started
18616:C 20 Jun 2021 14:05:45.681 # Configuration loaded

9. Connect using redis cli

root@LAPTOP-Q063DEUM:/usr/local/bin# redis-cli -h localhost -p 6379
localhost:6379> ping
PONG
localhost:6379> set menu new Date
(error) ERR syntax error
localhost:6379> set menu 2021-06-20
OK
localhost:6379> keys
(error) ERR wrong number of arguments for 'keys' command
localhost:6379> keys *
1) "menu"
localhost:6379> get menu
"2021-06-20"
localhost:6379>

10. Check whether the redis process starts ps -ef | grep redis

root@LAPTOP-Q063DEUM:/usr/local/bin# ps -ef | grep redis
root     18617     1  0 14:05 ?        00:00:00 redis-server 127.0.0.1:6379
root     18630    85  0 14:11 pts/0    00:00:00 redis-cli -p 6379
root     18632 14241  0 14:11 pts/1    00:00:00 grep --color=auto redis

11. Shut down the redis service

./redis-cli shutdown

redis-benchmark

Redis benchmark is a stress testing tool, an official performance testing tool

Redis benchmark command line parameters

$ redis-benchmark -h localhost -p 6379 -c 100 -n 100000		## 100 concurrent, 100000 requests

====== SET ======
  100000 requests completed in 1.74 seconds
  100 parallel clients
  3 bytes payload
  keep alive: 1
  host configuration "save": 900 1 300 10 60 10000
  host configuration "appendonly": no
  multi-thread: no

75.03% <= 1 milliseconds
100.00% <= 1 milliseconds
57504.31 requests per second

Parameter item interpretation
1,Write test on 100000 requests
2,100 Concurrent clients
3,Write 3 bytes at a time
4,Keep alive There is only one connection
5,All requests were processed within 1 ms
6,57504 requests are processed per second.

Basic knowledge of redis

redis has 16 databases by default. The 0 database is used by default. You can use select to switch

Use the DBSIZE command to view the current database size

View all keys in the current database*

Clear the current database flushdb

Clear all database contents flush all

redis It's single threaded

Redis is very fast. Officials say that redis is based on memory operation. CPU is not the performance bottleneck of redis. The bottleneck of redis is based on machine memory and network bandwidth. Since it can be implemented by single thread, single thread is used

Why is redis single thread so fast?

1. Myth 1: high performance servers must be multi-threaded

2. Myth 2: multithreading (CPU context switching) must be more efficient than single thread

Core:

Read / write speed: CPU > memory > hard disk

redis puts all data in memory, so it is the most efficient to use single thread to operate. Multithreading (CPU context switching: time-consuming operation) is the most efficient for memory systems without context switching

Redis five data types

redis-key

127.0.0.1:6379> keys *		## View all key s
1) "menu"
2) "key:{tag}:__rand_int__"
127.0.0.1:6379> set name Wei Likai	## Set a key
OK
127.0.0.1:6379> keys *
1) "name"
2) "menu"
3) "key:{tag}:__rand_int__"
127.0.0.1:6379> get name		## View a key
"\xe9\x9f\xa6\xe7\xab\x8b\xe5\x87\xaf"
127.0.0.1:6379> expire name 10	## Set the expiration time of the key, in seconds
(integer) 1
127.0.0.1:6379> ttl name		## View remaining time of key
(integer) 7
127.0.0.1:6379> ttl name
(integer) 4
127.0.0.1:6379> ttl name
(integer) 2
127.0.0.1:6379> ttl name
(integer) 0
127.0.0.1:6379> keys *
1) "menu"
2) "key:{tag}:__rand_int__"
127.0.0.1:6379> type menu		## View the type of key
string
127.0.0.1:6379> type key:{tag}:__rand_int__
string
127.0.0.1:6379>
127.0.0.1:6379> exists menu		## Determine whether the key exists
(integer) 1
127.0.0.1:6379> exists name
(integer) 0
127.0.0.1:6379> set name wlk
OK
127.0.0.1:6379> keys *
1) "name"
2) "menu"
3) "key:{tag}:__rand_int__"
127.0.0.1:6379> move name 3		## Move the key to another library (16 libraries in total)
(integer) 1
127.0.0.1:6379> keys *
1) "menu"
2) "key:{tag}:__rand_int__"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> select 3		## Switch database
OK
127.0.0.1:6379[3]> keys *
1) "name"
127.0.0.1:6379[3]> get name
"wlk"

Detailed explanation of String type

127.0.0.1:6379> keys *			## View all key s
1) "name"
2) "menu"
3) "key:{tag}:__rand_int__"
127.0.0.1:6379> set age 28		## Set value
OK
127.0.0.1:6379> get menu		## Get value
"2021-06-20,2021-06-21"
127.0.0.1:6379> exists age		## Determine whether a key exists
(integer) 1
127.0.0.1:6379> strlen menu		## Gets the length of the key
(integer) 21
127.0.0.1:6379> append menu ",2021-06-21 23:01"		## The append command appends a string. If the key does not exist, set key
(integer) 38
127.0.0.1:6379> get menu
"2021-06-20,2021-06-21,2021-06-21 23:01"
127.0.0.1:6379> get age
"28"
127.0.0.1:6379> incr age		## incr, increase by 1
(integer) 29
127.0.0.1:6379> incrby age 2	## Specify step size, increase by 2
(integer) 31
127.0.0.1:6379> get age
"31"
127.0.0.1:6379> decr age		## decr, decrease by 1
(integer) 30
127.0.0.1:6379> decrby age 3	## Specify the step size and reduce it by 3
(integer) 27
127.0.0.1:6379> get age
"27"
127.0.0.1:6379>
########################################################
String range
127.0.0.1:6379> set key1 "hello,chenFeng"
OK
127.0.0.1:6379> get key1
"hello,chenFeng"
127.0.0.1:6379> GETRANGE key1 0 3	## Intercept string [0,3]
"hell"
127.0.0.1:6379> GETRANGE key1 0 -1	## Get the entire key value
"hello,chenFeng"
#########################################################
replace
127.0.0.1:6379> set key2 abcdefg
OK
127.0.0.1:6379> get key2
"abcdefg"
127.0.0.1:6379> SETRANGE key2 1 xx	## Replace the string starting at the specified location
(integer) 7
127.0.0.1:6379> get key2
"axxdefg"
##########################################################
setex (set with expire)		## Set expiration time
setnx (set if not exist)	## If it does not exist, the set value (often used in distributed locks)
127.0.0.1:6379> setex key3 30 "chenfeng,i like you"		## Set the value of key3 to expire in 30 seconds
OK
127.0.0.1:6379> get key3
"chenfeng,i like you"
127.0.0.1:6379> ttl key3	## View the remaining expiration time of key3
(integer) 23
127.0.0.1:6379> setnx key4 "mongoDb"	## If key4 does not exist, create key4 with the value "mongoDb"
(integer) 1
127.0.0.1:6379> get key3
(nil)
127.0.0.1:6379> get key4
"mongoDb"
127.0.0.1:6379> setnx key4 "redis"		## If key4 exists, the creation fails and the original value will not be replaced
(integer) 0
127.0.0.1:6379> get key4
"mongoDb"
#########################################################
mset	Batch setting value
mget	Batch get value
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3		## Batch setting value
OK
127.0.0.1:6379> keys *
1) "k1"
2) "k3"
3) "k2"
127.0.0.1:6379> mget k1 k2 k3				## Batch get value
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> MSETNX k1 v1 k4 v4			## This operation is atomic. Because k1 already exists, k4 also failed to set
(integer) 0
127.0.0.1:6379> keys *
1) "k1"
2) "k3"
3) "k2"
#####################################################
# Object 	 Set a user:1 object with the value of a JSON string
127.0.0.1:6379> set user:1 {name:zhangsan,age:25}
OK
127.0.0.1:6379> keys *
1) "user:1"
127.0.0.1:6379> get user:1
"{name:zhangsan,age:25}"
127.0.0.1:6379> mset user:1:name wangwu user:1:age 28	## The key here is a clever design, user: {ID}: {file}. This design is completely OK in redis
OK
127.0.0.1:6379> keys *
1) "user:1:age"
2) "user:1:name"
3) "user:1"
127.0.0.1:6379> get user:1
"{name:zhangsan,age:25}"
127.0.0.1:6379>
127.0.0.1:6379> get user:1:name
"wangwu"
127.0.0.1:6379> get user:1:age
"28"

List type details

Detailed explanation of Set type

Hash hash type details

Detailed explanation of Zset ordered set

Three special data types

Topics: Database Redis