Ehcache緩存框架

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
	<diskStore path="c:\\temp" />
	<cacheManagerEventListenerFactory class="" properties="" />

	<!--
	Uncomment the following in a clustered configuration.
	-->

	<!--<cacheManagerPeerProviderFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
		properties="peerDiscovery=automatic,multicastGroupAddress=230.0.0.1,multicastGroupPort=4446,timeToLive=1"
		propertySeparator=","
	/>
	<cacheManagerPeerListenerFactory
		class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
	/>-->

	<!--
	Hibernate will use the defaultCache unless custom configurations are defined
	below for individual domain objects, collection, queries, etc.
	-->

	<defaultCache
		maxElementsInMemory="10000"
		eternal="false"
        timeToIdleSeconds="600"
		overflowToDisk="true"
	>
		<!--<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
		<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />-->
	</defaultCache>
	
	<!--
	The cache name is the same as the class name specified in your Hibernate
	mapping file.
	-->

	<cache
		name="sampleCache"
		maxElementsInMemory="5"
		maxElementsOnDisk="100" 
		eternal="false"
        timeToIdleSeconds="2" 
        timeToLiveSeconds="2"
		overflowToDisk="true"
	>
		<!--<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
		<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />-->
	</cache>
</ehcache>

 

以上ehcache.xml是ehcache的配置文件,並且存放在應用的classpath中。下面是對該XML文件中的一些元素及其屬性的相關說明:

 

<diskStore>元素:指定一個文件目錄,當EHCache把數據寫到硬盤上時,將把數據寫到這個文件目錄下。

 

<defaultCache>元素:設定緩存的默認數據過期策略。

 

<cache>元素:設定具體的命名緩存的數據過期策略。

 

<cache>元素的屬性

 

name:緩存名稱。通常爲緩存對象的類名(非嚴格標準)。

 

maxElementsInMemory:設置基於內存的緩存可存放對象的最大數目。

 

maxElementsOnDisk:設置基於硬盤的緩存可存放對象的最大數目。

 

eternal:如果爲true,表示對象永遠不會過期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認爲false;

 

timeToIdleSeconds: 設定允許對象處於空閒狀態的最長時間,以秒爲單位。當對象自從最近一次被訪問後,如果處於空閒狀態的時間超過了timeToIdleSeconds屬性值,這個對象就會過期。當對象過期,EHCache將把它從緩存中清空。只有當eternal屬性爲false,該屬性纔有效。如果該屬性值爲0,則表示對象可以無限期地處於空閒狀態。

 

timeToLiveSeconds:設定對象允許存在於緩存中的最長時間,以秒爲單位。當對象自從被存放到緩存中後,如果處於緩存中的時間超過了 timeToLiveSeconds屬性值,這個對象就會過期。當對象過期,EHCache將把它從緩存中清除。只有當eternal屬性爲false,該屬性纔有效。如果該屬性值爲0,則表示對象可以無限期地存在於緩存中。timeToLiveSeconds必須大於timeToIdleSeconds屬性,纔有意義。

 

overflowToDisk:如果爲true,表示當基於內存的緩存中的對象數目達到了maxElementsInMemory界限後,會把益出的對象寫到基於硬盤的緩存中。

使用:

CacheManager cacheManager = EhcachePlugIn.getCacheManager();
Cache cache = cacheManager.getCache("sampleCache");

 EhcachePlugIn可以自定義爲任何工廠,作用是返回一個CacheManager實例。

 

cacheManager.getCache("sampleCache");

 參數爲ehcache文件中<cache>元素的name屬性。

 

引入ehcache.xml

URL url = getClass().getResource("/"+xmlPath);
cacheManager = new CacheManager(url);

 xmlPath爲ehcache.xml在classpath下的具體路徑。

 

對象的存儲

CacheManager cacheManager = EhcachePlugIn.getCacheManager();
Cache cache = cacheManager.getCache("sampleCache");
System.out.println("The Key In Cache?:"+cache.isKeyInCache(EHCACHE_KEY));
System.out.println("Cache is :"+cache);
		
		Element result = cache.get(EHCACHE_KEY);
		
		if(null==result)
		{
			System.out.println("No Data In Ehcache");
			List list = new ArrayList();
			
			for(int i=20;i<50;i++)
			{
				Student student = new Student(26,"kook"+i);
				list.add(student);
			}
			cache.put(new Element(EHCACHE_KEY,list));
			cache.flush();
			
			result = cache.get(EHCACHE_KEY);
		}

		List ehcacheList = (List)result.getValue();
		
		Iterator iter =  ehcacheList.iterator();
		
		while (iter.hasNext()) {
			Student element = (Student) iter.next();
			System.out.println("Studeng name is:"+element.getName());
		}

 

注意:如果緩存的對象要寫入到硬盤中的話,則該對象必須實現了Serializable接口才行。

 

詳細代碼在附件中,爲一個完成的ECLIPSE PROJECT.

發佈了58 篇原創文章 · 獲贊 1 · 訪問量 3571
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章