Quickly learn and get started with Redis

1. What is Redis?

It can be understood as a non-relational database, intermediate key, but strictly speaking, it is not a database, it is a collection of data structured storage methods, does not comply with SQL standards, transactions,

1.2 Why do you want to use it for what?

A-The data is stored in the memory, the access speed is fast, and it is not easy to lose.       

B- There are five types of types that can be stored

C- can perform cluster and transaction operations

D- There are interfaces for clients in multiple languages, which is more convenient

Generally, it is useful in actual projects: for caching, storing information when logging in, leaderboards, counters to prevent thread safety, etc.

2. Installation and use of Redis

2.1 Download address 

Download | Redis ;

Chinese official website: CRUG website

After downloading, just unzip and play

2.2 Use the command to start redis

You can double-click directly, but it is recommended to start with cmd

2.2 Five major data types of redis -- basic command operation

A-String type-key —value type

set name XX -- set the key to name and the value is xx

get name xx Get the key as name and the value is xx

mset (set multiple values) mget (get multiple)

Too many screenshots to see for yourself

 B-List type

C-key type

 D-set type

 E-hash type

 2.3 Actual use of java to operate redis

Under the construction based on the SpringBoot project

Corresponding import dependencies

<!--spirngboot springdata对redis支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Perform basic configuration in the configuration file

redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: 123456
    jedis:
      pool:
        max-wait: 2000ms
        min-idle: 2
        max-idle: 8

use in code

 @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void test(){
        redisTemplate.opsForValue().set("redis","string类型");
        System.out.println(redisTemplate.opsForValue().get("redis"));
    }

Note that there will be a serialization problem here,

When we put the object in redis, there will be a serialization problem when we take it out, so we need to deserialize it.

Method 1: The object you want to put in redis implements the Serializer interface, but in this way, each class to be placed implements it,

Method 2: Transform redis,

Abandon the previous redis interface and use the following redis for operations in the project

/缓存的配置
@Configuration
public class RedisConfig {

    @Resource
    private RedisConnectionFactory factory;


    //使用JSON进行序列化
    @Bean
    public RedisTemplate<Object, Object> redisTemplate() {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        //JSON格式序列化
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
         //key的序列化
        redisTemplate.setKeySerializer(genericJackson2JsonRedisSerializer);
        //alue的序列化
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        //hash结构key的虚拟化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //hash结构value的虚拟化
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        return redisTemplate;

    }
}

Different points use serialized redis when importing

//序列化的redis
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

3. Persistence of redis

The presence or absence of redis is implemented based on memory. In order to prevent data loss caused by downtime, etc.,

Redis backs up the data in memory to another disk.

Two persistence methods of redis: AOP, RDB

  • RDB: record data snapshot
     
       Advantages:  
           1. Generate a persistent file, which is convenient for file backup. Disaster recovery RDB is a very good choice
           2. Fork sub-processes to persist, performance is better than AOF, file size is smaller, and recovery speed at startup Disadvantages
       :
          1. There is no way to keep 100% of the data from being lost.
          2. The data set is large, and the server will freeze when the FORk child process is persisted.


    AOF: record write command
       advantages:  
         1. Data is more secure
         2. Append is used, even if it is down for a long time, it will not affect the persistent data
         3. The log is too large and can be rewritten
         4. AOF log format is clear and easy to understand  
     

       Disadvantages:
          1.AOF files are usually larger than RDB files
          2.AOF data recovery is slower than RDB
      

    Best practice: combine the two, use RDB for data backup, migration, and disaster recovery. AOF persistence ensures that data is not lost.

4. Redis elimination mechanism

What are the elimination mechanisms?

  • volatile-lru: Select the least recently used data from the data set with an expiration time set to eliminate

  • volatile-ttl: Select the data that will expire from the data set that has set the expiration time and eliminate it

  • volatile-random: Randomly select data elimination from the data set with an expiration time set

  • allkeys-lru: Select the least recently used data from the data set to eliminate

  • allkeys-random: Randomly select data from the data set to eliminate

  • no-enviction: Do not use enviction

For modifying the configuration redis.window.conf, modify maxmemory to modulate the elimination mechanism

There are many more things about redis, such as sentinel mode, cluster cluster, avalanche encountered in caching, breakdown, penetration, etc.

This article is just to let beginners quickly use the redis middleware in the project, and the redis in-depth version will be launched later

Welcome to study and exchange, please point out the shortcomings, if you like it, please like + bookmark, thank you guys

Guess you like

Origin blog.csdn.net/m0_67601895/article/details/126362254