Redis NoSQL, five common data types of redis, redis persistence mode, Jedis

Posted by markdr on Wed, 09 Feb 2022 06:28:24 +0100

NOSQL

NOSQL(Not Only Sql) is not only sql, but also refers to non relational database

There is a very important type of database in non relational database: cache database

Function: deal with data processing problems based on massive users and massive data.

Relational database

advantage:
It is a disk database, and the data is permanently stored on the disk, so the data is safe

Disadvantages:

Slow

Reason 1: the continuous writing of disk database is the reading and writing of IO

Reason 2: relational database has various logical controls such as constraints, checks and transaction operations

Non relational database

advantage

Fast, because it's a memory operation

shortcoming

If the data is unsafe, it is easy to lose some data

For massive access, first access NOsql. There are no numbers in NOsql. The data that will access mysql will be written into NOsql, and all accesses will access the data in NOsql

Five common data types of Redis

1,String


2,List


3,set

4,zset


5,hash

redis persistence mode

1. RDB policy

Redis DataBase(RDB) is the default persistence policy of redis When the persistence conditions are met, all the data in the memory at this time will be persisted to the disk file dump RDB is also called snapshot strategy because it can persist all memory data at a certain time

RDB default rule:

If more than one key is changed (addition, deletion and modification), the data will be persisted to dump once every 900 seconds (15 minutes) RDB file

If more than 10 key s are changed (addition, deletion and modification), the data will be persisted to dump once every 300 seconds (5 minutes) RDB file

If more than 10000 key s are changed (added, deleted or modified), the data will be persisted to dump once every 60 seconds (1 minute) RDB file

Advantages of rdb strategy

The persistence frequency is low, so the cache performance of redis will not be affected

Disadvantages of rdb strategy?

The low persistence frequency leads to serious data loss and data insecurity

2. AOF strategy

This policy is not enabled by default. This policy is to persist the addition, deletion and modification commands in one second to appendonly every second In AOF file

Advantages of AOF strategy

The persistence frequency is high, the data security is high, and the data is not easy to lose. The data within 1 second can be lost at most

AOF disadvantages

If the persistence frequency is high, the performance of redis will be affected. If the persistence frequency is high, it will lead to high pressure on the cpu

Which one is recommended for RDB and AOF in the future

If you want high data security, turn on AOF. Otherwise, only RDB is recommended, because AOF will reduce the performance of redis and seriously affect the CPU (if the CPU exceeds 60%, you will choose to turn off AOF). Note that AOF will affect the performance of redis

Jedis

java client operating redis

maven

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.0</version>
</dependency>

jedis.properties

# maximum connection
maxTotal=10
# Maximum timeout
maxWaitMillis=30000
# Host address of the server
host=localhost
# Port number of redis server
port=6379

Get Jedis tool class

/**
 * Objective: to provide external connection objects of Jedis
 */
public class JedisUtils {

    //1. Define global connection pool
    private static JedisPool jedisPool;

    //2. Initialize the connection pool object
    static {

        //Use ResourceBundle to read the property configuration file jedis Properties data
        //  Introduction: ResourceBundle is translated as "resource package", and the data of properties file can also be parsed
        //  Syntax of creating resource package object: resourcebundle resourcebundle = resourcebundle Getbundle ("file name") is read according to the file name under the classpath
        //  Syntax for reading data: string value = resourcebundle GetString ("key") reads the value according to the key

        //Create ResourceBundle resource package object
        ResourceBundle resourceBundle = ResourceBundle.getBundle("jedis");
        //Read data
        Integer maxTotal = Integer.valueOf(resourceBundle.getString("maxTotal"));
        Integer maxWaitMillis = Integer.valueOf(resourceBundle.getString("maxWaitMillis"));
        String host = resourceBundle.getString("host");
        Integer port = Integer.valueOf(resourceBundle.getString("port"));

        //Create connection pool configuration object
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal); //maximum connection
        config.setMaxWaitMillis(maxWaitMillis); //3 seconds

        //Create connection pool object
        jedisPool = new JedisPool(config, host, port);
    }

    //3. Provide common methods to obtain connections
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

ublic class JedisTesy2 {

    @Test
    public void test(){

        Jedis jedis = JedisUtil.getJedis();

        jedis.set("name","halulu");

        jedis.set("name","halulu Enhanced version");

        String name = jedis.get("name");
        System.out.println("name = " + name);
        jedis.del("name");

        jedis.setex("name",300, "halulu Escape version");

        jedis.setnx("name","halulu");

        jedis.close();
    }
}

Topics: MySQL