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一次

你可能感兴趣的:(ehcache)