Spring緩存注解@Cacheable @CacheEvit @CachePut使用介紹
Spring在3.1版本,就提供了一條基于注解的緩存策略,實際使用起來還是很絲滑的,本文將針對幾個常用的注解進行簡單的介紹說明,有需要的小伙伴可以嘗試一下
本文主要知識點:
@Cacheable: 緩存存在,則使用緩存;不存在,則執行方法,并將結果塞入緩存 @CacheEvit: 失效緩存 @CachePut: 更新緩存I. 項目環境1. 項目依賴本項目借助SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA + redis5.0進行開發開一個web服務用于測試
<dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency></dependencies>
全程使用默認配置,redis本機,端口6379,無密碼
II. 緩存注解介紹1. @Cacheable這個注解用于修飾方法or類,當我們訪問它修飾的方法時,優先從緩存中獲取,若緩存中存在,則直接獲取緩存的值;緩存不存在時,執行方法,并將結果寫入緩存這個注解,有兩個比較核心的設置
/** * 與 cacheNames 效果等價 */ @AliasFor('cacheNames') String[] value() default {}; @AliasFor('value') String[] cacheNames() default {}; /** * 緩存key */ String key() default '';
cacheNames可以理解為緩存key的前綴,可以為組件緩存的key變量;當key不設置時,使用方法參數來初始化,注意key為SpEL表達式,因此如果要寫字符串時,用單引號括起來
一個簡單的使用姿勢
/** * 首先從緩存中查,查到之后,直接返回緩存數據;否則執行方法,并將結果緩存 * <p> * redisKey: cacheNames + key 組合而成 --> 支持SpEL * redisValue: 返回結果 * * @param name * @return */@Cacheable(cacheNames = 'say', key = '’p_’+ #name')public String sayHello(String name) { return 'hello+' + name + '-->' + UUID.randomUUID().toString();}
如我們傳參為 yihuihui, 那么緩存key為 say::p_yihuihui
除了上面三個配置值之外,查看@Cacheable注解源碼的童鞋可以看到還有condition設置,這個表示當它設置的條件達成時,才寫入緩存
/** * 滿足condition條件的才寫入緩存 * * @param age * @return */@Cacheable(cacheNames = 'condition', key = '#age', condition = '#age % 2 == 0')public String setByCondition(int age) { return 'condition:' + age + '-->' + UUID.randomUUID().toString();}
上面這個case中,age為偶數的時候,才走緩存;否則不寫緩存接下來是unless參數,從名字上可以看出它表示不滿足條件時才寫入緩存
/** * unless, 不滿足條件才寫入緩存 * * @param age * @return */@Cacheable(cacheNames = 'unless', key = '#age', unless = '#age % 2 == 0')public String setUnless(int age) { return 'unless:' + age + '-->' + UUID.randomUUID().toString();}2. @CachePut
不管緩存有沒有,都將方法的返回結果寫入緩存;適用于緩存更新
/** * 不管緩存有沒有,都寫入緩存 * * @param age * @return */@CachePut(cacheNames = 't4', key = '#age')public String cachePut(int age) { return 't4:' + age + '-->' + UUID.randomUUID().toString();}3. @CacheEvict
這個就是我們理解的刪除緩存
/** * 失效緩存 * * @param name * @return */@CacheEvict(cacheNames = 'say', key = '’p_’+ #name')public String evict(String name) { return 'evict+' + name + '-->' + UUID.randomUUID().toString();}4. @Caching
在實際的工作中,經常會遇到一個數據變動,更新多個緩存的場景,對于這個場景,可以通過@Caching來實現
/** * caching實現組合,添加緩存,并失效其他的緩存 * * @param age * @return */@Caching(cacheable = @Cacheable(cacheNames = 'caching', key = '#age'), evict = @CacheEvict(cacheNames = 't4', key = '#age'))public String caching(int age) { return 'caching: ' + age + '-->' + UUID.randomUUID().toString();}
上面這個就是組合操作
從 caching::age緩存取數據,不存在時執行方法并寫入緩存; 失效緩存 t4::age5. 異常時,緩存會怎樣?上面的幾個case,都是正常的場景,當方法拋出異常時,這個緩存表現會怎樣?
/** * 用于測試異常時,是否會寫入緩存 * * @param age * @return */@Cacheable(cacheNames = 'exception', key = '#age')@Cacheable(cacheNames = 'say', key = '’p_yihuihui’')public int exception(int age) { return 10 / age;}
根據實測結果,當age==0時,上面兩個緩存都不會成功
6. 測試用例接下來驗證下緩存注解與上面描述的是否一致
@RestControllerpublic class IndexRest { @Autowired private BasicDemo helloService; @GetMapping(path = {'', '/'}) public String hello(String name) {return helloService.sayHello(name); }}
上面這個主要是驗證@Cacheable注解,若緩存不命中,每次返回的結果應該都不一樣,然而實際訪問時,會發現返回的都是相同的
curl http://localhost:8080/?name=yihuihui
失效緩存
@GetMapping(path = 'evict')public String evict(String name) { return helloService.evict(String.valueOf(name));}
失效緩存,需要和上面的case配合起來使用
curl http://localhost:8080/evict?name=yihuihuicurl http://localhost:8080/?name=yihuihui
剩下其他的相關測試類就比較好理解了,一并貼出對應的代碼
@GetMapping(path = 'condition')public String t1(int age) { return helloService.setByCondition(age);}@GetMapping(path = 'unless')public String t2(int age) { return helloService.setUnless(age);}@GetMapping(path = 'exception')public String exception(int age) { try {return String.valueOf(helloService.exception(age)); } catch (Exception e) {return e.getMessage(); }}@GetMapping(path = 'cachePut')public String cachePut(int age) { return helloService.cachePut(age);}7. 小結
最后管理小結一下Spring提供的幾個緩存注解
@Cacheable: 緩存存在,則從緩存取;否則執行方法,并將返回結果寫入緩存 @CacheEvit: 失效緩存 @CachePut: 更新緩存 @Caching: 都注解組合上面雖說可以滿足常見的緩存使用場景,但是有一個非常重要的點沒有說明,緩存失效時間應該怎么設置???如何給每個緩存設置不同的緩存失效時間,咱么下篇博文見,我是一灰灰,歡迎關注長草的公眾號一灰灰blog
III. 不能錯過的源碼和相關知識點0. 項目工程:https://github.com/liuyueyi/spring-boot-demo源碼:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/125-cache-ano
到此這篇關于Spring緩存注解@Cacheable @CacheEvit @CachePut使用介紹的文章就介紹到這了,更多相關Spring @Cacheable @CacheEvit @CachePut內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: