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


@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 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM