springboot ehcache 配置使用方法代碼詳解
EhCache是一個(gè)比較成熟的Java緩存框架,最早從hibernate發(fā)展而來, 是進(jìn)程中的緩存系統(tǒng),它提供了用內(nèi)存,磁盤文件存儲,以及分布式存儲方式等多種靈活的cache管理方案,快速簡單。
Springboot對ehcache的使用非常支持,所以在Springboot中只需做些配置就可使用,且使用方式也簡易。
下面通過本文給大家介紹springboot ehcache 配置使用方法,具體內(nèi)容如下所示:
1. pom 引入依賴
<!-- Ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
2.resources 目錄下直接放個(gè)文件ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir"/> <!--defaultCache:echcache的默認(rèn)緩存策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <!-- 菜單緩存策略 --> <cache name="menucache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache> </ehcache>
3.在Service層 方法上加上注解
@CacheEvict(value="menucache", allEntries=true) ,更新緩存
@Cacheable(key="'menu-'+#parentId",value="menucache") 讀取緩存,"'menu-'+#parentId" 通配符,也可以直接寫死字符串
menucache 對應(yīng) 上面 xmlname="menucache"
/**刪除菜單 * @param MENU_ID * @www.fhadmin.org */ @CacheEvict(value="menucache", allEntries=true) public void deleteMenuById(String MENU_ID) throws Exception{ this.cleanRedis(); menuMapper.deleteMenuById(MENU_ID); } /** * 通過ID獲取其子一級菜單 * @param parentId * @return * @www.fhadmin.org */ @Cacheable(key="'menu-'+#parentId",value="menucache") public List<Menu> listSubMenuByParentId(String parentId) throws Exception { return menuMapper.listSubMenuByParentId(parentId); }
到此這篇關(guān)于springboot ehcache 配置使用方法代碼詳解的文章就介紹到這了,更多相關(guān)springboot ehcache 配置使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用lombok注解導(dǎo)致mybatis-plus TypeHandler失效的解決
這篇文章主要介紹了使用lombok注解導(dǎo)致mybatis-plus TypeHandler失效的解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07聊聊SpringCloud中的Ribbon進(jìn)行服務(wù)調(diào)用的問題
SpringCloud-Ribbon是基于Netflix?Ribbon實(shí)現(xiàn)的一套客戶端負(fù)載均衡的工具。本文給大家介紹SpringCloud中的Ribbon進(jìn)行服務(wù)調(diào)用的問題,感興趣的朋友跟隨小編一起看看吧2022-01-01springboot 運(yùn)行 jar 包讀取外部配置文件的問題
這篇文章主要介紹了springboot 運(yùn)行 jar 包讀取外部配置文件,本文主要描述linux系統(tǒng)執(zhí)行jar包讀取jar包同級目錄的外部配置文件,主要分為兩種方法,每種方法通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07SpringBoot @SpringBootTest加速單元測試的小訣竅
這篇文章主要介紹了SpringBoot @SpringBootTest加速單元測試的小訣竅,具有很好的參考價(jià)值,對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11MybatisPlus為何可以不用@MapperScan詳解
這篇文章主要給大家介紹了關(guān)于MybatisPlus為何可以不用@MapperScan的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MybatisPlus具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-04-04