oscache、ehcache緩存內容存入硬盤的方法

1.oscache將緩存內容存入硬盤:

cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener

cache.persistence.overflow.only=false

cache.path=d\:\\temp\\cache

*cache.persistence.class
指定類是被持久化的類。class必須實現PersistenceListener接口。
作爲硬盤持久,可以實現com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener接口,但是在window系統中會出現文件名異常的問題導致文件無法創建,因爲文件名是使用|串聯
解決的方法有2個:1〉自己實現PersistenceListener接口,自定義實現類;2〉修改DiskPersistenceListener類的class方法然後覆蓋jar包中的類。
注意:DiskPersistenceListener 需要設定硬盤路徑:cache.path

*cache.path
指定硬盤緩存的路徑。目錄如果不存在將被建立。同時注意oscache應該要有權限寫文件系統。
cache.path=c:\\myapp\\cache
or *ix:
cache.path=/opt/myapp/cache

*cache.persistence.overflow.only (NEW! Since 2.1)
指定是否只有在內存不足的情況下才使用硬盤緩存。
默認值false(不使用硬盤緩存)。但推薦是true如果內存cache被允許的話。這個屬性徹底的改變了cache的行爲,使得persisted cache 和memory完全不同。


oscache.properties

cache.memory=false

cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
cache.persistence.overflow.only=false
cache.path=d\:\\temp\\cache
cache.key=__oscache_no1
cache.capacity=1000

package ibatisCache;

import java.io.Reader;
import java.util.List;

import org.apache.log4j.PropertyConfigurator;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlClientMap {
	
	
	public static SqlMapClient init() throws Exception{
		Reader reader = Resources.getResourceAsReader("ibatisCache/SqlMap.xml");
		return SqlMapClientBuilder.buildSqlMapClient(reader);
	}
	
	
	public static void main(String[] args){
		try {
			PropertyConfigurator.configure("src/log4j/log4j.properties");
			SqlMapClient sqlMapClient = init();
			List list = sqlMapClient.queryForList("test.selectTest");
			for(int i = 0;i < list.size();i++){
				System.out.println(list.get(i));
			}
			
			
			list = sqlMapClient.queryForList("test.selectTest");
			for(int i = 0;i < list.size();i++){
				System.out.println("---" + list.get(i));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



2.ehcache將緩存內容存入硬盤:

<diskStore path="C:/temp"/>

<cache name="authCache1"
           maxElementsInMemory="36"
           maxElementsOnDisk="10000"
           overflowToDisk="true"

           diskPersistent="true"
           diskSpoolBufferSizeMB="20"
            />

當內存中的數據量超過了設置的最大記錄數maxElementsInMemory,則會將多餘的記錄數存入硬盤,但是內存中已經存在的內容則不會自動存入硬盤,需要手動使用cache.flush()來強制將內存內容放入硬盤中;

一般maxElementsOnDisk的值需要設置的比maxElementsInMemory大,這樣效率會高一些;

需要設置overflowToDisk爲true,表示使用硬盤緩存策略;

需要設置diskPersistent爲true,否則重啓服務後緩存文件會被清理掉,同時該屬性會在一定程度上拖慢緩存速度,因爲它需要不停地監控設置的硬盤大小是否已滿關聯屬性爲diskSpoolBufferSizeMB,如果硬盤夠大可以將該屬性設置足夠大;

最後 會生成*.index和*.data2個文件;


*重建緩存會自動從硬盤讀取數據到內存


diskStore :指定數據存儲位置,可指定磁盤中的文件夾位置

maxElementsInMemory: 在內存中緩存的element的最大數目
maxElementsOnDisk: 在磁盤上緩存的element的最大數目,默認值爲0,表示不限制。
eternal: 設定緩存的elements是否永遠不過期。如果爲true,則緩存的數據始終有效,如果爲false那麼還要根據timeToIdleSeconds,timeToLiveSeconds判斷
overflowToDisk: 如果內存中數據超過內存限制,是否要緩存到磁盤上。

以下屬性是可選的:
timeToIdleSeconds: 對象空閒時間,指對象在多長時間沒有被訪問就會失效。只對eternal爲false的有效。默認值0,表示一直可以訪問。
timeToLiveSeconds: 對象存活時間,指對象從創建到失效所需要的時間。只對eternal爲false的有效。默認值0,表示一直可以訪問。
diskPersistent: 是否在磁盤上持久化。指重啓jvm後,數據是否有效。默認爲false。
diskExpiryThreadIntervalSeconds: 對象檢測線程運行時間間隔。標識對象狀態的線程多長時間運行一次。
diskSpoolBufferSizeMB: DiskStore使用的磁盤大小,默認值30MB。每個cache使用各自的DiskStore。
memoryStoreEvictionPolicy: 如果內存中數據超過內存限制,向磁盤緩存時的策略。默認值LRU,可選FIFO、LFU。


ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">

    <diskStore path="C:/temp"/>
	
	<defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskSpoolBufferSizeMB="30"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />

            
    <cache name="authCache1"
           maxElementsInMemory="36"
           maxElementsOnDisk="10000"
           eternal="true"
           diskPersistent="true"
           overflowToDisk="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LRU"
            />

</ehcache>



package ehcache_test;

import ibatisCache.SqlClientMap;

import java.net.URL;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.log4j.PropertyConfigurator;

import com.ibatis.sqlmap.client.SqlMapClient;

public class EhcacheTest {
	
	public static void main(String[] args) throws Exception{
		PropertyConfigurator.configure("src/log4j/log4j.properties");
		/**
		 * 創建緩存管理對象
		 */
		URL url = EhcacheTest.class.getResource("ehcache.xml");
		CacheManager cacheManager = CacheManager.create(url);
		Thread.sleep(10000);
		
		/**
		 * 獲取緩存對象
		 */
		Cache cache = cacheManager.getCache("authCache1");
		/**
		 * 存入數據
		 */
		SqlMapClient sqlMapClient = SqlClientMap.init();
		List list = sqlMapClient.queryForList("test.selectTest");
		for(int i = 0;i < list.size();i++){
			Element element = new Element(i, list.get(i));
			cache.put(element);
			System.out.println(list.get(i));
		}
		
		/*強行輸出內存數據到硬盤*/
		cache.flush();
		Thread.sleep(5000);
		
		/**
		 * 關閉ehcache
		 */
		Thread.sleep(5000);
		cacheManager.shutdown();
		
		/**
		 * 取出數據
		 */
		cacheManager = CacheManager.create(url);
		Thread.sleep(10000);
		
		cache = cacheManager.getCache("authCache1");
		
		for(int i = 0;i < 36;i++){
			Element elements = cache.get(i);
			System.out.print(elements.getKey() + " ----- ");
			System.out.println(elements.getValue());
		}
		
		cacheManager.shutdown();
	}
	
}




以上是個人總結和借鑑的一些經驗,有不足之處還請指出一起討論






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章