前言
緩存可以通過將經(jīng)常訪問的數(shù)據(jù)存儲在內(nèi)存中,減少底層數(shù)據(jù)源如數(shù)據(jù)庫的壓力,從而有效提高系統(tǒng)的性能和穩(wěn)定性。我想大家的項目中或多或少都有使用過,我們項目也不例外,但是最近在review公司的代碼的時候?qū)懙暮艽狼襩ow, 大致寫法如下:
publicUsergetById(Stringid){
Useruser=cache.getUser();
if(user!=null){
returnuser;
}
//從數(shù)據(jù)庫獲取
user=loadFromDB(id);
cahce.put(id,user);
returnuser;
}
其實Spring Boot 提供了強大的緩存抽象,可以輕松地向您的應用程序添加緩存。本文就講講如何使用 Spring 提供的不同緩存注解實現(xiàn)緩存的最佳實踐。
基于 Spring Boot + MyBatis Plus + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
項目地址:https://github.com/YunaiV/ruoyi-vue-pro
視頻教程:https://doc.iocoder.cn/video/
啟用緩存@EnableCaching
現(xiàn)在大部分項目都是是SpringBoot項目,我們可以在啟動類添加注解@EnableCaching來開啟緩存功能。
@SpringBootApplication
@EnableCaching
publicclassSpringCacheApp{
publicstaticvoidmain(String[]args){
SpringApplication.run(Cache.class,args);
}
}
既然要能使用緩存,就需要有一個緩存管理器Bean,默認情況下,@EnableCaching 將注冊一個ConcurrentMapCacheManager的Bean,不需要單獨的 bean 聲明。ConcurrentMapCacheManager將值存儲在ConcurrentHashMap的實例中,這是緩存機制的最簡單的線程安全實現(xiàn)。
基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
項目地址:https://github.com/YunaiV/yudao-cloud
視頻教程:https://doc.iocoder.cn/video/
自定義緩存管理器
默認的緩存管理器并不能滿足需求,因為她是存儲在jvm內(nèi)存中的,那么如何存儲到redis中呢?這時候需要添加自定義的緩存管理器。
添加依賴
org.springframework.boot spring-boot-starter-data-redis
配置Redis緩存管理器
@Configuration @EnableCaching publicclassCacheConfig{ @Bean publicRedisConnectionFactoryredisConnectionFactory(){ returnnewLettuceConnectionFactory(); } @Bean publicCacheManagercacheManager(){ RedisCacheConfigurationredisCacheConfiguration=RedisCacheConfiguration.defaultCacheConfig() .disableCachingNullValues() .serializeValuesWith(SerializationPair.fromSerializer(newGenericJackson2JsonRedisSerializer())); RedisCacheManagerredisCacheManager=RedisCacheManager.builder(redisConnectionFactory()) .cacheDefaults(redisCacheConfiguration) .build(); returnredisCacheManager; } }
現(xiàn)在有了緩存管理器以后,我們?nèi)绾卧跇I(yè)務層面操作緩存呢?
我們可以使用@Cacheable、@CachePut 或@CacheEvict 注解來操作緩存了。
@Cacheable
該注解可以將方法運行的結(jié)果進行緩存,在緩存時效內(nèi)再次調(diào)用該方法時不會調(diào)用方法本身,而是直接從緩存獲取結(jié)果并返回給調(diào)用方。

例子1:緩存數(shù)據(jù)庫查詢的結(jié)果。
@Service
publicclassMyService{
@Autowired
privateMyRepositoryrepository;
@Cacheable(value="myCache",key="#id")
publicMyEntitygetEntityById(Longid){
returnrepository.findById(id).orElse(null);
}
}
在此示例中,@Cacheable 注解用于緩存 getEntityById()方法的結(jié)果,該方法根據(jù)其 ID 從數(shù)據(jù)庫中檢索 MyEntity 對象。
但是如果我們更新數(shù)據(jù)呢?舊數(shù)據(jù)仍然在緩存中?
@CachePut
然后@CachePut 出來了, 與 @Cacheable 注解不同的是使用 @CachePut 注解標注的方法,在執(zhí)行前不會去檢查緩存中是否存在之前執(zhí)行過的結(jié)果,而是每次都會執(zhí)行該方法,并將執(zhí)行結(jié)果以鍵值對的形式寫入指定的緩存中。@CachePut 注解一般用于更新緩存數(shù)據(jù),相當于緩存使用的是寫模式中的雙寫模式。
@Service
publicclassMyService{
@Autowired
privateMyRepositoryrepository;
@CachePut(value="myCache",key="#entity.id")
publicvoidsaveEntity(MyEntityentity){
repository.save(entity);
}
}
@CacheEvict
標注了 @CacheEvict 注解的方法在被調(diào)用時,會從緩存中移除已存儲的數(shù)據(jù)。@CacheEvict 注解一般用于刪除緩存數(shù)據(jù),相當于緩存使用的是寫模式中的失效模式。

