ehcache調用說明,ehcache調用

來源:互聯網
上載者:User

ehcache調用說明,ehcache調用
ehcache簡介

ehcache是非常優秀的、輕量級的、本機快取方案,它可以解決大並發情況下靜態資源的快速儲存與訪問。

引入ehcache jar包

這裡引入最新版本的ehcache jar包:

<dependency>    <groupId>net.sf.ehcache</groupId>    <artifactId>ehcache</artifactId>    <version>2.10.0</version>    <type>pom</type></dependency>     
基本概念

1. CacheManager
CacheManager是緩衝的管理器,使用ehcache必需建立一個CacheManager,它的建立方式有兩種:
a. 單例模式:

private static volatile CacheManager cacheManager = CacheManager.create()

b. ehcache 2.x以後允許多個cacheManager存在,可以通過.xml配置新的cacheManager,如下demo-ehcache.xml

<ehcache name="DemoCacheManager"></ehcache>

通過.xml檔案初始化非單例模式的cacheManager,如下:

    private static volatile CacheManager cacheManager = CacheManager.newInstance(this.class.getClassLoader().getResourceAsStream("demo-ehcache.xml"));

2. cache(緩衝)
一個CacheManager可以管理多個緩衝,每個緩衝在記憶體中都有其自己的屬性,例如緩衝元素(Element)的最大個數、緩衝在記憶體中的最長存活時間、緩衝的淘汰演算法等。一個cache可以儲存多個緩衝元素(Element)。緩衝在使用前都要進行註冊,註冊有兩種方式:
1. 通過xml設定檔註冊。設定檔可配置緩衝的名字,緩衝在堆中的最大個數,緩衝的淘汰演算法等,參數非常之多,下面是一個最簡單的例子:

<ehcache>    <cache name="demo"           maxEntriesLocalHeap="10000"           eternal="false"           timeToIdleSeconds="300"           timeToLiveSeconds="600"           memoryStoreEvictionPolicy="LFU"            >    </cache></ehcache>

上面配置了一個緩衝名為demo的緩衝,它在記憶體中的最大個數為10000個,最長存活時間timeToLiveSeconds為10分鐘(600s),最長可訪問時間timeToIdleSeconds為5分鐘(300s)。當個數超過最大個數限制時採用LFU演算法進行淘汰。eternal表示緩衝是否永久不失效,當這個值為true時,上面的最大存活時間和最長可訪問時間無效。
2. 通過代碼動態註冊
下面是一個例子,與通過xml檔案註冊效果一樣:

  Cache newMemCache = new Cache("demo",          1000,         // 記憶體中最大元素個數          MemoryStoreEvictionPolicy.LFU,   // 淘汰策略          false,                 // 是否存到disk          "/data/appdatas",                    // 存在disk的位置          fale,   // 是否永久有效          600,    // timeToLiveSeconds          300,    // timeToIdleSeconds          false,  // 是否在JVM重啟的時候,是否儲存到disk          60 * 60,  // 多長時間跑一次disk逾時的線程(這個參數沒用)          null    // 註冊事件  );  cacheManager.addCache(newMemCache);

3. Element緩衝元素
緩衝元素是儲存資料的最小單位,由其ElementKey和對象組成,如下定義:

Element element = new Element(elementkey, object);

注意:這裡的object可以為任意對象!!

調用說明

1. add操作
往緩衝中添加Element,如下:

  Cache cache = cacheManager.getCache("demo");  if (cache != null) {      Element element = new Element("demoElementKey", "demo object");      cache.put(element);  }

2. get操作
從緩衝中取元素

  Cache cache = cacheManager.getCache("demo");  if (null != cache) {      Element element = cache.get("demoElementKey");  }
具體例子

具體例子請參考我寫的ehcahce-client

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.