spring ehcache实现应用缓存

本文详细介绍了ehcache缓存的核心配置、Spring框架中ehcache的整合方式及应用,包括配置ehcache.xml、applicationContext.xml,实现缓存管理、缓存策略设置以及通过拦截器实现方法级缓存,确保了系统的高效性和响应速度。

一、依赖包:

ehcache-core

二、ehcache.xml配置文件:

<ehcache>
     <cache name="webCache"
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="60"
            overflowToDisk="false"
            memoryStoreEvictionPolicy="LFU"
            />
</ehcache>
三、applicationContext.xml配置文件:

<bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
          <property name="configLocation" value="classpath:ehcache/ehcache-app.xml" />
    </bean>
     
    <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
	      <property name="cacheManager">
	        	<ref local="defaultCacheManager"/>
	      </property>
	      <property name="cacheName" value="webCache" />
    </bean>
    <bean id="methodCacheInterceptor" class="com.csair.uservice.interceptor.MethodCacheInterceptor">
    	<property name="cache">
    		<ref local="ehCache"/>
    	</property>
    </bean>
    <bean id="methodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    	<property name="advice">
    		<ref local="methodCacheInterceptor"/>
    	</property>
    	<property name="patterns">
    		<list>
    			<value>com.csair.uservice.service.impl.NoticeServiceImpl.getNoticeDTOPage</value>
    		</list>
    	</property>
    </bean>
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    	<property name="interceptorNames">
    		<list>
    			<value>methodPointcutAdvisor</value>
    		</list>
    	</property>
    	<property name="beanNames">
    		<list>
    			<value>noticeService</value>
    		</list>
    	</property>
    </bean>
四、拦截器:

public class MethodCacheInterceptor implements MethodInterceptor,
		InitializingBean {
	private static Logger log = Logger.getLogger(MethodCacheInterceptor.class);
	
	private Cache cache;

	@Override
	public void afterPropertiesSet() throws Exception {
		log.info("afterPropertiesSet");
	}

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		String targetName = invocation.getThis().getClass().getName();
		String methodName = invocation.getMethod().getName();
		Object result = null;
		String cacheKey = getCacheKey(targetName, methodName);
		Element element = null;
		synchronized(this) {
			element = cache.get(cacheKey);
			if(element == null) {
				log.info("加入到缓存: " + cache.getName());
				result = invocation.proceed();
				element = new Element(cacheKey, (Serializable)result);
				cache.put(element);
			} else {
				log.info("使用缓存: " + cache.getName());
			}
		}
		return element.getObjectValue();
	}

	private String getCacheKey(String targetName, String methodName) {
		StringBuffer sb = new StringBuffer();
		sb.append(targetName).append(".").append(methodName);
		return sb.toString();
	}

	public void setCache(Cache cache) {
		this.cache = cache;
	}
	
}
五、被缓存的对象都要序列化


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值