通常配置ehcache分布式,都是在xml中配置的。例如:
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="port=40001,socketTimeoutMillis=2000"/> <!--缓存成员发现工厂,管理cacheManager对象 --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=32"/> <cache name="allWindowsParticulars" maxElementsInMemory="3000" eternal="false" timeToIdleSeconds="7200" timeToLiveSeconds="7200" overflowToDisk="false" overflowToOffHeap="false" diskPersistent="false" memoryStoreEvictionPolicy="LRU"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=false" /> <!-- 服务器(Tomcat)启动就同步其他服务器(Tomcat)中的缓存--> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache>
如果cache节点是通过代码创建的,该怎么配置呐?解决办法如下:
CacheConfiguration config = cacheManager.getConfiguration().getDefaultCacheConfiguration(); CacheConfiguration.CacheEventListenerFactoryConfiguration cacheEventListenerFactoryConfiguration=new CacheConfiguration.CacheEventListenerFactoryConfiguration(); cacheEventListenerFactoryConfiguration.setClass("net.sf.ehcache.distribution.RMICacheReplicatorFactory"); cacheEventListenerFactoryConfiguration.setProperties("replicateAsynchronously=true,replicatePuts=true,replicateUpdates=true," + "replicateUpdatesViaCopy=false,replicateRemovals=true"); config.addCacheEventListenerFactory(cacheEventListenerFactoryConfiguration); CacheConfiguration.BootstrapCacheLoaderFactoryConfiguration bootstrapCacheLoaderFactoryConfiguration = new CacheConfiguration.BootstrapCacheLoaderFactoryConfiguration(); bootstrapCacheLoaderFactoryConfiguration.setClass("net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"); config.addBootstrapCacheLoaderFactory(bootstrapCacheLoaderFactoryConfiguration); new Cache(config);