Moocnet_ Learning summary of introduction to redis

Time:2020-10-18

Time: Sunday, May 21, 2017
Note: part of this article is from moocnet. @Moocnet: http://www.imooc.com
Teaching example source code: None
Personal learning source code: https://github.com/zccodere/s…

Chapter 1: Course Introduction

1-1 Course Introduction

Course introduction

Overview of NoSQL
Overview of redis
Installation and use of redis
Getting started with jedis
Data type of redis
General operation of keys
Features of redis
Persistence of redis

Chapter 2: overview of NoSQL

Overview of 2-1 NoSQL

What is NoSQL

NoSQL = Not Only SQL
Non relational database

Why NoSQL is needed

High performance high concurrent read and write
Efficient storage and access of massive data in huge storage
High scalability & high availability

Mainstream NoSQL products

Moocnet_ Learning summary of introduction to redis

Four categories of NoSQL database

Key value storage: redis
    Excellent: quick query
    Bad: the stored data lacks structure
Column storage: HBase
    Excellent: quick query、扩展性强
    Bad: the function is relatively limited
Document database: mongodb
    Excellent: data structure requirements are not very strict
    Bad: poor query performance, lack of unified query syntax
Graphic database: infogrid
    Excellent: using graph structure correlation algorithm
    Bad: we need to calculate the whole graph to get the result, so it is not easy to do the distributed cluster scheme

Comparison of four kinds of NoSQL databases

Moocnet_ Learning summary of introduction to redis

Features of NoSQL

Easy to expand
Flexible data model
Large amount of data, high performance
High availability

Chapter three: overview of redis

Overview of 3-1 redis

Redis high performance key value pair database, key value data types supported:

String type
List type
Ordered set type
Hash type
Collection type

Application scenarios of redis

cache
Task queue
Website visit statistics
Data expiration processing
App Leaderboard
Session separation in distributed cluster architecture

Chapter 4: installation and use of redis

4-1 installation of redis

Windows installation redis build environment

Virtual machine: VMware 10.0.2
Linux system: centos-6.5
SSH client: SecureCRT 7.3, securefx 7.3

It is recommended to use docker under Linux system to install redis. You need to install docker first

Download Image
docker pull hub.c.163.com/library/redis:latest
Run image
docker run --name myredis -d -p 6379:6379 hub.c.163.com/library/redis
Enter the container
docker exec -it CONTAINERID /bin/bash
Connect to redis
Redis cli 172.17.0.1:6379 or redis cli
dispatch orders
Sending Ping and replying to Pong means that redis is successfully installed

Moocnet_ Learning summary of introduction to redis

Chapter 5: introduction to jedis

5-1 getting started with jedis

Introduction to jedis

Jedis is the official preferred java client development package for redis
https://github.com/xetorthio/jedis

Construction of test project environment

Use gradle as the build tool. See the source code for details.

Code demonstration:

package com.myimooc.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 *Jedis test
 * Created by ChangComputer on 2017/5/21.
 */
public class JedisDemo1 {

    /**
     *Test single instance
     */
    @Test
    public void demo1(){
        //1. Set IP address and port
        Jedis jedis = new Jedis("192.168.77.128",6379);
        //2. Save data
        jedis.set("name","myimooc");
        //3. Access to data
        String value = jedis.get("name");

        System.out.println(value);

        //4. Release resources
        jedis.close();
    }

    /**
     *Connect using connection pooling
     */
    @Test
    public void demo2(){
        //Gets the configuration object for the connection pool
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //Set the maximum number of connections
        jedisPoolConfig.setMaxTotal(30);
        //Set the maximum number of idle connections
        jedisPoolConfig.setMaxIdle(10);
        //Get connection pool
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,"192.168.77.128",6379);
        //Connection through connection pool
        Jedis jedis = jedisPool.getResource();
        //Set data
        jedis.set ("name", "Zhang San");
        //Access to data
        String value = jedis.get("name");
        System.out.println(value);
        //Releasing resources
        jedis.close();
        //Release connection pool
        jedisPool.close();

    }
}

Chapter 6: the data structure of redis

6-1 string

Five data types:

String
Hash
String list
String set
Sorted set
String set

Key points for key definition:

Don't be too long: it's best not to exceed 1024 bytes
Don't be too short: too short is not good for reading
Unified command specification

Store string

Binary secure, the data stored and retrieved are the same
The maximum data length that value can hold is 512M

Store string common commands

Set key value
Value: get key
Get value: GetSet key value
Delete: del key
Increase or decrease of value: incr key decr key
Extension command: incrby key number decode key number
Append command: append key str

6-2 hash

Storage hash

Map container for string key and string value
Each hash can store 4294967295 key value pairs

