穿透
application.yml 中将 spring.cache.redis.cache-null-values 设为 true(查询结果为 null 也进行缓存)
spring:
cache:
type: redis
redis:
cache-null-values: true
雪崩
application.yml 中通过 spring.cache.redis.time-to-live 设置过期时间(单位ms)
spring:
cache:
type: redis
redis:
time-to-live: 5000
击穿
使用 @Cacheable 注解时指定 sync 为 true,默认为 false
sync 设为 true 后,查询时会使用 synchronized 加锁
@Cacheable(sync = true)
对应源码如下: