ehcache 搭建

1.所需jar包:

ehcache-core-2.6.0.jar

ehcache-terracotta-2.6.0.jar (与terracotta集成)

2.EhcacheCacheManagerProxy代码如下:

package cache.ehcache;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Status;
import net.sf.ehcache.cluster.CacheCluster;
import net.sf.ehcache.cluster.ClusterNode;
import net.sf.ehcache.cluster.ClusterScheme;
import net.sf.ehcache.cluster.ClusterTopologyListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCache;
import org.springframework.cache.support.AbstractCacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.util.Assert;

public class EhcacheCacheManagerProxy extends AbstractCacheManager {

	private static final Logger LOGGER = LoggerFactory.getLogger(EhcacheCacheManagerProxy.class);
	private CacheManager cacheManager;
	private NoOpCacheManager noOpCacheManager = new NoOpCacheManager();
	private List<ClusterTopologyListener> listenerList;
	private boolean globalFlag = true;        //外部标志位
	private boolean innerFlag = true;         //内部标志位

	/**
	 * Returns the backing Ehcache {@link net.sf.ehcache.CacheManager}.
	 * @return
	 */
	public CacheManager getCacheManager() {
		return cacheManager;
	}

	/**
	 * Sets the backing EhCache {@link net.sf.ehcache.CacheManager}.
	 */
	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}

	/**
	 * 
	 * @param globalFlag
	 */
	public void setGlobalFlag(boolean globalFlag) {
		this.globalFlag = globalFlag;
	}

	public void setListenerList(List<ClusterTopologyListener> listenerList) {
		this.listenerList = listenerList;
	}

	@Override
	protected Collection<Cache> loadCaches() {
		Assert.notNull(this.cacheManager, "A backing EhCache CacheManager is required");
		Status status = this.cacheManager.getStatus();
		Assert.isTrue(Status.STATUS_ALIVE.equals(status),
				"An 'alive' EhCache CacheManager is required - current cache is " + status.toString());

		String[] names = this.cacheManager.getCacheNames();
		Collection<Cache> caches = new LinkedHashSet<Cache>(names.length);
		for (String name : names) {
			caches.add(new EhCacheCache(this.cacheManager.getEhcache(name)));
		}
		//add cluster listener
		addClusterListener();
		
		return caches;
	}

	@Override
	public Cache getCache(String name) {
		if(!globalFlag || !innerFlag){
			return noOpCacheManager.getCache(name);
		}
		Cache cache = super.getCache(name);
		if (cache == null) {
			// check the EhCache cache again
			// (in case the cache was added at runtime)
			Ehcache ehcache = this.cacheManager.getEhcache(name);
			if (ehcache != null) {
				cache = new EhCacheCache(ehcache);
				addCache(cache);
			}
		}
		return cache;
	}

	private void addClusterListener(){
		if(listenerList == null){
			listenerList = new ArrayList<ClusterTopologyListener>();
		}
		listenerList.add(new ClusterTopologyListener(){
			@Override
			public void nodeLeft(ClusterNode node) {
				LOGGER.error("cluster nodeLeft,nodeIp=" + (node == null ? "null" : node.getIp()));
				innerFlag = true;
			}
			
			@Override
			public void nodeJoined(ClusterNode node) {
				LOGGER.error("cluster nodeJoined,nodeIp=" + (node == null ? "null" : node.getIp()));
				innerFlag = true;
			}
			
			@Override
			public void clusterRejoined(ClusterNode oldNode, ClusterNode newNode) {
				LOGGER.error("cluster clusterRejoined,oldNodeIp=" + (oldNode == null ? "null" : oldNode.getIp()) + ",newNodeIp=" + (newNode == null ? "null" : newNode.getIp()));
				innerFlag = true;
			}
			
			@Override
			public void clusterOnline(ClusterNode node) {
				LOGGER.error("cluster clusterOnline,nodeIp=" + (node == null ? "null" : node.getIp()));
				innerFlag = true;
			}
			
			@Override
			public void clusterOffline(ClusterNode node) {
				LOGGER.error("cluster clusterOffline,nodeIp=" + (node == null ? "null" : node.getIp()));
				innerFlag = false;
			}
		});
		CacheCluster cluster = cacheManager.getCluster(ClusterScheme.TERRACOTTA);
		for(ClusterTopologyListener listener : listenerList){
			if(listener == null){
				continue;
			}
			cluster.addTopologyListener(listener);
		}
	}

}

