@Caching: used to customize sophisticated caching rules
package com.gong.springbootcache.controller; import com.gong.springbootcache.bean.Employee; import com.gong.springbootcache.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.PathVariable; Import org.springframework.web.bind.annotation.RequestMapping; Import org.springframework.web.bind.annotation.ResponseBody; @Controller public class EmployeeController { @Autowired the EmployeeService the employeeService; / / value: Specifies the name of the cache, each cache component has a unique name. Cache component is managed by CacheManager. // Key: When used in the data cache key, use the default parameter value, the method returns the value 1- = Key // #id may be so expressed: # root.args [0] (first parameter) // keyGenerator: Specifies the generated assembly cache id, use the key to one or keyGenerator // CacheManager, cacheResolver: specify which referred to the cache manager, you can use one of the parameters // cache when match conditions: condition // unless: unless specified condition is when the return value of true, the method will not be cached // Sync: whether to use asynchronous mode // "+ # root.methodName '[# + ID +]'" @Cacheable (value = " EMP " ) @ResponseBody @RequestMapping ( " / EMP / {ID} " ) public the Employee the getEmp (@PathVariable (" ID " ) Integer ID) { the Employee EMP = employeeService.getEmp (ID); return EMP; } @CachePut (value = "EMP", Key = "# employee.id" ) @ResponseBody @GetMapping ( "/ EMP") public Employee updateEmp(Employee employee){ Employee emp = employeeService.updateEmp(employee); return emp; } @CacheEvict(value = "emp",key = "#id") @ResponseBody @GetMapping("/emp/del/{id}") public String deleteEmp(@PathVariable("id") Integer id){ //employeeService.deleteEmp(id); return "删除成功"; } @Caching( cacheable = { @Cacheable(value = "emp",key = "#lastName") }, put = { @CachePut(value = "emp",key = "#result.id"), @CachePut(value = "emp",key = "#result.email"), } ) @ResponseBody @RequestMapping("/emp/lastName/{lastName}") public Employee getEmpByLastName(@PathVariable("lastName") String lastName){ Employee employee = employeeService.getEmpByLastName(lastName); return employee; } }
In the execution Localhost: 8080 / emp / lastName / jack after the request, it will also be @ CachePut cache rule added to the cache.
@CacheConfig (cacheNames = "emp") marked on the class to be stored in a class all cached public properties, such as setting the name cache.