开发者学堂课程【SpringBoot 快速掌握 - 高级应用:@CachePut】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/613/detail/9292
@CachePut
@CachePut:
既调用方法,又更新缓存数据;同步更新缓存
修改了数据库的某个数据,同时更新缓存;
运行时机:
先调用目标方法
将只标玄法的结果缓存起来
测试步骤:
查询1号员工;查到的结果会放到缓存中
以后查询还是之前的结果
更新1号员工;【lastName:zhangsan;gender:θ】
将方法的返回值也放进缓存了;
*key:传入的 employee 对象值:返回的 employee 对象;
*key="#employee. id":使用传入的参数的员工 id;key=“#result. id:使用返回后的 id Qeachacle 的 key 是不能用 #result
@CachePut(value="emp",key="#result.id")
public Employee updateEmp(Employee employee){
System. out. println("updateEmp:"+emplo/ee);
emp1oyeeMapper .updateEmp(employee);
return employee;
查询1号员工?
应该是更新后的员工;
为什么是没更新前的?
因为第一步查询后放入了缓存中,1号员工在缓存中没有更新。
更新之后会给缓存中放 key
@Cach&Put(value="emp")
publicEmployeeupdateEmp(Employee employee){ employeeaper .updatemρ(emρleme); return employee;
}
}
package com. atguigu. cache. controller ;
import...
@ RestController
public class EmployeeController {
@Autowired
EmployeeService em ployeeService ;
@ GetMapping ("/emp/{id}")
publicEmployeegetEmployee (@ PathVariable ("id")Integerid){Employee employee= employeeService .getEmp(id);
return employee;
}
@ GetMapping ("/empj")
public Employee update(Employee employee){
Employee emp= emp1oyeeService .updateEmp(employee);
return emp;
}