進程內緩存框架EhCache
進程內緩存框架EhCache
進程內緩存框架 EhCache
• Author: Poechant
• Blog: blog.CSDN.net/Poechant
• Email: zhongchao.ustc#gmail.com (#->@)
• Date: February 27th, 2012
1. What the hell is EhCache?
EhCache 是一個純Java的進程內緩存框架,具有快速、精幹等特點,是Hibernate中默認的CacheProvider。EhCache 具有以下特點(摘自開源中國社區)。
• 快速
• 簡單
• 多種緩存策略
• 緩存數據有兩級:內存和磁碟,因此無需擔心容量問題
• 緩存數據會在虛擬機重啟的過程中寫入磁碟
• 可以通過 RMI、可插入 API 等方式進行分散式緩存
• 具有緩存和緩存管理器的偵聽介面
• 支持多緩存管理器實例,以及一個實例的多個緩存區域
• 提供 Hibernate 的緩存實現
2. 常用 API
構造函數
方法原型public Cache(String name,
int maxElementsInMemory,
boolean overflowToDisk,
boolean eternal,
long timeToLiveSeconds,
long timeToIdleSeconds)該構造函數是 EhCache 1.0 版本中的出現的,後續開發到 2.0 之後,出現了帶有 CacheConfiguration 參數的構造函數,用法更強大。比如如下這個:public Cache(CacheConfiguration cacheConfiguration,
RegisteredEventListeners registeredEventListeners,
BootstrapCacheLoader bootstrapCacheLoader)用法可參見 EhCache 的 API Docs。
參數含義
• name - 緩存的名稱,default`保留為默認緩存名稱;
• maxElementsInMemory - 內存中的最大同時緩存元素個數;
• overflowToDisk - 是否持久化(使用磁碟);
• eternal - 對象是否永久有效(永不過期);
• timeToLiveSeconds - 對象從其創建開始計算的生存時間(秒);
• timeToIdleSeconds - 對象從其最後一次被訪問開始計算的生存時間(秒)。
一般來說,只有 name、maxElementsInMemory 和 timeToLiveSeconds 顯式設置,其他的參數常根據其不同的類型而設置為 false、0 等。
put 方法
方法原型
public final void put(Element element)
throws IllegalArgumentException,
IllegalStateException,
CacheException參數含義
• element:所要存儲的元素,可參見其定義 Element API Docs。它是 EhCache 中緩存的基本單元。
get 方法
方法原型
public final Element get(Object key)
throws IllegalStateException,
CacheException參數含義
• key:可以為任意類型的 key,比較靈活。
remove 方法
方法原型
public final boolean remove(Object key)
throws IllegalStateException參數含義
• key:指定的 key。
3. 示例
源碼package com.sinosuperman.ehcache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
public class Test {
public static void main(String[] args) {
String name = "Namespace";
int capacity = 500;
int refreshPeriod = 5000;
// Initialize EhCache
Cache cache = new Cache(name, capacity, false, false, refreshPeriod, 0);
cache.initialise();
System.out.println(
"Initialize EhCache: " +
" name: " + name +
" capacity: " + capacity +
" expire: " + refreshPeriod
);
// Set data into EhCache
String key1 = "Key";
String value1 = "Value";
Element element1 = new Element(key1, value1);
cache.put(element1);
System.out.println("Set (" + key1 + ", " + value1 + ") into EhCache.");
// Get data from EhCache
Element element2 = cache.get(key1);
String key2 = (String) element2.getObjectKey();
String value2 = (String) element2.getObjectValue();
System.out.println("Get (" + key2 + ", " + value2 + ") from EhCache.");
// Remove data from EhCache
if (cache.remove(key2)) {
System.out.println("Remove data with key = " + key2 + " successfully.");
}
// Get EhCache size
System.out.println("EhCache size is " + cache.getSize());
}
}結果
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Initialize EhCache: name: Namespace capacity: 500 expire: 5000
Set (Key, Value) into EhCache.
Get (Key, Value) from EhCache.
Remove data with key = Key successfully.
EhCache size is 0
《解決方案》
謝謝分享