版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15071263/article/details/84316946
Springboot 2.0.x 集成Redis缓存
1、引入Redis缓存依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2、配置Redis 数据库
spring:
redis:
host: localhost
database: 0
port: 6379
jedis:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1ms
password:
ssl: false
3、配置Redis CacheManager
@Component
public class CacheConfig {
@Bean
CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
// 设置缓存有效期20分钟
.entryTtl(Duration.ofMinutes(20));
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
}
4、开启缓存
@EnableCaching
5、使用缓存
// value 可以随便写什么值,根据需要而定
// key生成策略可以根据需求来定,如果是常规全局缓存,可以写一个可识别的固定值
@Cacheable(value = CacheConfig.COMMON,key = "'DBEC90C227604576B66E8BA64C09A0D9'")