SpringとEHCacheを利用して結果をキャッシュします.
言葉を述べる
Spring 1.1.1から、EHCacheはキャッシュソリューションとしてSpringに統合されました.
デモブロックの例を示します.方法を返した結果をキャッシュします.
Spring IoCでEHCacheを配置します.
SpringにEHCacheを配置するのは簡単です.EHCacheを設定するためのehcache.xmlファイルが必要です.
このブロックはorg.aopalliance.intercept.MethodIntercepterインターフェースを実現します.一旦運転したら、ブロックされた方法がキャッシュできるように構成されているかどうかを確認します.これはキャッシュしたいbean方法を選択的に構成することができる.呼び出された方法がキャッシュ可能に構成されている限り、スクリーンセーバはこの方法のためにcache keyを生成し、この方法の結果がキャッシュされているかどうかを確認する.キャッシュされた場合は、キャッシュの結果を返します.ブロックされた方法を再起動し、キャッシュされた結果は次の呼び出しになります.
org.taha.interceptor.MethodCacheInterceptor
デフォルトの条件では、すべての方法の戻り結果がキャッシュされています.
キャッシュエリアはIoCを利用して形成されます.
cacheKeyの生成には、方法パラメータの要因も含まれている.
MethodCacheInterceptorを使う
以下はMethodCacheInterceptorをどのように配置するかを抜粋しました.
夏新著『Hibernate開発ガイド』では、EHCacheの配置ファイルをこう説明しています.
Spring 1.1.1から、EHCacheはキャッシュソリューションとしてSpringに統合されました.
デモブロックの例を示します.方法を返した結果をキャッシュします.
Spring IoCでEHCacheを配置します.
SpringにEHCacheを配置するのは簡単です.EHCacheを設定するためのehcache.xmlファイルが必要です.
< ehcache >
<! — .data 。
Java , 。
:
user.home –
user.dir –
java.io.tmpdir – -->
< diskStore path = " java.io.tmpdir " />
<! — 。CacheManager 。
defaultCache :
maxInMemory - 。
eternal - ( : ) 。 ,
。
timeToIdleSeconds - 。
, 。
( : , )。 0 。
timeToLiveSeconds - 。 。
。
overflowToDisk - maxInMemory
。
-->
< cache name = " org.taha.cache.METHOD_CACHE "
maxElementsInMemory = " 300 "
eternal = " false "
timeToIdleSeconds = " 500 "
timeToLiveSeconds = " 500 "
overflowToDisk = " true "
/>
</ ehcache >
スクリーンショットは「org.taha.cache.METHOD_」を使用します.CACHE"エリアキャッシュ方法は結果を返します.このエリアはSpring IoCを利用してbeanにアクセスさせます.
< bean id = " cacheManager " class = " org.springframework.cache.ehcache.EhCacheManagerFactoryBean " >
< property name = " configLocation " >
< value > classpath:ehcache.xml </ value >
</ property >
</ bean >
< bean id = " methodCache " class = " org.springframework.cache.ehcache.EhCacheFactoryBean " >
< property name = " cacheManager " >
< ref local = " cacheManager " />
</ property >
< property name = " cacheName " >
< value > org.taha.cache.METHOD_CACHE </ value >
</ property >
</ bean >
私たちのMethodCacheIntercepterを構築します.このブロックはorg.aopalliance.intercept.MethodIntercepterインターフェースを実現します.一旦運転したら、ブロックされた方法がキャッシュできるように構成されているかどうかを確認します.これはキャッシュしたいbean方法を選択的に構成することができる.呼び出された方法がキャッシュ可能に構成されている限り、スクリーンセーバはこの方法のためにcache keyを生成し、この方法の結果がキャッシュされているかどうかを確認する.キャッシュされた場合は、キャッシュの結果を返します.ブロックされた方法を再起動し、キャッシュされた結果は次の呼び出しになります.
org.taha.interceptor.MethodCacheInterceptor
package org.taha.interceptor;
import java.io.Serializable;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
/** */ /**
* @author <a href="Omar''''>mailto:[email protected]">Omar Irbouh</a>
* @since 2004.10.07
*/
public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean {
private static final Log logger = LogFactory.getLog(MethodCacheInterceptor. class );
private Cache cache;
/** */ /**
*
*/
public void setCache(Cache cache) {
this .cache = cache;
}
/** */ /**
* 。
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, " A cache is required. Use setCache(Cache) to provide one. " );
}
/** */ /**
*
*
* (serializable)
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
Object result;
logger.debug( " looking for method result in cache " );
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null ) {
// call target/sub-interceptor
logger.debug( " calling intercepted method " );
result = invocation.proceed();
// cache method result
logger.debug( " caching result " );
element = new Element(cacheKey, (Serializable) result);
cache.put(element);
}
return element.getValue();
}
/** */ /**
* creates cache key: targetName.methodName.argument0.argument1
*/
private String getCacheKey(String targetName,
String methodName,
Object[] arguments) {
StringBuffer sb = new StringBuffer();
sb.append(targetName)
.append( " . " ).append(methodName);
if ((arguments != null ) && (arguments.length != 0 )) {
for ( int i = 0 ; i < arguments.length; i ++ ) {
sb.append( " . " )
.append(arguments[i]);
}
}
return sb.toString();
}
}
MethodCacheInterceptorコードは説明しました.デフォルトの条件では、すべての方法の戻り結果がキャッシュされています.
キャッシュエリアはIoCを利用して形成されます.
cacheKeyの生成には、方法パラメータの要因も含まれている.
MethodCacheInterceptorを使う
以下はMethodCacheInterceptorをどのように配置するかを抜粋しました.
< bean id = " methodCacheInterceptor " class = " org.taha.interceptor.MethodCacheInterceptor " >
< property name = " cache " >
< ref local = " methodCache " />
</ property >
</ bean >
< bean id = " methodCachePointCut " class = " org.springframework.aop.support.RegexpMethodPointcutAdvisor " >
< property name = " advice " >
< ref local = " methodCacheInterceptor " />
</ property >
< property name = " patterns " >
< list >
< value > . * methodOne </ value >
< value > . * methodTwo </ value >
</ list >
</ property >
</ bean >
< bean id = " myBean " class = " org.springframework.aop.framework.ProxyFactoryBean " >
< property name = " target " >
< bean class = " org.taha.beans.MyBean " />
</ property >
< property name = " interceptorNames " >
< list >
< value > methodCachePointCut </ value >
</ list >
</ property >
</ bean >
訳注夏新著『Hibernate開発ガイド』では、EHCacheの配置ファイルをこう説明しています.
< ehcache >
< diskStore path = " java.io.tmpdir " />
< defaultCache
maxElementsInMemory = " 10000 " // Cache
eternal = " false " // Cache
timeToIdleSeconds = " 120 " //
timeToLiveSeconds = " 120 " //
overflowToDisk = " true " // ,
/>
</ ehcache >