org.springframework.cache.annotation.Cacheable、
缓存保存、更新、 失效时间
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
@Cacheable
注解方法,用于获取缓存值, 如果没有缓存,则执行方法获取值并缓存起来, 否则直接取缓存值;
@CachePut
新值替换缓存中旧值
@CacheEvict
移除缓存
Cache Providers 加载顺序
image.png
image.png
image.png
因此,只要注入spring-data-starter-redis就行了, 走redis
CacheAutoConfiguration 自动加载和配置, 不需要再手动设置了
image.png
@EnableAutoConfiguration
image.png
@EnableCaching 用不到的, 因为只有当需要用户自定义CacheManager的时候才能用到。用来做代码标记的。给Cache框架的扫描做标记的。
无论是定义方法, 还是CacheManager, 都必须标记的。
image.png
image.png
总结: Cache的用法很简单, 引入spring-data-starter-redis,在包含缓存方法的类中, 注解@EnableCaching(也可注解在Application上),给Cache框架作标记。
@EnableCaching 和 @EnableScheduling 都得声明注解
image.png