Java的進程內緩衝架構:EhCache

來源:互聯網
上載者:User

標籤:

Ehcache緩衝的特點:1. 快速.2. 簡單.3. 多種緩衝策略4. 快取資料有兩級:記憶體和磁碟,因此無需擔心容量問題5. 快取資料會在虛擬機器重啟的過程中寫入磁碟6. 可以通過RMI、可插入API等方式進行分布式緩衝7. 具有緩衝和緩衝管理器的偵聽介面8. 支援多緩衝管理器執行個體,以及一個執行個體的多個快取區域9. 提供Hibernate的緩衝實現  Ehcache緩衝的使用(1) – 安裝ehcacheEhcache 的特點,是一個純Java ,過程中(也可以理解成插入式)緩衝實現,單獨安裝Ehcache ,需把ehcache-X.X.jar 和相關類庫方到classpath中。如項目已安裝了Hibernate ,則不需要做什麼,直接可以使用Ehcache 。  Ehcache緩衝的使用(2) - 產生CacheManager使用CacheManager 建立並管理Cache1.建立CacheManager有4種方式:A:使用預設設定檔建立Java代碼1.CacheManager manager = CacheManager.create();  B:使用指定設定檔建立Java代碼1.CacheManager manager = CacheManager.create("src/config/ehcache.xml");  C:從classpath中找尋設定檔並建立Java代碼1.URL url = getClass().getResource("/anothername.xml"); 2.CacheManager manager = CacheManager.create(url);  D:通過輸入資料流建立Java代碼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建立CacheCache 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    

CacheManager singletonManager = CacheManager.create(); 

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

manager.addCache(memoryOnlyCache); 

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

 

刪除cache    

CacheManager singletonManager = CacheManager.create(); 

singletonManager.removeCache("sampleCache1");      

在使用ehcache後,需要關閉  

CacheManager.getInstance().shutdown()     

caches 的使用 

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

執行crud操作    

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

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

cache.put(element);       

//update    

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

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

//This updates the entry for "key1" 

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

//get Serializable    

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

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

Serializable value = element.getValue();       

//get non serializable    

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

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

Object value = element.getObjectValue();       

//remove    

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

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

 

Java的進程內緩衝架構:EhCache

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.