spring缓存使用

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

缓存注解有:

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

在这里插入图片描述

在这里插入图片描述
@Caching使用
@Caching(
cacheable={
@Cacheable( value="",key="" )
},
put ={
@CachePut(value="",key="")
}
)

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

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

第三步:使用缓存注解