天天看點

Java的程序内緩存架構:EhCache

EhCache 是一個純Java的程序内緩存架構,具有快速、精幹等特點,是Hibernate中預設的CacheProvider。

Ehcache緩存的特點:

1. 快速.

2. 簡單.

3. 多種緩存政策

4. 緩存資料有兩級:記憶體和磁盤,是以無需擔心容量問題

5. 緩存資料會在虛拟機重新開機的過程中寫入磁盤

6. 可以通過RMI、可插入API等方式進行分布式緩存

7. 具有緩存和緩存管理器的偵聽接口

8. 支援多緩存管理器執行個體,以及一個執行個體的多個緩存區域

9. 提供Hibernate的緩存實作

Ehcache緩存的使用(1) – 安裝ehcache

Ehcache 的特點,是一個純Java ,過程中(也可以了解成插入式)緩存實作,單獨安裝Ehcache ,需把ehcache-X.X.jar 和相關類庫方到classpath中。如項目已安裝了Hibernate ,則不需要做什麼,直接可以使用Ehcache 。

Ehcache緩存的使用(2) - 生成CacheManager

使用CacheManager 建立并管理Cache

1.建立CacheManager有4種方式:

A:使用預設配置檔案建立

Java代碼

1.CacheManager manager = CacheManager.create(); 

B:使用指定配置檔案建立

1.CacheManager manager = CacheManager.create("src/config/ehcache.xml"); 

C:從classpath中找尋配置檔案并建立

1.URL url = getClass().getResource("/anothername.xml"); 

2.CacheManager manager = CacheManager.create(url); 

D:通過輸入流建立

1.InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath()); 

2.try { 

3.manager = CacheManager.create(fis); 

4.} finally { 

5.fis.close(); 

6.} 

Ehcache緩存的使用(3) – 解讀Ehcache配置檔案ehcache.xml

重要的參數

<diskStore path="D:/work2/renhewww/cache"/>

<cache name=" sampleCache1"

      maxElementsInMemory="1"

           maxElementsOnDisk="10000"

           eternal="false"

           overflowToDisk="true"

           diskSpoolBufferSizeMB="20"

           diskPersistent="true"

           timeToIdleSeconds="43200"

           timeToLiveSeconds="86400"

           memoryStoreEvictionPolicy="LFU"

        />

屬性解釋:

必須屬性:

        name:設定緩存的名稱,用于标志緩存,惟一

        maxElementsInMemory:在記憶體中最大的對象數量

        maxElementsOnDisk:在DiskStore中的最大對象數量,如為0,則沒有限制

        eternal:設定元素是否永久的,如果為永久,則timeout忽略

        overflowToDisk:是否當memory中的數量達到限制後,儲存到Disk

可選的屬性:

        timeToIdleSeconds:設定元素過期前的空閑時間

        timeToLiveSeconds:設定元素過期前的活動時間

        diskPersistent:是否disk store在虛拟機啟動時持久化。預設為false

   diskExpiryThreadIntervalSeconds:運作disk終結線程的時間,預設為120秒

        memoryStoreEvictionPolicy:政策關于Eviction

緩存子元素:

    cacheEventListenerFactory:注冊相應的的緩存監聽類,用于處理緩存事件,如put,remove,update,和expire

    bootstrapCacheLoaderFactory:指定相應的BootstrapCacheLoader,用于在初始化緩存,以及自動設定。

Ehcache緩存的使用(4) – 建立Cache

通過CacheManager建立Cache

Cache cache = manager.getCache("sampleCache1");

Ehcache緩存的使用(5) – 利用cache存取資料

存儲資料

Element element = new Element("key1", "value1"); 

cache.put(new Element(element); 

擷取資料

Element element = cache.get("key1");

緩存的建立,采用自動的方式  

CacheManager singletonManager = CacheManager.create(); 

singletonManager.addCache("testCache"); 

Cache test = singletonManager.getCache("testCache");       

或者直接建立Cache    

Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2); 

manager.addCache(memoryOnlyCache); 

删除cache    

singletonManager.removeCache("sampleCache1");      

在使用ehcache後,需要關閉  

CacheManager.getInstance().shutdown()     

caches 的使用 

Cache cache = manager.getCache("sampleCache1");        

執行crud操作    

Cache cache = manager.getCache("sampleCache1"); 

cache.put(element);       

//update    

cache.put(new Element("key1", "value1"); 

//This updates the entry for "key1" 

cache.put(new Element("key1", "value2");       

//get Serializable    

Element element = cache.get("key1"); 

Serializable value = element.getValue();       

//get non serializable    

Object value = element.getObjectValue();       

//remove    

Element element = new Element("key1", "value1"  cache.remove("key1");     

繼續閱讀