spring ehcache實現應用緩存

一、依賴包:

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;
	}
	
}
五、被緩存的對象都要序列化


發佈了110 篇原創文章 · 獲贊 6 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章