62,612
社区成员
发帖
与我相关
我的任务
分享
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="C:/ehcache/session"/>
<!--调用ehcache2的RMI-->
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//192.168.1.2:40002/userIdcache "/>
<!--RMI监听40001端口-->
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.1,port=40001,socketTimeoutMillis=2000"/>
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="userIdcache "
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0"
overflowToDisk="false" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="true"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<!--监听配置-->
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />
</cache>
</ehcache>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="C:/ehcache/session"/>
<!--调用ehcache1的RMI-->
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//192.168.1.1:40001/userIdcache "/>
<!--RMI监听40002端口-->
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.2,port=40002,socketTimeoutMillis=2000"/>
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="userIdcache "
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="0"
overflowToDisk="false" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="true"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<!--监听配置-->
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />
</cache>
</ehcache>
public class CacheFactory {
private CacheManager manager;
private static CacheFactory factory= null;
private CacheFactory(){
manager = new CacheManager("src/ehcache.xml");
}
public static CacheFactory getInstance(){
if(factory==null){
factory = new CacheFactory();
}
return factory;
}
public Cache getCache(){
return manager.getCache("userIdcache");
}
public static void main(String[] args) {
System.out.println(CacheFactory.getInstance().getCache());
}
}
public class TestCache {
public static void main(String[] args) {
Cache cache = CacheFactory.getInstance().getCache();
//这里给cache里边放一个key1
Element element = new Element("key1", "value1");
cache.put(element);
System.out.println("server1: put cache :"+element.getValue());
}
}
public class TestCache {
public static void main(String[] args) {
Cache cache = CacheFactory.getInstance().getCache();
//这里Element为空 获取不到key1
Element element = cache.get("key1");
System.out.println("server2:"+element.getValue());
}
}