Ehcache 整合Spring 使用頁面、對象緩存

Ehcache在很多項目中都出現過,用法也比較簡單。一般的加些配置就可以了,而且Ehcache可以對頁面、對象、數據進行緩存,同時支持集羣/分佈式緩存。如果整合Spring、Hibernate也非常的簡單,Spring對Ehcache的支持也非常好。EHCache支持內存和磁盤的緩存,支持LRU、LFU和FIFO多種淘汰算法,支持分佈式的Cache,可以作爲Hibernate的緩存插件。同時它也能提供基於Filter的Cache,該Filter可以緩存響應的內容並採用Gzip壓縮提高響應速度。

一、準備工作

如果你的系統中已經成功加入Spring、Hibernate;那麼你就可以進入下面Ehcache的準備工作。

1、 下載jar包

Ehcache 對象、數據緩存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache-core-2.5.2-distribution.tar.gz

Web頁面緩存:http://ehcache.org/downloads/destination?name=ehcache-web-2.0.4-distribution.tar.gz&bucket=tcdistributions&file=ehcache-web-2.0.4-distribution.tar.gz

2、 需要添加如下jar包到lib目錄下

ehcache-core-2.5.2.jar

ehcache-web-2.0.4.jar 主要針對頁面緩存

3、 當前工程的src目錄中加入配置文件

ehcache.xml

ehcache.xsd

這些配置文件在ehcache-core這個jar包中可以找到

二、Ehcache基本用法

CacheManager cacheManager = CacheManager.create();
// 或者
cacheManager = CacheManager.getInstance();
// 或者
cacheManager = CacheManager.create(“/config/ehcache.xml”);
// 或者
cacheManager = CacheManager.create(“http://localhost:8080/test/ehcache.xml“);
cacheManager = CacheManager.newInstance(“/config/ehcache.xml”);
// …….

// 獲取ehcache配置文件中的一個cache
Cache sample = cacheManager.getCache(“sample”);
// 獲取頁面緩存
BlockingCache cache = new BlockingCache(cacheManager.getEhcache(“SimplePageCachingFilter”));
// 添加數據到緩存中
Element element = new Element(“key”, “val”);
sample.put(element);
// 獲取緩存中的對象,注意添加到cache中對象要序列化 實現Serializable接口
Element result = sample.get(“key”);
// 刪除緩存
sample.remove(“key”);
sample.removeAll();

// 獲取緩存管理器中的緩存配置名稱
for (String cacheName : cacheManager.getCacheNames()) {
System.out.println(cacheName);
}
// 獲取所有的緩存對象
for (Object key : cache.getKeys()) {
System.out.println(key);
}

// 得到緩存中的對象數
cache.getSize();
// 得到緩存對象佔用內存的大小
cache.getMemoryStoreSize();
// 得到緩存讀取的命中次數
cache.getStatistics().getCacheHits();
// 得到緩存讀取的錯失次數
cache.getStatistics().getCacheMisses();

三、頁面緩存

頁面緩存主要用Filter過濾器對請求的url進行過濾,如果該url在緩存中出現。那麼頁面數據就從緩存對象中獲取,並以gzip壓縮後返回。其速度是沒有壓縮緩存時速度的3-5倍,效率相當之高!其中頁面緩存的過濾器有CachingFilter,一般要擴展filter或是自定義Filter都繼承該CachingFilter。

CachingFilter功能可以對HTTP響應的內容進行緩存。這種方式緩存數據的粒度比較粗,例如緩存整張頁面。它的優點是使用簡單、效率高,缺點是不夠靈活,可重用程度不高。

EHCache使用SimplePageCachingFilter類實現Filter緩存。該類繼承自CachingFilter,有默認產生cache key的calculateKey()方法,該方法使用HTTP請求的URI和查詢條件來組成key。也可以自己實現一個Filter,同樣繼承CachingFilter類,然後覆寫calculateKey()方法,生成自定義的key。

CachingFilter輸出的數據會根據瀏覽器發送的Accept-Encoding頭信息進行Gzip壓縮。

在使用Gzip壓縮時,需注意兩個問題:

  1. Filter在進行Gzip壓縮時,採用系統默認編碼,對於使用GBK編碼的中文網頁來說,需要將操作系統的語言設置爲:zh_CN.GBK,否則會出現亂碼的問題。

  2. 默認情況下CachingFilter會根據瀏覽器發送的請求頭部所包含的Accept-Encoding參數值來判斷是否進行Gzip壓縮。雖然IE6/7瀏覽器是支持Gzip壓縮的,但是在發送請求的時候卻不帶該參數。爲了對IE6/7也能進行Gzip壓縮,可以通過繼承CachingFilter,實現自己的Filter,然後在具體的實現中覆寫方法acceptsGzipEncoding。

具體實現參考:

protected boolean acceptsGzipEncoding(HttpServletRequest request) {

boolean ie6 = headerContains(request, “User-Agent”, “MSIE 6.0”);

boolean ie7 = headerContains(request, “User-Agent”, “MSIE 7.0”);

return acceptsEncoding(request, “gzip”) || ie6 || ie7;

}

在ehcache.xml中加入如下配置

發佈了86 篇原創文章 · 獲贊 15 · 訪問量 71萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章