3.两个配置文件如下:

spring-cache.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xsi:schemaLocation="    
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> 
 
    <!-- 指定ehcache.xml的位置 --> 
    <bean id="cacheManagerFactory" 
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
        p:configLocation="classpath:/demo/resouce/spring/cache/ehcache.xml" p:shared="true" /> 
 
    <!-- 声明缓存Manager --> 
    <bean id="ehcacheManager" class="cache.ehcache.EhcacheCacheManagerProxy" 
        p:cacheManager-ref="cacheManagerFactory" 
        p:globalFlag="true">      
    </bean>
</beans> 

ehcache.xml(cache 配置文件)

<?xml version="1.0" encoding="UTF-8"?>  
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:noNamespaceSchemaLocation="ehcache.xsd"  
         updateCheck="false" monitoring="autodetect"  
         dynamicConfig="true" name="test">  
     
    <cacheManagerEventListenerFactory class="" properties=""/>  
  
    <terracottaConfig url="ip1:端口,ip2:端口" rejoin="true"/> <!--terracotta服务器配置  -->  
    <defaultCache  
           maxEntriesLocalHeap="10000"  
           eternal="false"  
           overflowToDisk="false"  
           timeToIdleSeconds="5"  
           timeToLiveSeconds="5"
           memoryStoreEvictionPolicy="LRU"
           maxElementsInMemory="100000"
           maxElementsOnDisk="100000"
           copyOnRead="true">
           <terracotta clustered="true"> 开启集群  
           	   <nonstop immediateTimeout="false" timeoutMillis="3000">
			       <timeoutBehavior type="noop" />
			   </nonstop>
           </terracotta>
    </defaultCache>  
       
    <cache name="test1"    
            eternal="false"  
            overflowToDisk="false"  
            timeToIdleSeconds="600"  
            timeToLiveSeconds="600"  
            memoryStoreEvictionPolicy="LRU"  
            maxElementsInMemory="100000" 
            maxElementsOnDisk="100000"  
            copyOnRead="true">  
    <terracotta clustered="true">
    	<nonstop immediateTimeout="false" timeoutMillis="3000">
		    <timeoutBehavior type="noop" />
		</nonstop>
    </terracotta>    
    </cache>
    


</ehcache>  

