首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Spring+Ehcache xml文件配置形式

2012-12-20 
Spring+Ehcachexml文件配置方式Srping+Ehcache 在Service层配置缓存(太懒了,不写如何配置了,就代码吧)?1.

Spring+Ehcache xml文件配置方式

Srping+Ehcache 在Service层配置缓存(太懒了,不写如何配置了,就代码吧)

?

1. ehcache 文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">    <diskStore path="java.io.tmpdir"/>    <defaultCache            maxElementsInMemory="10000"            eternal="false"            timeToIdleSeconds="520"            timeToLiveSeconds="520"            overflowToDisk="true"            maxElementsOnDisk="10000000"            diskPersistent="false"            diskExpiryThreadIntervalSeconds="120"            memoryStoreEvictionPolicy="LRU"     /> <cache name="testServiceCache"            maxElementsInMemory="10"            eternal="false"            timeToIdleSeconds="200"            timeToLiveSeconds="300"            overflowToDisk="true"            />                </ehcache>

?

2. applicationContext.xml 文件 相关代码

    <!-- Service Cache   Ehcache -->           <bean id="cacheManager" expression="execution(* com.miao.service.*.*(..))" /><aop:advisor pointcut-ref="methodCachePointCut" advice-ref="methodCacheInterceptor" /></aop:config>    <!--  也可用此种方式 指定哪些方法使用cache-->    <!--     <bean id="methodCachePointCut" name="code">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 Object invoke(MethodInvocation invocation) throws Throwable {String targetName = invocation.getThis().getClass().getName();String methodName = invocation.getMethod().getName();Object[] arguments = invocation.getArguments();Object result;String cacheKey = getCacheKey(targetName, methodName, arguments);Element element = cache.get(cacheKey);if (element == null) {result = invocation.proceed();   //调用被拦截的目标方法LOGGER.info("Set into Cache");element = new Element(cacheKey, (Serializable) result);cache.put(element);}return element.getValue();}private String getCacheKey(String targetName, String methodName,Object[] arguments) {StringBuffer sb = new StringBuffer();//拼cacheKey 这里使用 className.methodName(arg1,arg2.....argN) 作为keysb.append(targetName).append(".").append(methodName).append("(");if ((arguments != null) && (arguments.length != 0)) {for (int i = 0; i < arguments.length; i++) {sb.append(arguments[i]);if (i + 1 != arguments.length) {                    sb.append(",");                }}}sb.append(")");return sb.toString();}public void afterPropertiesSet() throws Exception {if(null == cache) {throw new IllegalArgumentException("Cache should not be null.");}}}

?

?4.使用Junit测试即可,传递相同的参数,调用service内的同一个方法,只会进入DAO一次

热点排行