springboot integrates redis cache

Adding redis cache using springBoot needs to be introduced in the POM file

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>

We need two dependencies to add cache support, one is SpringBoot's internal cache configuration, and the other is our redis cache.

After the configuration of the Redis database
dependency is added, we need to configure our local redis database to connect to the project. We open the application-local.properties configuration file and add the configuration content shown in Figure 8 below:

#redis
spring.redis.cluster.nodes=172.0.0.1:6379
spring.redis.pool.max-active=20
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.max-wait=10
spring.redis.timeout=5000

1、简易方式br/>@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

//生成key br/>@Override
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
br/>@Override
public Object generate(Object target, Method method, Object... objects) {
return MumsInfoConstant.REDIS_KEY;
}
};
}
}

Check out the Spring cache annotations @Cacheable, @CacheEvict, @CachePut usage reference
(
https://www.cnblogs.com/fashflying/p/6908028.html )
// serviceImpl method @Cacheable("infos") br/> @Override
@Cacheable("infos")
public List<User> queryUser() {
return this.UserMapper.queryUser();
}

As above, you only need to add an annotation to find it in the cache when calling the query. If not, execute the method and then store it in the cache.

When performing addition, deletion and modification, the cache needs to be deleted as follows: br/>@Override
@CacheEvict("infos")
public void editUserStatus(Map<String, Object> info) {
UserMapper.editStatus(info);
}
In the corresponding Add an annotation to the method so that the cache will be deleted

Just test it at last.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324732042&siteId=291194637