@Caching :制定多個緩存規則
@Cacheable 和 @CachePut 同時標注時 ,@CachePut導致還是會走方法跟數據庫交互
//根據lastName查,並將結果緩存,並且可以用於下次通過id或者email查詢 (因為有Cacheput注解,所有根據lastName查的方法還是會執行) @Caching( cacheable = {@Cacheable(/*value = "emp",*/key = "#lastName")}, put = {@CachePut(/*value = "emp",*/key = "#result.id"),@CachePut(/*value = "emp",*/key = "#result.email")} ) public Employee getEmpByLastName(String lastName){ System.out.println("根據...."+lastName+"查詢員工"); return employeeMapper.getEmpByLastName(lastName); }
另外:
@CacheConfig :抽取緩存公共配置,可以標注在類上
如:
@CacheConfig(cacheNames = "emp") @Service public class EmployeeService