用到緩存,主要是用來解決并發問題的。
其中ehcache是一個純Java的過程中緩存實作Hibernate2.1,Spring都支援EHcache嵌入。
本文主要寫Spring中引入ehcache而不是用hibernate.
ehcache部署起來很簡單,主要分兩步:
1.首先要給他寫個核心配置XML檔案
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="cache1"
maxElementsInMemory="10000"
eternal="false" maxElementsOnDisk="1000"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>
屬性解釋:
簡單配置,在ehcache.xml檔案中有此配置,在使用Ehcache前最好将其删除掉,自己配置。
緩存名cache1,記憶體中最多可緩存10000個Element,其中的element會在閑置5分鐘或是存活10分鐘之後失效。
超過10000element時,element将會輸出到磁盤中,輸出路徑是java.io.tmpdir。
從其他文章找到其詳細解釋:
· Cache配置
· name:Cache的唯一辨別
· maxElementsInMemory:記憶體中最大緩存對象數。
· maxElementsOnDisk:磁盤中最大緩存對象數,若是0表示無窮大。
· eternal:Element是否永久有效,一但設定了,timeout将不起作用。
· overflowToDisk:配置此屬性,當記憶體中Element數量達到maxElementsInMemory時,Ehcache将會Element寫到磁盤中。
· timeToIdleSeconds:設定Element在失效前的允許閑置時間。僅當element不是永久有效時使用,可選屬性,預設值是0,也就是可閑置時間無窮大。
· timeToLiveSeconds:設定Element在失效前允許存活時間。最大時間介于建立時間和失效時間之間。僅當element不是永久有效時使用,預設是0.,也就是element存活時間無窮大。
· diskPersistent:是否緩存虛拟機重新開機期資料。(這個虛拟機是指什麼虛拟機一直沒看明白是什麼,有高人還希望能指點一二)。
· diskExpiryThreadIntervalSeconds:磁盤失效線程運作時間間隔,預設是120秒。
· diskSpoolBufferSizeMB:這個參數設定DiskStore(磁盤緩存)的緩存區大小。預設是30MB。每個Cache都應該有自己的一個緩沖區。
· memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache将會根據指定的政策去清理記憶體。預設政策是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用)。這裡比較遺憾,Ehcache并沒有提供一個使用者定制政策的接口,僅僅支援三種指定政策,感覺做的不夠理想。
2.實際要緩存的類調用
寫一個執行個體類,這樣大家就明白差不多了:
import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class Demo {
static CacheManager manager= new CacheManager();
/**
*##############################################################################
*
* @DESCRIBE
* @param args
* @throws InterruptedException
*
*##############################################################################
*/
public static void main(String[] args) throws InterruptedException {
String[] cacheNames = manager.getCacheNames();
System.out.println("讀取的緩存清單為:");
for(int i=0;i<cacheNames.length;i++){
System.out.println("-- "+(i+1)+" "+cacheNames[i]);
}
Cache cache = manager.getCache("cache1");
Element element = new Element("key1", "value1");
cache.put(element);
element = cache.get("key1");
Serializable value = element.getValue();
System.out.println("序列化後的值為:"+value.toString());
element = cache.get("key1");
Object value1 = element.getObjectValue();
System.out.println("未序列化的值為:"+value1.toString());
int elementsInMemory = cache.getSize();
System.out.println("得到緩存的對象數量:"+elementsInMemory);
long elementsInMemory1 = cache.getMemoryStoreSize();
System.out.println("得到緩存對象占用記憶體的數量:"+elementsInMemory1);
long elementsInMemory2 = cache.getDiskStoreSize();
System.out.println("得到緩存對對象占用磁盤的數量:"+elementsInMemory2);
int hits = cache.getHitCount();
System.out.println("得到緩存讀取的命中次數:"+hits);
int hits1 = cache.getMemoryStoreHitCount();
System.out.println("得到記憶體中緩存讀取的命中次數:"+hits1);
int hits2 =cache.getDiskStoreHitCount();
System.out.println("得到磁盤中緩存讀取的命中次數:"+hits2);
int hits3 = cache.getMissCountNotFound();
System.out.println("得到緩存讀取的丢失次數:"+hits3);
int hits4 = cache.getMissCountExpired();
System.out.println("得到緩存讀取的已經被銷毀的對象丢失次數:"+hits4);
}
}
另:附件裡需要的ehcache.jar包