springboot缓存注解——@Cacheable和@CacheConfig

时间:2021-07-14 20:35:43

@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