Ehcache學習(七)執行個體一

來源:互聯網
上載者:User

Ehcache使用用例(一)

Singleton建立方式

Ehcache1.2版本之後,都可以使用singleton(工廠建立方法)去建立一個singleton的CacheManager執行個體。

CacheManager.create();

String[] cacheNames = CacheManager.getInstance().getCacheNames();

使用預設配置建立CacheManager

CacheManager manager = new CacheManager();

String[] cacheNames = manager.getCacheNames();

使用設定檔建立指定的CacheManager

CacheManager manager1 = new CacheManager(“src/config/ehcache1.xml”);

CacheManager manager2 = new CacheManager(“src/config/ehcache2.xml”);

String[] cacheNamesForManager1 = manager1.getCacheNames();

String[] cacheNamesForManager2 = manager2.getCacheNames();

設定檔載入方式

· CacheManager manager = new CacheManager();會在classpath路徑下找ehcache.xml設定檔。

· CacheManager manager = new CacheManager(“src/config.ehcache.xml”); 也可以根據相對檔案路徑來載入設定檔.

· 通過URL載入

URL url = getClass().getResource(“/anotherconfigurationname.xml”);

CacheManager manager = new CacheManager(url);

· 通過流載入

InputSream fis = new FileInputStream(new File(“src/config/ehcache.xml”).getAbsolutePath());

Try {

CacheManager manager = new CacheManager(fis);

} finally {

Fis.close();

}

編碼實現添加和緩衝

Ehcache中不僅可以用設定檔來配置緩衝,而在代碼中也可以實現同樣的功能。

CacheManager singletonManager = CacheManager.create();

Cache memoryOnlyCache = new Cache(“testCache”, 50000, false, false, 8, 2);

Cache test = singletonManager.getCache(“testCache”);

刪除只需要調用singletonManager.removeCache(“testCache”);

Shotdown CacheManager

在使用完Ehcache後,必須要shutdown緩衝。Ehcache中有自己的關閉機制,不過最好在你的代碼中顯示調用CacheManager.getInstance().shutdown();

http://blog.csdn.net/mgoann/archive/2009/04/20/4095155.aspx

聯繫我們

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