Python 官方文档:入门教程 => 点击学习
目录主要使用到的注解:使用步骤注解属性介绍@Cacheable() 的属性值及释义:@CachPut属性值及释义:@CachEvict属性值及释义:@Caching总结主要使用到的注
1、@EnableCaching 这个注解,标注在 SpringBoot 主启动类上,表示系统开启缓存。
@EnableCaching
@springBootApplication(scanBasePackages = CommonConstant.DEFAULT_PACKAGE_NAME)
public class PortalApp {
public static void main(String[] args) {
SpringApplication.run(PortalApp.class, args);
}
}
2、在对应需要进行缓存的方法上加入对应的注解即可;
value/cacheNames = "demoCommon";//Redis的第一层文件夹为demoCommon key="#id"; //系统自定义key值格式,相当于value下边一层 unless = "#result==null";//方法返回值结果为空时,不存入缓存;
代码示例如下:
//-- 根据id查询demo
@Cacheable(cacheNames = "demoCommon", unless = "#result==null", key="#id")
public Demo queryById(String id) {
return demoMapper.queryById(id);
}
调用上述接口,会将接口返回的数据以下图格式存入redis,接口再使用此id作为参数查询时,会直接去缓存里拿:
value/cacheNames = "demoCommon";//redis的第一层文件夹为demoCommon keyGenerator="myKeyGenerator"; //系统自定义key值格式,相当于value下边一层
调用示例如下:
@CachePut(value = "demoCommon", key=#"#demo.id")
public Demo updateById(Demo demo) {
demoMapper.updateById(demo);
return demo;
}
调用上图接口,会根据传入的id找到对应的key缓存的值,并修改缓存中的value;
value/cacheNames = "demoCommon";//redis的第一层文件夹为demoCommon key=#"#demo.id"; //系统自定义key值格式,相当于value下边一层 allEntries="true";// 是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空value/cachaeNames下边的所有缓存
@CacheEvict(value = "demoCommon",allEntries="true",key = "#demo.id")
public Demo deleteById(Demo demo) {
demoMapper.deleteById(demo);
return demo;
}
调用接口后,redis为:
接口需要使用多个注解标签,则可使用此注解;示例如下:
@Caching(put = {
@CachePut(value = "demoCommon1", key = "#demo.id")
@CachePut(value = "demoCommon2", key = "#demo.id")
@CachePut(value = "demoCommon3", key = "#demo.id")
})
public Demo updateByIdC(Demo demo) {
demoMapper.updateById(demo);
return demo;
}
到此这篇关于SpringBoot基于注解的缓存的文章就介绍到这了,更多相关SpringBoot注解的缓存内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot学习之基于注解的缓存
本文链接: https://www.lsjlt.com/news/144436.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0