欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合redis使用緩存注解詳解

 更新時間:2024年01月15日 09:48:10   作者:Mu_Mu是一只小白  
這篇文章主要介紹了SpringBoot整合redis使用緩存注解詳解,@Cacheable在方法執(zhí)行前判斷對應緩存是否存在,如果存在直接返回緩存結(jié)果,否者執(zhí)行方法將結(jié)果緩存,適用于查詢類,需要的朋友可以參考下

1.啟動類標明@EnableCaching

@SpringBootApplication
@MapperScan("com.jx.luckyDraw.mapper")
@EnableCaching
public class LuckyDrawApplication {

    public static void main(String[] args) {
        SpringApplication.run(LuckyDrawApplication.class, args);
    }



}

2.常用注解的種類

@Cacheable

@CachePut

@CacheEvict

2.1 作用

  • @Cacheable:在方法執(zhí)行前判斷對應緩存是否存在,如果存在直接返回緩存結(jié)果,否者執(zhí)行方法將結(jié)果緩存,適用于查詢類。
  • @CachePut:與@Cacheable不同的是@CachePut一定會執(zhí)行方法,并將方法的返回值更新到緩存,適用于更新,插入。
  • @CacheEvict:清除緩存。

2.2 例子

@Cacheable

  @Cacheable(cacheNames = "drawDetails", key = "#userId + ':' + #batchId", unless = "#result ==null")
    public DrawDetailPO getDrawDetails(String userId, Long batchId) {

當getDrawDetails方法的返回值不為null時,將方法的執(zhí)行結(jié)果按照#userId + ‘:’ + #batchId 的方式緩存到redis中。

redis中鍵名為:

drawDetails::81466011bd2a7cf40502a08827038390:1490935513660657664

@CacheEvict

 @CacheEvict(value = {"drawBatch", "drawDetails"}, allEntries = true, condition = "#result > 0")
    @Override
    public int newDrawBatchInfo(Integer batchCount) {

當newDrawBatchInfo方法的返回值大于0時,將命名空間為drawBatch" 或者drawDetails的鍵全部刪除。

allEntries 默認為false,當有多個鍵時必須配置true才能刪除。

在這種沒指定key,使用默認keyGenerator 時,必須使用allEntries =true才能刪除

  @Cacheable(cacheNames = "employeeSelectList", unless = "#result ==null")
    public List<HrmEmployeeSelectVO> querySelectList(HrmEmployeeSelectVO employeeSelectVO) {
        return employeeMapper.querySelectList(employeeSelectVO);
    }
@CacheEvict(value = "employeeSelectList",allEntries = true,condition = "#result = true ")

beforeInvocation 屬性:是否在方法執(zhí)行前刪除,默認為false。

到此這篇關(guān)于SpringBoot整合redis使用緩存注解詳解的文章就介紹到這了,更多相關(guān)SpringBoot整合redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論