Java Ehcache緩存框架入門級使用實(shí)例
前言
JAVA緩存實(shí)現(xiàn)方案有很多,最基本的自己使用Map去構(gòu)建緩存,或者使用memcached或Redis,但是上述兩種緩存框架都要搭建服務(wù)器,而Map自行構(gòu)建的緩存可能沒有很高的使用效率,那么我們可以嘗試一下使用Ehcache緩存框架。
Ehcache主要基于內(nèi)存緩存,磁盤緩存為輔的,使用起來方便。下面介紹如何在項(xiàng)目中使用Ehcache
入門使用教程
1.maven引用
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.4</version> </dependency>
2.在classpath下建立一個ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--timeToIdleSeconds 當(dāng)緩存閑置n秒后銷毀 -->
<!--timeToLiveSeconds 當(dāng)緩存存活n秒后銷毀 -->
<!--
緩存配置
name:緩存名稱。
maxElementsInMemory:緩存最大個數(shù)。
eternal:對象是否永久有效,一但設(shè)置了,timeout將不起作用。
timeToIdleSeconds:設(shè)置對象在失效前的允許閑置時間(單位:秒)。僅當(dāng)eternal=false對象不是永久有效時使用,可選屬性,默認(rèn)值是0,也就是可閑置時間無窮大。
timeToLiveSeconds:設(shè)置對象在失效前允許存活時間(單位:秒)。最大時間介于創(chuàng)建時間和失效時間之間。僅當(dāng)eternal=false對象不是永久有效時使用,默認(rèn)是0.,也就是對象存活時間無窮大。
overflowToDisk:當(dāng)內(nèi)存中對象數(shù)量達(dá)到maxElementsInMemory時,Ehcache將會對象寫到磁盤中。
diskSpoolBufferSizeMB:這個參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認(rèn)是30MB。每個Cache都應(yīng)該有自己的一個緩沖區(qū)。
maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)。
diskPersistent:是否緩存虛擬機(jī)重啟期數(shù)據(jù) Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盤失效線程運(yùn)行時間間隔,默認(rèn)是120秒。
memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存。默認(rèn)策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進(jìn)先出)或是LFU(較少使用)。
clearOnFlush:內(nèi)存數(shù)量最大時是否清除。
-->
<!-- 磁盤緩存位置 -->
<diskStore path="java.io.tmpdir/easylink-mall-web/ehcache"/>
<!-- 默認(rèn)緩存 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- 商戶申請數(shù)據(jù)緩存 數(shù)據(jù)緩存40分鐘 -->
<cache
name="merchant-apply-cache"
eternal="false"
timeToIdleSeconds="2400"
timeToLiveSeconds="2400"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>
3.與spring的cacheManager結(jié)合使用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 支持緩存注解 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- 默認(rèn)是cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
<!-- cache管理器配置 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true" />
</bean>
</beans>
4.代碼使用
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.easylink.mall.entity.Merchant;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/spring.xml")
public class EhcacheTest {
@Autowired
private CacheManager cacheManager;
@Test
public void execute() {
// 獲取商戶申請緩存容器
Cache cache = cacheManager.getCache("merchant-apply-cache");
Merchant merchant = new Merchant();
Long id = IdWorker.getId();
merchant.setId(id);
merchant.setName("緩存測試");
// 將商戶申請數(shù)據(jù)添加至緩存中 // key : id value : object
cache.put(id, merchant);
// 獲取商戶申請數(shù)據(jù)
// 方法1
Merchant cacheMerchant1 = (Merchant) cache.get(id).get();
System.out.println(cacheMerchant1.getName());
// 方法2
Merchant cacheMerchant2 = cache.get(id, Merchant.class);
System.out.println(cacheMerchant2.getName());
// 將商戶申請數(shù)據(jù)從緩存中移除
cache.evict(id);
}
}
5.注意事項(xiàng)
cache.get(key) 和cache.get(key, class);方法,由于不知道你存入的key是什么類型,所以get的時候不會做key的類型檢查,如上述例子中
Long id = IdWorker.getId(); cache.put(id, merchant); Merchant cacheMerchant2 = cache.get(id, Merchant.class);
put進(jìn)去時的key是Long類型的,get的時候也只能傳入對應(yīng)Long類型的key才能獲取到對應(yīng)的value,如果傳入的是String類型的key,即使兩個key的值是一致的,也會導(dǎo)致無法獲取到對應(yīng)的value。這個情況很容易發(fā)生在對request請求的參數(shù),由于是String字符串類型,但是忘了做類型轉(zhuǎn)換就直接把這個String當(dāng)做key去獲取對應(yīng)的value。導(dǎo)致獲取不到,請同學(xué)們要注意,親身經(jīng)歷,血與淚的教訓(xùn)。
總結(jié)
以上就是我自己總結(jié)的Ehcache入門級用法,Ehcache是個不錯的內(nèi)存緩存框架,如果沒使用過的話,可以嘗試使用。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java substring(a)與substring(a,b)的使用說明
這篇文章主要介紹了java substring(a)與substring(a,b)的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
SpringSecurity導(dǎo)致SpringBoot跨域失效的問題解決
本文主要介紹了SpringSecurity導(dǎo)致SpringBoot跨域失效的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
vue數(shù)據(jù)響應(yīng)式原理重寫函數(shù)實(shí)現(xiàn)數(shù)組響應(yīng)式監(jiān)聽
Vue的通過數(shù)據(jù)劫持的方式實(shí)現(xiàn)數(shù)據(jù)的雙向綁定,即使用Object.defineProperty()來實(shí)現(xiàn)對屬性的劫持,但是Object.defineProperty()中的setter是無法直接實(shí)現(xiàn)數(shù)組中值的改變的劫持行為的,需要的朋友可以參考下2023-05-05
java編程之單元測試(Junit)實(shí)例分析(附實(shí)例源碼)
這篇文章主要介紹了java編程之單元測試(Junit),結(jié)合實(shí)例形式較為詳細(xì)的分析總結(jié)了Java單元測試的原理、步驟及相關(guān)注意事項(xiàng),并附帶了完整代碼供讀者下載參考,需要的朋友可以參考下2015-11-11
詳解Java?List中五種常見實(shí)現(xiàn)類的使用
Java中提供了非常多的使用的List實(shí)現(xiàn)類,本文將重點(diǎn)介紹一下常見的五種實(shí)現(xiàn)類以及他們的應(yīng)用場景,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
idea2020.3配置maven環(huán)境并配置Tomcat的詳細(xì)教程
這篇文章主要介紹了idea2020.3配置maven環(huán)境并配置Tomcat的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
淺談Java中浮點(diǎn)型數(shù)據(jù)保留兩位小數(shù)的四種方法
今天在進(jìn)行開發(fā)的過程中遇到了一個小問題,是關(guān)于如何將double類型的數(shù)據(jù)保留兩位小數(shù)。具有一定的參考價值,本文就詳細(xì)的介紹一下2021-09-09
Assert.assertNotNull()斷言是否是空問題
這篇文章主要介紹了Assert.assertNotNull()斷言是否是空問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

