EhCache是一個比較成熟的Java緩存框架,最先從hibernate發展而來, 是進程中的緩存系統,它提供了用內存,磁盤文件存儲,以及分佈式存儲方式等多種靈活的cache管理方案,快速簡單。html
Springboot對ehcache的使用很是支持,因此在Springboot中只需作些配置就可以使用,且使用方式也簡易。java
在你的項目上配置如下幾步便可使用spring
pom.xml加依賴;緩存
<!-- Ehcache 座標 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
建立ehcache.xml配置文件app
位置:classpath目錄下,即src/main/resources/ehcache.xml框架
內容:分佈式
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!--磁盤存儲路徑--> <diskStore path="java.io.tmpdir"/> <!--defaultCache:echcache的默認緩存策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <!--accessToken緩存策略:自定義緩存策略--> <cache name="accessToken" maxElementsInMemory="10000" overflowToDisk="false" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache> </ehcache>
說明:測試
一、<diskStore path=
"java.io.tmpdir"
/>
spa
磁盤存儲路徑,當內存緩存滿了的時候,就會往這裏面放,java.io.tmdir是操做系統緩存的臨時目錄,不一樣操做系統緩存目錄不同。操作系統
二、maxElementsInMemory:內存緩存中最多能夠存放的元素數量,若放入Cache中的元素超過這個數值,則有如下兩種狀況:
1)若overflowToDisk=true,則會將Cache中多出的元素放入磁盤文件中;
2)若overflowToDisk=false,則根據memoryStoreEvictionPolicy策略替換Cache中原有的元素
三、overflowToDisk:內存不足時,是否啓用磁盤緩存
四、eternal 緩存中對象是否永久有效
五、timeToIdleSeconds 緩存數據在失效前的容許閒置時間(單位:秒),僅當eternal=false時使用,默認值是0表示可閒置時間無窮大,若超過這個時間沒有訪問此Cache中的某個元素,那麼此元素將被從Cache中清除
六、timeToLiveSeconds 緩存數據的總的存活時間(單位:秒),僅當eternal=false時使用,從建立開始計時,失效結束。
七、maxElementsOnDisk 磁盤緩存中最多能夠存放的元素數量,0表示無窮大
八、diskExpiryThreadIntervalSeconds 磁盤緩存的清理線程運行間隔,默認是120秒
九、memoryStoreEvictionPolicy 內存存儲與釋放策略,即達到maxElementsInMemory限制時,Ehcache會根據指定策略清理內存 共有三種策略,分別爲LRU(最近最少使用)、LFU(最經常使用的)、FIFO(先進先出)
下面說說他們之間的關係:
eternal很少說,true表示緩存永久有效,false表示不爲永久有效
主要是timeToLiveSeconds 和timeToIdleSeconds 之間的使用(單獨配置時,以上已說明)
舉例說明1:timeToLiveSeconds =3600 timeToIdleSeconds =300
以上配置表明緩存有效時間爲3600秒(自緩存創建起一個小時有效 ),在有效的一個小時內,若是連續五分鐘未訪問緩存,則緩存失效,特別說明的是,就算緩存訪問從未間斷,到一個小時後,緩存也會失效
舉例說明2:timeToLiveSeconds =0 timeToIdleSeconds =300
以上配置表明緩存有效時間爲永遠有效,若是連續五分鐘未訪問緩存,則緩存失效。特別說明的是,若是緩存訪問從未間斷,該緩存會一直有效
另外,defaultCache是默認緩存方式,cache是自定義的緩存方式,自行設置name。這裏設置爲accessToken
在Springboot配置文件中把ehcache.xml配置進去;即在application.properties中加入如下配置代碼
spring.cache.ehcache.config=ehcache.xml
第三步結束,ehcache在Springboot中就配置完成了,下面就是怎麼在Springboot中使用。
在啓動類前加上@EnableCaching註解;這樣的話,啓動類啓動時會去啓動緩存啓動器。
@SpringBootApplication @EnableCaching public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
實現緩存方法
@Component public class TokenCache { public static final String CACHE_NAME = "accessToken"; //生成token存入緩存 @Cacheable(value = CACHE_NAME, key = "#token") public String setCache(String token) { System.out.println("set token:將token放入緩存"); return token; } //判斷token是否存在緩存。不存在返回checkfail;存在返回token,並自動刷新閒置時間 @Cacheable(value = CACHE_NAME, key = "#token") public String checkToken(String token) { System.out.println("check fail:token驗證失敗"); return "checkfail"; } //清空token緩存 @CacheEvict(value = CACHE_NAME, allEntries=true) public void cleanAllToken() { System.out.println("clean all token:清空全部token"); } }
CACHE_NAME 爲上面配置時的緩存規則名稱,須要先聲明一下
在方法setCache裏面,作各類邏輯處理。緩存數據以kv形式存, 這裏測試以value爲key,以及value
第一次調用時候返回的值就會保存在緩存裏。
這裏爲了更直觀的體現出來,能夠第一次調用setCache方法,把數據放到緩存裏,第二次調用tokencheck方法,參數傳上面的token值,返回的不是checkfail 而是setCache方法return的值
cleanAllToken:用於清空全部緩存。系統能夠作個定時清空緩存,防止惡意訪問致使的token佔用
使用緩存方法
@Autowired private TokenCache tokenCache;//緩存方法類
新建一個token緩存
tokenCache.setCache(accessToken);
token緩存驗證
String token = tokenCache.checkToken(accessToken.toString()); if(token.equals("checkfail")){ //過時:攔截並終止請求 }else{ //token正確,自動刷新閒置時間,轉發請求 }
參考:https://blog.csdn.net/wuerwei1/article/details/80604142
https://www.cnblogs.com/xzmiyx/p/9897623.html