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" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    	<property name="configLocation">  
        	<value>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>testServiceCache</value>
        </property>
    </bean>
    
	<!-- methodCacheInterceptor 要自己实现的代码 -->
    <bean id="methodCacheInterceptor" class="com.miao.service.MethodCacheInterceptor">
        <property name="cache">
            <ref local="methodCache"/>
        </property>
    </bean>
    
    <!-- 配置要拦截的service, 拦截service包下所有类的所有方法  -->
    <aop:config>
		<aop:pointcut id="methodCachePointCut" expression="execution(* com.miao.service.*.*(..))" />
		<aop:advisor pointcut-ref="methodCachePointCut" advice-ref="methodCacheInterceptor" />
	</aop:config>
    
	<!--  也可用此种方式 指定哪些方法使用cache-->
    <!-- 
    <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice">
            <ref local="methodCacheInterceptor"/>
        </property>
        <property name="patterns">
            <list>
                <value>.*getStepPartKitList</value>
            </list>
        </property>
    </bean>
	-->

 

3.MethodCacheInterceptor

 

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) 作为key
		sb.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一次

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值