Common commands for storing hash:

Assignment: hset name key value hmset name key value key value
Value: hget name key hmget name key key hgetall name
Delete: HDEL name key del name
Add number: hincrby name key number
Self study command: hexests name key HLEN name hkeys name hvals name

6-3 list

Store list

ArrayList uses arrays
LinkedList uses two-way linking
Add data to double linked list
Delete data from double linked list

Store list common commands

Add both ends

lpush key value1 value2 value3
Put value1, Value2 and value3 into the linked list as key. When the key does not exist, the linked list will be created automatically.
    When the linked list corresponding to key exists, the data is inserted from the left.
rpush key value1 value2 value 3
Insert data from the right.

view list

lrange key start end
View the key linked list, starting from the specified start to end

Eject at both ends

lpop key
Pop up on the left, return and pop up the head (first) element of the linked list associated with the specified key
rpop key
Pop up on the right, return and pop up the tail (last) element of the linked list associated with the specified key

Get the number of list elements

llen key
Returns the length of the linked list associated with the specified key. When the key does not exist, it returns 0

Extended command

lpushx key value
Insert value in the header only if the specified key associated list exists
rpushx key value
Insert value at the end only if the specified key associated list exists
lrem key count value
In the linked list associated with the specified key, delete count values
    When the count is less than 0, it is deleted from the end to the end
    When count equals 0, all are deleted
    When count is greater than 0, it is deleted from beginning to end
lset key index value
In the linked list associated with the specified key, modify the value whose subscript is index to value
linsert key before value newvalue
Insert newvalue before value in the linked list associated with the specified key
linsert key after value newvalue
In the linked list associated with the specified key, insert newvalue after value
rpoplpush key1 key2
Pop up the first value in key1 and press it into the head of key2

Rpolpush usage scenarios

Moocnet_ Learning summary of introduction to redis

6-4 set

Storage set

Unlike the list type, duplicate elements are not allowed in the set collection
The maximum number of elements a set can contain is 4294967295

Add elements

sadd key value1 value2 value3
Add value1 and Value2 and value3 to the set set set of the key

Delete element

srem key value1 value2
Delete value1 and Value2 in the set set set as key

Gets the elements in the collection

smembers key
Gets the element in the set set set set as key
sismember key value1
Determine whether value1 exists in the set set of key. 1. Exist; 0, do not exist

Difference set operation in set

sdiff key1 key2
Gets the elements that exist in key1 but not in key2. It's related to the order of keys

Intersection operations in sets

sinter key1 key2
Get the elements that exist in both key1 and key2

Union operation in set

sunion key1 key2
Get all the elements in key1 and key2. When the elements are repeated, one duplicate element will be removed

Extended command

scard key
Gets the number of members in the set set set as key
srandmember key
A member of the set set set randomly returned as key
sdiffstore newKey key1 key2
Store the elements of the difference set of two sets key1 and key2 into another set newkey
sinterstore newKey key1 key2
Store the elements of the intersection of two sets key1 and key2 in another collection newkey
sunionstore newKey key1 key2
Store the elements of the union of two sets key1 and key2 in another collection newkey

Usage scenarios

Track some unique data. For example, to access the information of a blog's unique IP address, you only need to store the visitor's IP address in redis every time you visit the blog. It can be used to maintain the relationship between data objects by making full use of the convenience and efficiency of set type server-side aggregation operation. For example, the ID of all customers who purchase an electronic device is stored in a specified set, while the ID of a customer who purchases another electronic product is stored in another set. If you want to know which customers have purchased these two products at the same time, you can use their intersection operation at this time.

6-5 sorted-set

Overview of sorted set

Sorted sets and sets types are very similar. They are both sets of strings and do not allow duplicate members to appear in a set. The main difference between them is that each member in sorted sets will have a score associated with it. Redis sorts the members in the set from small to large through scores. However, it should be noted that although the members in sorted sets must be unique, the score can be repeated. Adding, deleting or updating a member in sorted set is a very fast operation, and its time complexity is logarithm of the number of members in the set. Because the members in sorted sets are ordered in their positions in the collection, even accessing members in the middle of the collection is still very efficient. In fact, this feature of redis is difficult to implement in many other types of databases. In other words, it is very difficult to model in other databases to achieve the same efficiency as redis at this point.

Sorted set common commands

Add elements

zadd key score1 value1 score2 value2 score3 value3
Add three elements to the sorted set collection named key. Each element has a score, score and value

Get elements

zscore key value
Get the score score corresponding to value in the sorted set set named key
zcard key
Gets the number of members in the sorted set collection named key

Delete element

zrem key value1 value2
Remove the value value1 and Value2 elements from the sorted set collection named key

Range query

