spring cache cache optimization

cache optimization

Cache optimization based on Redis

insert image description here

1. Environment construction

Redis is configured
and added to the Pom file

        <!--导入Redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

Add Redis configuration class

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    
    
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    
    
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        //默认的Key序列化器为:JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}

Add configuration to yml
insert image description here

2. SMS verification code, login optimization

Add valid time verification to the verification code, and set the valid time of the SMS verification code
insert image description here

If the login is successful, the verification code in the cache will be automatically deleted.
Optimization location: com.cc.controller.UserController sendMsg and login
injected into RedisTemplete
insert image description here
to optimize the verification code
insert image description here
. Optimize
the login method after login
insert image description here
insert image description here

The filter should also be changed here, because the login id data is changed from session to redis storage, so the relevant parts of the filter must be modified
insert image description here

3. Cache the front-end menu data

insert image description here

The idea of ​​caching is to ensure that the data in the cache database and DBMS are kept in sync to avoid reading dirty data (data that has not been updated)
insert image description here

Optimize DishController and add cache. If
you visit again, you can find that if you have already cached the current dish category, you will not check the database again.

Update the dishes and update the cache at the same time
to ensure that there is less dirty data, so join the cleanup cache. If the new data is not cleaned up in time, the list database cannot be updated synchronously. There will be problems.
The exact data is cleaned up here. Clearing the cache in a large area is also costly in terms of performance.
This is full clearing.
insert image description here

This is precision cleaning

insert image description here

2.SpringCache

Introduction
insert image description here

Common annotations and functions of SpringCache
insert image description here

Quick Start
The @EnableCaching annotation should be added to the startup class to enable the cache framework
insert image description here

@CachePut annotation
Cache method return value, cache one or more pieces of data
insert image description here

@CacheEvict annotation
delete cache
insert image description here

@Cacheable annotation
First check whether Spring has cached the current data, and return it directly if it has been cached.
If there is no cache, it will be cached directly in memory
insert image description here

Some special cases, condition attribute and unless attribute
insert image description here

In the past, the cache container that comes with SpringCache is used. The performance is definitely not as good as that of Redis.
So now we start to introduce Redis as a SpringCache cache product
and switch to Redis as a cache product.

SpringCache-Redis
insert image description here

import jar package
insert image description here

Just inject the corresponding cache product Manager, here we take RedisManager as an example
insert image description here

Use SpringCache-Redis to cache package data
insert image description here

The @EnableCaching annotation should be added to the startup class to enable the cache framework
insert image description here

The pitfall when adding annotations
This is equivalent to getting the attributes in Setmeal from Return, but the data at the time of Return is the Setmeal data encapsulated by Result, which obviously cannot be serialized. Here, the serialization of the Result class is also required.
insert image description here

Inherit the serialization class to make it serializable
insert image description here

At this point, the optimization of the cache is completed. At this time, if there is a cache of the current value name in the cache, it will be returned automatically. If not, it will be queried. The time for the current cache to automatically expire is configured in detail in yml

Save package method cache optimization
Once the package is saved, the corresponding cache has to be deleted, because the data needs to be re-acquired after the data is updated
, and the package is updated for the same reason as above

delete method to add
insert image description here

The save method should also be added
insert image description here

Guess you like

Origin blog.csdn.net/u011624157/article/details/128682950