ehcache可以实现多个应用缓存共享,更新同步。
例如现在服务器有server1和server2两个应用,server1缓存获取key为ehcache的缓存,然后循环添加key=i,value=i的10条缓存。server2缓存添加key为ehcache的缓存并循环输出key=i的10条缓存。
server1第一次运行,获取key为ehcache的缓存应该为null,然后添加缓存。
server2第一次运行,添加key为ehcache的缓存,然后输出key=i的10条缓存有值。
server1第二次运行,获取key为ehcache的缓存有值,key=i的10条缓存数据被更新。
思路清晰然后开始实现:
server1代码
创建类EhcacheAction
public class EhcacheAction {
public static void main(String[] args) {
//cacheManager实例化
CacheManager cacheManager = CacheManager.create("classpah:cache/ehcache.xml");
Cache cache= cacheManager.getCache("userCache");
//获取key为ehcache的缓存并打印出来
Element temp = cache.get("ehcache");
System.out.println(temp.getObjectValue());
for (int i = 0; i < 10; i++) {
//添加缓存key=i,value=i
Element temp=new Element(i,i);
cache.put(temp);
System.out.println("第" + i + "次cache.put");
}
}
server1 ehcache.xml配置
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40002/userCache" propertySeparator=","/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=40001,socketTimeoutMillis=2000" />
<cache name="userCache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300000"
timeToLiveSeconds="600000"
memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
</cache>
server2代码
public class EhcacheAction {
public static void main(String[] args) {
//cacheManager实例化
CacheManager cacheManager = CacheManager.create("classpah:cache/ehcache.xml");
Cache cache= cacheManager.getCache("userCache");
//添加key="ehcache"的缓存
cache.put(new Element("ehcache", "newaddvalue"));
for (int i = 0; i < 10; i++) {
System.out.println("第"+i+"次加载cache.size="+cache.getSize());
}
}
}
server2 ehcache.xml配置
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40001/userCache" propertySeparator=","/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=40002,socketTimeoutMillis=2000" />
<cache name="userCache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300000"
timeToLiveSeconds="600000"
memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
</cache>
运行server1,server2查看输出类容。这样就实现了同台服务器两个应用缓存共享了。
我尝试过将server1和server2分别附属在两台服务器上,然后将各自的ehcache.xml中cacheManagerPeerProviderFactory 节点中hostName属性赋值为其它服务器的ip地址,运行后发现拒绝连接(防火墙已经全部关闭)这个问题还有待研究,希望有大神或已解决的高手帮助小弟告诉解决方法。