缓存-注解在缓存方法上

org.springframework.cache.annotation.Cacheable、

缓存保存、更新、 失效时间

org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration

org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration

@Cacheable
注解方法,用于获取缓存值, 如果没有缓存,则执行方法获取值并缓存起来, 否则直接取缓存值;

@CachePut
新值替换缓存中旧值

@CacheEvict
移除缓存

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-caching-provider-simple

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。