24、springboot与缓存(2)
具体看上文!!!
@Service public class EmpService {@AutowiredEmployeeMapper employeeMapper;@Cacheable(cacheNames ="e")public Employee getEmp(Integer id){System.out.println("查询:" + id +"员工");Employee emp = employeeMapper.getEmpById(id);return emp;} @CachePut(value = "e")public Employee update(Employee employee){System.out.println("update。。。。");employeeMapper.updateEmp(employee);return employee;} }
@ResponseBody @RequestMapping("/emps") public Employee update(Employee employee){Employee emp = empService.update(employee);return emp; }
此时测试流程:
@CacheEvict(value = "emp",key = "#id") public void del(Integer id){System.out.println("del:"+ id);//employeeMapper.deleteEmpById(id); }
@ResponseBody @RequestMapping("/del") public String del(Integer id){empService.del(id);return "success del "; }
假设同时查询1,2两个员工,然后删除1,再去查2,依然不会调用数据库
@CacheEvict(value = "e",allEntries = true) public void del(Integer id){System.out.println("del:"+ id);//employeeMapper.deleteEmpById(id); }
假设同时查询1,2两个员工,然后删除1,再去查2,会调用数据库
@Caching() 定义复杂的缓存规则
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Caching {Cacheable[] cacheable() default {};CachePut[] put() default {};CacheEvict[] evict() default {}; }
@Select("SELECT * FROM employee WHERE lastName = #{lastName}") Employee getEmpByLastName(String lastName);
EmpService.java
@Caching(cacheable = {@Cacheable(value = "emp",key = "#lastname")},put = {@CachePut(value = "emp",key = "#result.id"),@CachePut(value = "emp",key = "#result.email")} ) //此时会将该数据的id、email、lastname数据都加载缓存中 public Employee getByName(String lastname){return employeeMapper.getEmpByLastName(lastname); }
此时可以将lastname、id、email都可以查询的到
controller.java
@ResponseBody @RequestMapping("/caching/lastname/{lastname}") public Employee Cachings(@PathVariable("lastname") String lastname){Employee emp = empService.getByName(lastname);return emp; }
此时查询id=2的用户依然会在缓存中进行查询
控制台并没有进行打印。
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface CacheConfig {String[] cacheNames() default {};String keyGenerator() default "";String cacheManager() default "";String cacheResolver() default ""; }
转载于:https://www.cnblogs.com/Mrchengs/p/10435251.html
24、springboot与缓存(2)相关推荐
- springboot中缓存技术的使用、原理及其运行流程
Springboot中缓存的工作原理 要想在springboot中使用缓存,首先要了解springboot中缓存的工作原理. 我们知道springboot在启动时会有很多的自动配置类(xxx-Auto ...
- SpringBoot @Cacheable缓存入门程序
导语 在之前的博客中分享了关于SpringBoot缓存的一些基本的概念,在这篇博客中提供一个小小的入门的实例,通过这个实例可以更好的了解关于SpringBoot缓存有关的知识点. 首先既然是缓存的 ...
- SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut
SpringBoot Redis缓存 @Cacheable.@CacheEvict.@CachePut
- 【SpringBoot整合缓存】-----spring-boot-starter-cache篇
本专栏将从基础开始,循序渐进,以实战为线索,逐步深入SpringBoot相关知识相关知识,打造完整的SpringBoot学习步骤,提升工程化编码能力和思维能力,写出高质量代码.希望大家都能够从中有所收 ...
- 【SpringBoot整合缓存】-----Redis缓存篇
本专栏将从基础开始,循序渐进,以实战为线索,逐步深入SpringBoot相关知识相关知识,打造完整的SpringBoot学习步骤,提升工程化编码能力和思维能力,写出高质量代码.希望大家都能够从中有所收 ...
- 【Springboot 入门培训 】#18 SpringBoot Cache 缓存实现
目录 1 EhCache3 1 执行环境设置 2 Ehcache3 应用 3 Ehcache3 xml设置 2 Caffeine 3 Infinispan 1 SpringBoot嵌入式缓存 2 Ja ...
- SpringBoot之缓存篇
SpringBoot与缓存 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring ...
- JAVA学习笔记24——SpringBoot
SpringBoot 微服务架构 把每个功能元素独立出来,把独立出来的功能元素动态组合,需要的功能元素才去拿来组合,需要多一些时可以聚合多个功能元素,所以微服务架构是对功能元素进行复制,而没有对整个应 ...
- SpringBoot @Cacheable缓存注解的使用
文章目录 1. 引入缓存依赖 2. application.properties 配置 3. 准备基本的Controller.Service代码 4. @Cacheable注解 4.1 cacheNa ...
最新文章
- FPGA设计心得(3)Aurora IP core 的理论学习记录
- Linux网络编程一步一步学+基础
- (原创) 电源滤波为何要用几个不同量级的电容并联?
- 概率论与数理统计思维导图_数学思维到底有多重要?这个学科往往影响国家实力...
- “拼多多优惠券”测试的套路,今天让你秒懂~
- python如何输入特殊符号_用Python打印列表中的特殊字符
- 用Python打造一款智能语音聊天小软件!
- python实现图片转字符画
- 阿里中台搞了3年,凉了?网传:副总裁玄难“背锅”,辞职创业!咸鱼放弃维护 Flutter!...
- java调用opencc将中文简体繁体转换
- 根因定位论文:Root Cause Analysis of Anomalies of Multitier Services in Public Clouds
- 简单易学的win10安装教程,值得收藏
- python汇率兑换双向_汇率兑换—python第一课
- 【学习总结】Git学习-参考廖雪峰老师教程六-分支管理
- PyTorch中的torch.clamp()实现矩阵裁剪
- 如何用java写一个年日历
- google的黑镜子 -- 思考技术元素对生活的影响
- 默纳克3000服务器改协议,默纳克3000参数设置说明
- 计算机基本原理——CPU的工作原理
- uniapp 微信小程序开启分包
热门文章
- 网站二次开发如何防止别人打包源码_面试官:你说你对Kafka比较熟?看过源码吗?...
- this.$nextTick()的使用场景
- antdesign的表单中的下拉框设置默认值BUG处理
- 理解Event的冒泡模型
- 邮件服务器之微软Exchange server (转)
- Mac终端的ruby和pods安装和使用(超干货)
- (String)、toString、String.valueOf的区别
- Tomcat7基于Redis的Session共享实战二
- 第一章:认识Redhat Linux
- Google Chrome input 设置 line-height 后光标变得和input一样高