接上一节。
@CachePut:既调用方法,又更新缓存。
使用场景:当更改了数据库的某个数据,同时也更新缓存。
运行时机:先调用目标方法,然后将结果放入缓存。
packagecom.gong.springbootcache.controller;importcom.gong.springbootcache.bean.Employee;importcom.gong.springbootcache.service.EmployeeService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cache.annotation.CachePut;importorg.springframework.cache.annotation.Cacheable;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;
@Controllerpublic classEmployeeController {
@Autowired
EmployeeService employeeService;//value:指定缓存的名字,每个缓存组件有一个唯一的名字。缓存组件由CacheManager进行管理。//key: