You may think Redis is easy to use, but if someone asks you the following questions (like a colleague or an interviewer), can you answer them?
- What is Redis?
- What kinds of data structures can Redis store?
- How many persistence mechanisms does Redis have? What are their advantages and disadvantages?
- What scenarios require Redis?
- What is cache avalanche and how can it be avoided?
- What is cache penetration and how can it be avoided?
- What is cache breakdown and how can it be avoided?
If you can answer all the questions, congratulations, you have a certain understanding of Redis, if you can’t answer, it doesn’t matter, this series of blogs will explain a series of Redis, welcome to follow!
If you want to learn Redis, you need to know what Redis is and how to install the Redis environment, which is the main content of this blog.
1. Introduction of Redis
What is Redis?
Redis is an open source (BSD-licensed) store of in-memory data structures used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, collections, and ordered collections. – Redis website
Redis is an open source, network-enabled, memory-based and persistent, high-performance key-value database written in ANSI C. — Baidu Encyclopedia
Redis is a high-performance key-value storage system distributed under the BSD open source protocol, commonly known as data structure server. — Other netizens
Redis is a remote in-memory database with robust performance, replication features and a unique data model for problem solving. Redis provides five different types of data structures to which a wide variety of problems can be mapped naturally. — Redis In Action
Redis is a very fast non-relational database. It can store the mapping between keys and 5 different types of values, persist key-value pair data stored in memory to hard disk, use replication to expand read performance, and use client sharding to expand write performance. — Redis In Action
2. Redis environment installation (Windows)
Note: Redis does not officially provide the Windows version of Redis, and it is not recommended to use the Windows version of Redis in the production environment of my current company, which is deployed on Linux servers.
Microsoft Open Tech Group provides a Windows version of Redis, which can be downloaded from github.com/microsoftar… .
Unzip the downloaded files to your favorite directory, E:\Tools\ redis-x64-3.0.504, as shown below:
To start the Redis service, double-click redis-server.exe marked in red:
You can also open a CMD window, switch to the Redis directory, and run the following command to start:
redis-server.exe redis.windows.conf
Copy the code
If the server is restarted, you need to open the Redis server again. To solve this problem, we can install Redis as Windows service:
cd E:\Tools\Redis-x64-3.0.504
redis-server --service-install redis.windows.conf
Copy the code
You can start/stop the service directly on the interface, or you can run CMD to start/stop/uninstall the service:
Start the service:
redis-server --service-start
Copy the code
Stop service:
redis-server --service-stop
Copy the code
Uninstall service:
redis-server --service-uninstall
Copy the code
3. Redis environment installation (Linux)
IO /download a stable version of Redis on the local computer. Download a stable version of Redis on the local computer.
Then use a tool, such as Xftp, to upload the locally downloaded files to a directory on the Linux server, such as/MNT.
The second method is to directly download the Redis installation package to the/MNT directory of the server by running the following command:
CD MNT/wget - q http://download.redis.io/releases/redis-5.0.7.tar.gzCopy the code
Then, run the following command to decompress the file:
The tar - XZF redis - 5.0.7. Tar. GzCopy the code
Then, run the following command to move the decompressed file to the /usr/local directory and compile it:
Mv redis-5.0.7 /usr/local/cd /usr/local/redis-5.0.7 makeCopy the code
Note: After executing the make command, the screen will output a lot of information, about 2 or 3 screens
If the following information is displayed, the compilation is complete:
Then switch to the /usr/local/redis-5.0.7/src directory and run make install to install:
cd src/
make install
Copy the code
Then switch to /usr/local/redis-5.0.7, create folders bin and etc, and move the redis.conf file from this directory to etc.
Run the mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server command in the /usr/local/redis-5.0.7/src directory
Move to the bin directory you just created:
cd .. mkdir bin mkdir etc mv redis.conf etc/ cd src mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server / usr/local/redis - 5.0.7 / bin /Copy the code
Go to /usr/local/redis-5.0.7/etc and modify the redis configuration file redis.conf:
cd ..
cd etc/
vi redis.conf
Copy the code
Redis. Conf changes the following four points:
-
Change daemonize no to daemonize yes to run in the background
-
Comment out the line bind 127.0.0.1
Bind here means that only the specified network segment can access the redis remotely.
-
Change protect-mode yes to protect-mode no
The default value is protect-mode yes, which does not allow remote access
-
Add requirePass 123456 Set password (default password is empty)
Finally, switch to the /usr/local/redis-5.0.7/bin directory and run the following command to start the redis service:
cd .. CD bin/redis server/usr/local/redis - 5.0.7 / etc/redis. ConfCopy the code
After starting, you can run the following command to check whether Redis is started and the process ID:
ps aux | grep redis-server
Copy the code
4. Redis Hello World example
Open a CMD window and open a Redis-CLI interactive window to simply use Redis:
redis-cli.exe -h 127.0.0.1 -p 6379
Copy the code
Set up a key-value cache where key is hello and value is Hello world! :
set hello "hello world!"
Copy the code
Get the value with key hello:
get hello
Copy the code
5. Redis Desktop Manager
Although you can view Redis data by command, it is not very friendly. Here is a popular tool: Redis Desktop Manager.
Redisdesktop.com/
The current version of 2019.5 is available for a fee or a 14-day trial period.
In 2019, 6 copies will still be opened like the following, it seems that the price has increased, haha.
However, we can still download the previously free version at github.com/uglide/Redi… .
The installation process is relatively simple and will not be described here. After the installation, connect to the Redis server in the local Windows environment:
After the connection is successful, you can see the values you set earlier:
You can also connect to the Redis server in a remote Linux environment:
Some students may use the tool is Another Redis. Dsektop. Manager, you only post the download address: electronjs.org/apps/anothe… , interested students can download by themselves.
6. Source code and reference
Redis Installation and deployment (Windows)
Windows under the use of Redis (a) installation use
Install Redis on Linux