cache optimization
Cache optimization based on Redis
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
2. SMS verification code, login optimization
Add valid time verification to the verification code, and set the valid time of the SMS verification code
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
to optimize the verification code
. Optimize
the login method after login
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
3. Cache the front-end menu data
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)
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.
This is precision cleaning
2.SpringCache
Introduction
Common annotations and functions of SpringCache
Quick Start
The @EnableCaching annotation should be added to the startup class to enable the cache framework
@CachePut annotation
Cache method return value, cache one or more pieces of data
@CacheEvict annotation
delete cache
@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
Some special cases, condition attribute and unless attribute
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
import jar package
Just inject the corresponding cache product Manager, here we take RedisManager as an example
Use SpringCache-Redis to cache package data
The @EnableCaching annotation should be added to the startup class to enable the cache framework
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.
Inherit the serialization class to make it serializable
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
The save method should also be added