spring-cache-test.xml (AOP配置文件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang" xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/lang
     http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
     <!-- 用户缓存配置 -->
	 <aop:config>
	   <aop:advisor advice-ref="testAdvice" pointcut="execution(* com.demo.impl.TestImpl.*(..))"/>
	</aop:config>
	<cache:advice id="testAdvice" cache-manager="ehcacheManager">
	   <cache:caching cache="test1">
	      <cache:cacheable method="getTest" key="'${test}:key'"/>
	   </cache:caching>
	</cache:advice>
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaWeb的jar包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.1.jar aspectjrt-1.6.0.jar aspectjtools-1.6.0.jar aspectjweaver-1.6.0.jar bcprov-jdk16-1.46.jar c3p0-0.9.1.2.jar cas-client-core-3.3.3.jar cglib-2.2.2.jar commons-beanutils-1.8.0.jar commons-cli-1.2.jar commons-codec-1.9.jar commons-collections-3.2.1.jar commons-dbcp-1.4.jar commons-fileupload-1.3.1.jar commons-httpclient-3.1.jar commons-io-2.4.jar commons-lang-2.6.jar commons-lang3-3.3.2.jar commons-logging-1.1.1.jar commons-net-3.5.jar commons-pool-1.6.jar DataCenter-util-0.0.1-20161202.072205-3.jar DmDialect-for-hibernate4.0-jdk1.6.jar dom4j-1.6.1.jar elasticache-java-cluster-client-1.0.61.0.jar ezmorph-1.0.6.jar freemarker-2.3.8.jar ftp-1.0.0.jar ggserver-core-1.0.jar ggserver-thematic-1.0.jar gson-1.6.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.8.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar httpclient-4.0.jar httpclient-4.1.1.jar httpcore-4.0.jar httpcore-4.1.jar httpcore-nio-4.0.jar httpmine-4.0.jar ibatis-2.3.4.726.jar jackson-all-1.7.4.jar jackson-annotation-2.3.0.jar jackson-core-2.3.3.jar jackson-databind-2.3.3.jar java_websocket.jar javassist-3.11.0.GA.jar jaxen-1.1.jar jboss-logging-3.1.4.GA.jar jboss-logging-annotations-1.2.0.Final.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar jcifs-1.3.17.jar jcl-over-slf4j-1.7.10.jar jdom-2.0.5.jar jmemcached-core-1.0.0.jar jmock-1.2.0.jar jmock-cglib-1.2.0.jar jodconverter-2.2.2.jar jodconverter-cli-2.2.2.jar json-20140107.jar json-lib-2.2.3-jdk15.jar jsqlparser-0.8.0.jar jstl-1.2.jar juh-3.0.1.jar jurt-3.0.1.jar jzlib-1.1.3.jar log4j-1.2.16.jar mongo-java-driver-3.2.2.jar mysql-connector-java-5.0.8.jar ognl-3.0.5.jar ojdbc6-1.0.jar pdfbox-app-1.6.0.jar poi-3.12.jar poi-examples-3.12.jar poi-excelant-3.12.jar poi-ooxml-3.12.jar poi-ooxml-schemas-3.12.jar poi-scratchpad-3.12.jar ridl-3.0.0.jar simple-spring-memcached-3.5.0.jar slf4j-api-1.6.0.jar slf4j-log4j12-1.6.0.jar solr-solrj-3.4.0.jar spring-aop-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-cache-3.3.0.jar spring-context-4.1.6.RELEASE.jar spring-context-support-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar spring-data-commons-1.10.0.RELEASE.jar spring-data-mongodb-1.7.0.RELEASE.jar spring-expression-4.1.6.RELEASE.jar spring-instrument-4.1.6.RELEASE.jar spring-instrument-tomcat-4.1.6.RELEASE.jar spring-jdbc-4.1.6.RELEASE.jar spring-jms-4.1.6.RELEASE.jar spring-messaging-4.1.6.RELEASE.jar spring-orm-4.1.6.RELEASE.jar spring-oxm-4.1.6.RELEASE.jar spring-security-acl-3.1.6.RELEASE.jar spring-security-aspects-3.1.6.RELEASE.jar spring-security-cas-3.1.6.RELEASE.jar spring-security-config-3.1.6.RELEASE.jar spring-security-core-3.1.6.RELEASE.jar spring-security-crypto-3.1.6.RELEASE.jar spring-security-ldap-3.1.6.RELEASE.jar spring-security-openid-3.1.6.RELEASE.jar spring-security-remoting-3.1.6.RELEASE.jar spring-security-taglibs-3.1.6.RELEASE.jar spring-security-web-3.1.6.RELEASE.jar spring-test-4.1.6.RELEASE.jar spring-tx-4.1.6.RELEASE.jar spring-web-4.1.6.RELEASE.jar spring-webmvc-4.1.6.RELEASE.jar spring-webmvc-portlet-4.1.6.RELEASE.jar spring-websocket-4.1.6.RELEASE.jar struts2-core-2.3.20.jar struts2-json-plugin-2.3.20.jar struts2-sitemesh-plugin-2.0.14.jar struts2-spring-plugin-2.3.20.jar trove4j-2.0.2.jar ueditor-1.1.1.jar unoil-3.0.1.jar userSystem-1.0.jar velocity-1.5.jar xmemcached-2.0.0.jar xmemcached-provider-3.5.0.jar xmlbeans-2.6.0.jar xom-1.1.jar xpp3_min-1.1.4c.jar xstream-1.3.1.jar xwork-core-2.3.20.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值