zrange key start end
Find the elements between start and end. By default, the score is sorted from small to large
zrange key start end withscores
Finds the element between start and end and displays the element's score
zrevrange key start end withscores
Finds the element between start and end and displays the element's score,分数从大到小排序
zremrangebyrank key start end
Delete elements according to the specified range
zremrangebyscore key startscore endscore
Delete elements according to the specified score range

Extended command

zrangebyscore key startscore endscore withscore limit startindex count
Return the elements with scores between the specified range. The scores are sorted from small to large, starting with the subscript StartIndex, and count is displayed
zincrby key addscore value
Add addScore to the score score of the specified value element
zcount key startscore endscore
Gets the number of elements with a score between a range

Usage scenarios

In the ranking of the game. The scoreboard of large-scale games, whenever the player's score changes,
    You can execute the zadd command to update the player's score
Hot topics on microblog
Building index data

Chapter 7: general command of redis

7-1 general operation of keys in redis

Keys general operation

keys *
Get all the keys
keys my?
Get all keys that start with my
del key1 key2 key3
Delete key1 and key2 and Key3
exists key
Check whether the key exists. 1. It exists; 0. Does not exist
rename oldkey newkey
Rename key and rename oldkey to newkey
expire key 1000
Set the expiration time. Unit: Second
ttl key
Check the remaining timeout time of the key. If the key has not set the expiration time, it will return - 1
type key
Gets the data type of the specified key

Chapter 8: redis affairs

8-1 features of redis

Related characteristics

Multi database
Redis transaction

Multi database

A redis instance can contain multiple databases, and the client can specify which database to connect to. A redis instance can provide up to 16 databases with subscripts from 0 to 15. The client connects to database 0 by default. Select which database to connect to.

Multi database related commands

select 1
Connect to database 1
move key 1
Move key to database 1

Redis transaction related commands

multi
When the transaction is started, the subsequent commands will be stored in the command queue until the commit transaction command is executed
exec
When a transaction is committed, the commands in the command queue are atomized for execution
discard
Rollback transaction

Chapter 9: persistence of redis

Overview of 9-1 redis persistence

How persistence is used

RDB persistence: it is supported by default and does not need to be configured. Write the data and snapshot in memory to disk within the specified time interval
AOC persistence: every operation processed by the server will be recorded in the form of log. At the beginning of the redis server startup, the,
    The file will be read to rebuild the database to ensure the integrity of the data after startup
No persistence: disable the persistence function of redis server through configuration. In this case, redis is a caching mechanism
Using both RDB and AOF

9-2 RDB persistence

Advantages of RDB persistence

Only one file is included to facilitate file backup
RDB is a very good choice for disaster recovery
Performance maximization, persistence operation is completed by child process
Compared with AOF, large datasets start more efficiently

Disadvantages of RDB persistence

If you want to ensure the high availability of data, that is to avoid the loss of data, RDB will not be a good choice. When the system goes down before the fixed time persistence, and before it can write data to the hard disk, the data will be lost. When the data set is too large, it may cause the server to stop for hundreds of milliseconds or even one second.

RDB mode persistence configuration

redis.conf
Save 900 1: every 900 seconds, at least one key changes and is persisted once
Save 300 10: every 300 seconds, at least 10 keys change and will be persisted once
Save 60 10000: every 60 seconds, at least 10000 keys change and will be persisted once
Dbfile Name: configuration persistence file name
Dir: configuration persistence file save path, default. / configuration file current path

9-3 AOF persistence

Aof persistence advantages

Bring higher data security, synchronize every second or change or not
For the log file write operation, the append mode is adopted. When the write process is down, the existing content will not be destroyed
If the log is too large, redis will automatically start the rewriting mechanism
Aof contains clear format and easy to understand log file to record all modification operations, through which data reconstruction can be completed

Aof persistence disadvantage

For the same dataset, AOF files are larger than RDB files
According to different synchronization strategies, the running efficiency of RDB is lower than that of RDB

Aof persistent configuration

redis.conf
Appendonly: configure whether AOF persistence is enabled or not. Default No: not enabled
Appendfile Name: configure AOC persistence file name
Appendfsync always: synchronize every time you modify it. It is not enabled by default. For more security, it is recommended
Appendfsync everysec: synchronize once per second, enabled by default
Appendfsync No: not synchronized, not enabled by default

Chapter 10: course summary

10-1 course summary of redis

Course summary

What is NoSQL
What is redis introduced
Redis installation and simple use
To get started with jedis, you need to operate redis through the program
This paper introduces the common data types of redis, including string and hash
Understand the general operation of keys
Understand redis's multi database and transaction
Understand the persistence of redis

The most important thing is: to be able to use the program to operate redis