Redis is a very popular NoSQL database used as Cache based on KV structure.

The best way to learn Redis is to try it. Before introducing the core content of Redis, this article mainly introduces the installation and use of Redis and the basic knowledge of Redis.

Before we begin, I’d like to share with you this video tutorial on what NoSQL is and how it compares to a relational database. Know what Redis is, what it can do and how to use it; Master the installation and configuration of Redis in Windows and Linux, five data types, common operation commands, Redis persistence, master-slave replication, transaction control and Java development with Jedis operation.

Watch online: www.bilibili.com/video/BV1Uz.

Data download: www.bjpowernode.com/javavideo/1…

Redis is introduced

Remote Dictionary Server(Redis) is an open source C language, network support, memory based and persistent key-value database. The Key character type, whose value can be String, hash, list, set, and sorted sets. Each data type has its own unique command. So it is also commonly referred to as a data structure server.

Redis was written by Antirez, originally from Sicily, Italy, who now lives in Catania. Currently working at Pivotal, the team behind the Spring framework, Antirez is known as the father of Redis.

The Redis code is hosted on GitHub

Github.com/antirez/red…

Liverpoolfc.tv: redis. IO /

English: www.redis.cn/

Install Redis on Windows

Redis for Windows is Redis provided by Microsoft’s open source division. This version of Redis is suitable for developers to learn to use. Production environments use Redis on Linux systems

(1) Download

Liverpoolfc.tv: redis. IO /

Windows version: github.com/MSOpenTech/…

(2) Installation

Decompress the redis-x64-3.2.100. zip file and place it in a directory (for example, D :\tools\). Directory structure:

(3) Start

  1. Windows7 System double-click redis-server.exe to start Redis

  1. 10 Windows system

If redis-server.exe fails to be executed and the configuration file cannot be found, run the following command: On the cli, run the following command:

D: \ tools \ Redis – x64-3.2.100 > Redis – server. Exe Redis. Windows. Conf

As shown in figure:

(4) Close

Press CTRL + C to exit the Redis server.

Install Redis on Linux

(1) Installation

  1. Upload redis-4.0.13.tar.gz to Linux. Use the Xftp tool

  1. Decompress redis-4.0.13.tar.gz to /usr/local

  1. View the decompressed file

Switch to CD /usr/local

Perform ll

  1. Install the GCC compiler

Redis is written in C. Use source file installation mode, need to compile C source file, will use GCC compiler.

What is GCC?

GCC, short for GNU Compiler Collection, is the next set of Compilers for Linux (equivalent to JavAC) that are compilers for C or C ++ programs.

Install GCC using yum. Yum -y install GCC

In the decompressed Redis directory, run the CD /usr/local/redis-4.0.13 make command.

  1. Compile the Redis source file
  1. Start making

CD /usr/local/redis-4.0.13 and run make

If an error occurs during the make command:

error: jemalloc/jemalloc.h: No such file or directory

Solution Run make MALLOC=libc

2) Execute the make success flag

3) To view the result of make, go to the CD SRC directory

CD SRC is executing ls

(2) Start Redis

Conf in the redis installation directory is an important configuration file, which contains the configuration information of the Redis server.

You need to specify the configuration file path to start Redis.

Startup mode:

/redis-server Redis. conf Configuration file path

/redis-server redis.conf configuration file path &

The first kind of foreground start

Start the server of Redis: Switch to the SRC directory and run the redis-server program

Or use the full path

The redis application can not exit the current window, exit the window, the application is terminated.

View the process started by Redis in another window

The second kind of background startup

Run the./redis-server redis.conf configuration file path & in the SRC directory

Close the window and check the Redis process. It still exists.

View the Redis process

(3) Close Redis

Closing mode:

(1) Use the Redis client to shut down and issue the shutdown command to the server

Switch to redis-4.0.13/ SRC/and run./redis-cli shutdown

In this way, Redis completes the data operation and then shuts it down.

Such as:

② Kill PID or kill -9 PID

This shuts down the application without considering whether any data is being performed on the current application. First to use ps – ef | grep redis, find out process in the use of the kill pid

Redis client

A Redis client is a program that connects to a Redis server via the network, sends commands to the Redis server using commands that Redis can recognize in the client software, and tells Redis what it wants to do.

Redis displays the processing results on the client interface. Interact with the Redis server through the Redis client.

The Redis client sends the command and displays the Redis server processing results in.

Redis command line client:

Redis – CLI (Redis Command Line Interface) is the redis client based on the Command Line. It is used to interact with the server. We can use this client to execute various commands of Redis.

There are two common connection modes:

  1. Direct connection to REDis (default IP127.0.0.1, port 6379) :./redis-cli

In the redis installation directory/SRC, run./redis-cli

This command is connected to redis on host 127.0.0.1, port 6379

  1. Specify the IP address and port to connect to redis:./redis-cli -h 127.0.0.1 -p 6379

