Springboot 2.0.x 整合Redis快取
阿新 • • 發佈:2018-11-27
文章目錄
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'")