@Service
publicclassMyService{
@Autowired
privateMyRepositoryrepository;
@CacheEvict(value="myCache",key="#id")
publicvoiddeleteEntityById(Longid){
repository.deleteById(id);
}
}
@Caching
@Caching 注解用于在一個方法或者類上,同時指定多個 Spring Cache 相關(guān)的注解。

例子1:@Caching注解中的evict屬性指定在調(diào)用方法 saveEntity 時失效兩個緩存。
@Service
publicclassMyService{
@Autowired
privateMyRepositoryrepository;
@Cacheable(value="myCache",key="#id")
publicMyEntitygetEntityById(Longid){
returnrepository.findById(id).orElse(null);
}
@Caching(evict={
@CacheEvict(value="myCache",key="#entity.id"),
@CacheEvict(value="otherCache",key="#entity.id")
})
publicvoidsaveEntity(MyEntityentity){
repository.save(entity);
}
}
例子2:調(diào)用getEntityById方法時,Spring會先檢查結(jié)果是否已經(jīng)緩存在myCache緩存中。如果是,Spring 將返回緩存的結(jié)果而不是執(zhí)行該方法。如果結(jié)果尚未緩存,Spring 將執(zhí)行該方法并將結(jié)果緩存在 myCache 緩存中。方法執(zhí)行后,Spring會根據(jù)@CacheEvict注解從otherCache緩存中移除緩存結(jié)果。
@Service
publicclassMyService{
@Caching(
cacheable={
@Cacheable(value="myCache",key="#id")
},
evict={
@CacheEvict(value="otherCache",key="#id")
}
)
publicMyEntitygetEntityById(Longid){
returnrepository.findById(id).orElse(null);
}
}
例子3:當調(diào)用saveData方法時,Spring會根據(jù)@CacheEvict注解先從otherCache緩存中移除數(shù)據(jù)。然后,Spring 將執(zhí)行該方法并將結(jié)果保存到數(shù)據(jù)庫或外部 API。
方法執(zhí)行后,Spring 會根據(jù)@CachePut注解將結(jié)果添加到 myCache、myOtherCache 和 myThirdCache 緩存中。Spring 還將根據(jù)@Cacheable注解檢查結(jié)果是否已緩存在 myFourthCache 和 myFifthCache 緩存中。如果結(jié)果尚未緩存,Spring 會將結(jié)果緩存在適當?shù)木彺嬷?。如果結(jié)果已經(jīng)被緩存,Spring 將返回緩存的結(jié)果,而不是再次執(zhí)行該方法。
@Service
publicclassMyService{
@Caching(
put={
@CachePut(value="myCache",key="#result.id"),
@CachePut(value="myOtherCache",key="#result.id"),
@CachePut(value="myThirdCache",key="#result.name")
},
evict={
@CacheEvict(value="otherCache",key="#id")
},
cacheable={
@Cacheable(value="myFourthCache",key="#id"),
@Cacheable(value="myFifthCache",key="#result.id")
}
)
publicMyEntitysaveData(Longid,Stringname){
//CodetosavedatatoadatabaseorexternalAPI
MyEntityentity=newMyEntity(id,name);
returnentity;
}
}
@CacheConfig
通過@CacheConfig 注解,我們可以將一些緩存配置簡化到類級別的一個地方,這樣我們就不必多次聲明相關(guān)值:
@CacheConfig(cacheNames={"myCache"})
@Service
publicclassMyService{
@Autowired
privateMyRepositoryrepository;
@Cacheable(key="#id")
publicMyEntitygetEntityById(Longid){
returnrepository.findById(id).orElse(null);
}
@CachePut(key="#entity.id")
publicvoidsaveEntity(MyEntityentity){
repository.save(entity);
}
@CacheEvict(key="#id")
publicvoiddeleteEntityById(Longid){
repository.deleteById(id);
}
}
Condition & Unless
condition作用:指定緩存的條件(滿足什么條件才緩存),可用 SpEL 表達式(如 #id>0,表示當入?yún)?id 大于 0 時才緩存)
unless作用 : 否定緩存,即滿足 unless 指定的條件時,方法的結(jié)果不進行緩存,使用 unless 時可以在調(diào)用的方法獲取到結(jié)果之后再進行判斷(如 #result == null,表示如果結(jié)果為 null 時不緩存)
//whenid>10,the@CachePutworks.
@CachePut(key="#entity.id",condition="#entity.id>10")
publicvoidsaveEntity(MyEntityentity){
repository.save(entity);
}
//whenresult!=null,the@CachePutworks.
@CachePut(key="#id",condition="#result==null")
publicvoidsaveEntity1(MyEntityentity){
repository.save(entity);
}
清理全部緩存
通過allEntries、beforeInvocation屬性可以來清除全部緩存數(shù)據(jù),不過allEntries是方法調(diào)用后清理,beforeInvocation是方法調(diào)用前清理。
//方法調(diào)用完成之后,清理所有緩存
@CacheEvict(value="myCache",allEntries=true)
publicvoiddelectAll(){
repository.deleteAll();
}
//方法調(diào)用之前,清除所有緩存
@CacheEvict(value="myCache",beforeInvocation=true)
publicvoiddelectAll(){
repository.deleteAll();
}
SpEL表達式
Spring Cache注解中頻繁用到SpEL表達式,那么具體如何使用呢?
SpEL 表達式的語法

Spring Cache可用的變量

最佳實踐
通過Spring緩存注解可以快速優(yōu)雅地在我們項目中實現(xiàn)緩存的操作,但是在雙寫模式或者失效模式下,可能會出現(xiàn)緩存數(shù)據(jù)一致性問題(讀取到臟數(shù)據(jù)),Spring Cache 暫時沒辦法解決。最后我們再總結(jié)下Spring Cache使用的一些最佳實踐。
只緩存經(jīng)常讀取的數(shù)據(jù):緩存可以顯著提高性能,但只緩存經(jīng)常訪問的數(shù)據(jù)很重要。很少或從不訪問的緩存數(shù)據(jù)會占用寶貴的內(nèi)存資源,從而導致性能問題。
根據(jù)應用程序的特定需求選擇合適的緩存提供程序和策略。SpringBoot 支持多種緩存提供程序,包括 Ehcache、Hazelcast 和 Redis。
使用緩存時請注意潛在的線程安全問題。對緩存的并發(fā)訪問可能會導致數(shù)據(jù)不一致或不正確,因此選擇線程安全的緩存提供程序并在必要時使用適當?shù)耐綑C制非常重要。
避免過度緩存。緩存對于提高性能很有用,但過多的緩存實際上會消耗寶貴的內(nèi)存資源,從而損害性能。在緩存頻繁使用的數(shù)據(jù)和允許垃圾收集不常用的數(shù)據(jù)之間取得平衡很重要。
使用適當?shù)木彺嬷鸪霾呗?。使用緩存時,重要的是定義適當?shù)木彺嬷鸪霾呗砸源_保在必要時從緩存中刪除舊的或陳舊的數(shù)據(jù)。
使用適當?shù)木彺骀I設(shè)計。緩存鍵對于每個數(shù)據(jù)項都應該是唯一的,并且應該考慮可能影響緩存數(shù)據(jù)的任何相關(guān)參數(shù),例如用戶 ID、時間或位置。
常規(guī)數(shù)據(jù)(讀多寫少、即時性與一致性要求不高的數(shù)據(jù))完全可以使用 Spring Cache,至于寫模式下緩存數(shù)據(jù)一致性問題的解決,只要緩存數(shù)據(jù)有設(shè)置過期時間就足夠了。
特殊數(shù)據(jù)(讀多寫多、即時性與一致性要求非常高的數(shù)據(jù)),不能使用 Spring Cache,建議考慮特殊的設(shè)計(例如使用 Cancal 中間件等)。
責任編輯:彭菁
-
緩存
+關(guān)注
關(guān)注
1文章
248瀏覽量
27761 -
代碼
+關(guān)注
關(guān)注
30文章
4968瀏覽量
73988 -
數(shù)據(jù)源
+關(guān)注
關(guān)注
1文章
66瀏覽量
10088 -
SpringBoot
+關(guān)注
關(guān)注
0文章
177瀏覽量
688
原文標題:SpringBoot項目中使用緩存的正確姿勢,太優(yōu)雅了!
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
如何在XPS項目中使用SmartXplorer
labview項目中的類怎么才能夠遷移到另一個項目使用
如何在我的項目中使用停止模式?
在項目中使用SYSBIOS有好處嗎?
SpringBoot應用啟動運行run方法
在ESP-IDF項目中使用BSON有什么想法嗎?
如何在ESP-IDF項目中使用BSON ?
使用Method Swizzling遇到的問題和項目中使用的Swizzling方案
如何在SpringBoot項目中實現(xiàn)動態(tài)定時任務
如何在SpringBoot中解決Redis的緩存穿透等問題
如何正確使用SpringBoot項目中緩存Cache
Springboot項目的集成以及具體使用及配置
什么是springBoot業(yè)務組件化開發(fā)?談談SpringBoot業(yè)務組件化
SpringBoot項目中使用緩存的正確方法
評論