純 Java 進程內緩存庫,Hibernate 二級緩存標準實現,特點:快速、輕量、依賴少、文檔完善。官網:ehcache.sourceforge.net
// 1. 單例模式(默認加載 classpath:ehcache.xml)
CacheManager singleton = CacheManager.create();
// 2. 指定配置文件路徑
CacheManager manager = new CacheManager("src/config/ehcache.xml");
// 3. 從 URL 載入
URL url = getClass().getResource("/another.xml");
CacheManager manager = new CacheManager(url);
// 4. 從 InputStream 載入
InputStream is = new FileInputStream("src/config/ehcache.xml");
CacheManager manager = new CacheManager(is); // finally 關閉流
// 獲取所有緩存名稱
String[] names = manager.getCacheNames();
// 使用默認屬性
singleton.addCache("testCache");
Cache test = singleton.getCache("testCache");
// 自定義屬性
Cache custom = new Cache("testCache", 5000, false, false, 5, 2);
manager.addCache(custom);
| 參數 | 含義 |
|---|---|
| name | 緩存名稱 |
| maxElementsInMemory | 內存最大對象數 |
| overflowToDisk | 超出內存是否溢寫磁盤 |
| eternal | true=永不過期,忽略 TTL/TTI |
| timeToIdleSeconds | 最大閒置時間(兩次訪問間隔),0=無限 |
| timeToLiveSeconds | 最大生存時間(創建到過期),0=無限 |
Cache cache = manager.getCache("sampleCache1");
// 存入
cache.put(new Element("key1", "value1"));
// 取值(序列化/非序列化)
Serializable valSer = cache.get("key1").getValue();
Object valObj = cache.get("key1").getObjectValue();
// 刪除
cache.remove("key1");
// 強制刷盤(持久化緩存)
cache.flush();
// 關閉管理器
CacheManager.getInstance().shutdown();
// 對象數量
int size = cache.getSize();
// 內存/磁盤佔用字節數
long memSize = cache.getMemoryStoreSize();
long diskSize = cache.getDiskStoreSize();
// 命中統計
int totalHits = cache.getHitCount();
int memHits = cache.getMemoryStoreHitCount();
int diskHits = cache.getDiskStoreHitCount();
// 未命中統計
int missNotFound = cache.getMissCountNotFound(); // 鍵不存在
int missExpired = cache.getMissCountExpired(); // 已過期被淘汰
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="cache1"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="2"
timeToLiveSeconds="3"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>
public class Demo {
static CacheManager manager = new CacheManager();
public static void main(String[] args) throws InterruptedException {
String[] names = manager.getCacheNames();
System.out.println("緩存列表:" + Arrays.toString(names));
Cache cache = manager.getCache("cache1");
cache.put(new Element("key1", "value1"));
Element el = cache.get("key1");
System.out.println("序列化值:" + el.getValue());
System.out.println("原始對象:" + el.getObjectValue());
System.out.println("對象數:" + cache.getSize());
System.out.println("內存佔用:" + cache.getMemoryStoreSize());
System.out.println("磁盤佔用:" + cache.getDiskStoreSize());
System.out.println("總命中:" + cache.getHitCount());
System.out.println("內存命中:" + cache.getMemoryStoreHitCount());
System.out.println("磁盤命中:" + cache.getDiskStoreHitCount());
System.out.println("未命中(不存在):" + cache.getMissCountNotFound());
System.out.println("未命中(過期):" + cache.getMissCountExpired());
}
}
diskPersistent 實現重啟預熱MissCount 比率,命中率 < 80% 需調優