-h Redis host IP address (any Redis server can be specified)

-p Port number (different ports indicate different Redis applications)

In the redis installation directory/SRC, run./redis-cli -h 127.0.0.1 -p 6379

Case 1:

Redis remote client

Redis Desktop Manager: C++ written, quick response, good performance. Redisdesktop.com/

Click on the “DOWNLOAD”

  1. Install the client software

Use this tool on a Windows system, connect to Redis on Linux or Windows, and double-click this exe file to install

Startup interface after installation:

Use the client to connect to Redis for Linux

Before connecting to Linux Reids, you need to modify the configuration information of the Redis server. Redis servers are secured and by default only the local machine (the machine on which Redis is installed) can access them. The configuration information is stored in the Redis installation directory in the Redis. Modify two Settings for this file.

To connect to redis remotely, you need to modify the redis. Conf configuration file in the redis home directory:

①. Bind IP This line is commented

②. Protected-mode yes Change the protection mode to no

Run the vim command to modify the redis. Conf file. Back up the file before modifying it, and run the cp command

Perform vim redis. Conf

  1. Start Redis using redis.conf

After modifying the configuration file, you need to restart the Reids using the configuration file. By default, the configuration file is not loaded. Closed already started

To move Redis, run the following command to start Redis in the Redis installation directory:

./redis-server .. /redis.conf &

  1. Disable the firewall for Linux

On Linux, run the systemctl stop firewalld command

  1. Configure Redis Desktop Manamager(RDM) and connect to Redis

In the main window of RDM, click “Connect to Redis Server” in the lower left.

After successful connection:

Redis programming client

Jedis

Redis Java programming client, Redis official preferred use Jedis, Jedis is a small but very robust

Java client for Redis. Redis can be used from Jedis just like the Redis command line.

  • Jedis is fully compatible with Redis 2.8.x and 3.x.x
  • Jedis source: github.com/xetorthio/j…
  • The API documentation: xetorthio. Making. IO/jedis /

Redis other programming language clients:

More than 40 languages, including C, C++, C#, Erlang, Lua, Objective-C, Perl, PHP, Python, Ruby, Scala, and Go, have programming clients that connect to Redis

Redis basic operation commands

Redis defaults to 16 libraries (configurable in the redis.conf file, which is important and will be used in many subsequent operations)

(1) Communicate commands and check the status

Redis >ping return PONG

Explanation: If ping is typed, Redis returns PONG to us, indicating that redis service is running properly

(2) Check the number of keys in the current database: dbsize

Grammar: dbsize

Function: Returns the number of keys in the current database. Return value: number, number of keys

For example, check the number of keys in index 5 and then library 0

(3) Redis uses 16 libraries by default

Redis uses 16 libraries by default, ranging from 0 to 15. Modify the number of databases in the redis. Conf file. Databases 16

(4) Switch database command: select DB

With other databases, the command is Select index

Example 1: Select 5

Flushdb: flushdb; flushdb: flushdb

(6) The built-in redis client exits the current Redis connection :exit or quit

Redis Key operation command

1. keys

Grammar: Keys pattern

Find all keys that match pattern. Wildcards can be used in pattern. Wild card:

  • * : indicates 0- multiple characters, for example: keys * Queries all keys.
  • ? : indicates a single character, for example: wo? D) Word, wood

Example 1: Display all keys

Example 2: Use * to represent 0 or more characters

Example 3: Using? Represents a single character

2. exists

Syntax: exists key [key…

Function: Checks whether the key exists

Return value: integer, 1 if key exists, 0 if other keys exist. If multiple keys are used, the number of existing keys is returned.

Example 1: Check whether the specified key exists

Example 2: Check multiple keys

3. expire

Syntax: expire key seconds

Function: Sets the lifetime of a key. When the lifetime exceeds the lifetime, the key is automatically deleted. The units are seconds. Return value: 1 is returned on success, 0 otherwise.

Example 1: Set the red light countdown to 5 seconds

4. ttl

Syntax: TTL key

Function: Returns the remaining TTL (time to live) of the key in seconds.

  • -1: The lifetime of the key is not set and the key never expires.
  • -2: The key does not exist
  • Number: remaining time of the key, in seconds

Example 1: Set the redlight expiration time to 10 and check the remaining time

5. type

Syntax: Type key

Function: View the data type of the value stored in the key. Returned value: The data type represented by a string

  • None (key does not exist)
  • String (string)
  • List (list)
  • Set
  • Zset (ordered set)
  • Hash table

Example 1: Look at the key: wood that stores the string

Example 2: Check the nonexistent key

6. del

Syntax: del key [key…

Function: Deletes existing keys and ignores non-existing keys. Return value: number, number of keys deleted.

Example 1: Delete the specified key