The Dragon Scroll

Be just and fear not.

OSCacheに関するメモ

OSCacheに関するメモ。OSCacheのキャッシングでWebアプリケーションのパフォーマンスを向上する (1/2):CodeZine(コードジン)を参考に。
ここから、OSCacheのJarをダウンロードする。

コンテンツのキャッシュ

web.xmlに指定するurl-pattern単位でキャッシュを行う。

	
	    CacheFilter
	    
	        com.opensymphony.oscache.web.filter.CacheFilter
	    
	    
	        time
	        120
	    
	    
	        scope
	        application
	    
	

	
	    CacheFilter
            /HelloPapanda
	

/HelloPapandaにマッピングされたServletをキャッシングする。
timeは、キャッシュされる期間。単位は秒。scopeは、キャッシュが有効なスコープを指定する。

部分キャッシュ

taglibによるページの部分キャッシュ。

<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>

           ・・・

	
	現在の時刻は、<%= new java.text.SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(new java.util.Date()) %>

cacheタグで囲まれた部分だけキャッシュする。timeの代わりにcron式の指定も可能。

オブジェクトのキャッシュ

オブジェクトは、getFromCacheで取り出し、putInCacheで格納する。

    Cache cache = (Cache)getServletContext().getAttribute(CACHE_OBJECT_KEY); // CACHE_OBJECT_KEY="__oscache_cache"
    String name;

    try{
        name = (String)cache.getFromCache(NAME_CACHE_KEY,30); // NAME_CACHE_KEY="name_cache_key"
    }catch(NeedsRefreshException nre){
        name = "Papanda";
        cache.putInCache(NAME_CACHE_KEY,name);
    }