一、ehcache簡單介紹
EhCache 是一個純Java的程序内緩存架構,具有快速、精幹等特點,Ehcache是一種廣泛使用的開 源Java分布式緩存。主要面向通用緩存,Java EE和輕量級容器。它具有記憶體和磁盤存儲,緩存加載器,緩存擴充,緩存異常處理程式等特點。
優點:
1. 快速 、簡單 、擁有多種緩存政策。
2. 緩存資料有兩級:記憶體和磁盤,是以無需擔心容量問題
3. 緩存資料會在虛拟機重新開機的過程中寫入磁盤
4. 具有緩存和緩存管理器的偵聽接口 。支援多緩存管理器執行個體,以及一個執行個體的多個緩存區域 等。
缺點:
1. 使用磁盤Cache的時候非常占用磁盤空間:這是因為DiskCache的算法簡單,該算法簡單也導緻Cache的效率非常高。它隻是對元素直接追加存儲。是以搜尋元素的時候非常的快。如果使用DiskCache的,在很頻繁的應用中,很快磁盤會滿。
2. 不能保證資料的安全:當突然kill掉java的時候,可能會産生沖突,EhCache的解決方法是如果檔案沖突了,則重建cache。這對于Cache 資料需要儲存的時候可能不利。當然,Cache隻是簡單的加速,而不能保證資料的安全。
二、Spring整合ehcache
1、相關jar包依賴(Spring相關jar包自己引用不在此展示)
<!-- ehcache 相關依賴 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.2</version>
</dependency>
2、添加ehcache配置檔案ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="es">
<diskStore path="java.io.tmpdir"/>
<!--timeToIdleSeconds 當緩存閑置n秒後銷毀 -->
<!--timeToLiveSeconds 當緩存存活n秒後銷毀 -->
<!--
緩存配置
name:緩存名稱。
maxElementsInMemory:緩存最大個數。
eternal:對象是否永久有效,一但設定了,timeout将不起作用。
timeToIdleSeconds:設定對象在失效前的允許閑置時間(機關:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,預設值是0,也就是可閑置時間無窮大。
timeToLiveSeconds:設定對象在失效前允許存活時間(機關:秒)。最大時間介于建立時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,預設是0.,也就是對象存活時間無窮大。
overflowToDisk:當記憶體中對象數量達到maxElementsInMemory時,Ehcache将會對象寫到磁盤中。
diskSpoolBufferSizeMB:這個參數設定DiskStore(磁盤緩存)的緩存區大小。預設是30MB。每個Cache都應該有自己的一個緩沖區。
maxElementsOnDisk:硬碟最大緩存個數。
diskPersistent:是否緩存虛拟機重新開機期資料 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盤失效線程運作時間間隔,預設是120秒。
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache将會根據指定的政策去清理記憶體。預設政策是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用)。
clearOnFlush:記憶體數量最大時是否清除。
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<!-- test 資料緩存5分鐘 -->
<cache
name="test-cache"
eternal="false"
timeToLiveSeconds="60"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>
這裡我設定了test-cache政策,一分鐘過期。
cache元素相關屬性我已經在上面的xml裡寫的很清楚了,請廣大讀者仔細閱讀。
3.建立一個applicationContext-ehcache.xml(名稱可可随意更改)
<?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" />
<!-- 預設是cacheManager -->
<bean id="cacheManager" name= "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/ehcache.xml"/><!-- ehcache.xml的路徑 -->
<property name="shared" value="true" />
</bean> </beans>
4、applicationContext.xml中引入上面的xml(名稱可随意更改)
<import resource="classpath:applicationContext-ehcache.xml" />
三、測試
public class CacheTest extends BaseTest{
//擷取cacheManager執行個體
@Resource
private CacheManager cacheManager;
@Test
public void putCache() throws InterruptedException {
System.out.println("cacheManager = " +cacheManager);
//擷取配置檔案中的緩存
Cache cache = cacheManager.getCache("test-cache");
//存值,key-value
cache.put("test", 1);
Thread.sleep(10000);//自行設定,測試過期與否
//取值,如果已過期,則取不出值
System.out.println(cache.get("test",Integer.class));
}
}
輸出結果:
緩存沒有過期(如下圖)
緩存已過期(如下圖)
以上就是Spring整合ehcache實作緩存功能的示例,限于本人水準有限,發現問題還請不吝賜教!