spring缓存使用

Spring缓存默认使用 ConcurrentMapCacheManager创建的ConcurrentMapCache缓存,将数据保存在ConcurrentHashMap中

缓存注解有:

注解 描述
Cache 缓存接口,定义缓存操作,实现有RedisCache、EhCache、ConcurrentMapCache等
CacheManager 缓存管理器,管理各种缓存组件
@Cacheable 先查看缓存是否有这个KEY,如果有,直接取缓存,,如果没有,才执行方法
@CachePut 先执行方法,再更新缓存。key可以使用result的值
@CacheEvict 清空缓存,删除数据时,对应缓存也被清除
keyGenerator 缓存时key生成策略
serialize 缓存数据时value序列化策略
@CacheConfig 放在类上,定义公共的属性。比如定义了cacheNames,方法上就不用再定义value

spring缓存使用

spring缓存使用
@Caching使用
@Caching(
cacheable={
@Cacheable( value="",key="" )
},
put ={
@CachePut(value="",key="")
}
)

在springboot中使用spring缓存步骤如下:
第一步:导入spring-boot-starter-cache模块

第二步:在主程序上添加@EnableCaching开启注解

第三步:使用缓存注解