读书人

ehcache分布式事例

发布时间: 2013-03-21 10:08:17 作者: rapoo

ehcache分布式例子
配置文件:
recluster_ehcache_0.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cacheManagerPeerProviderFactory
/>

<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory />
</defaultCache>

<cache name="UserCache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="100000" timeToLiveSeconds="100000"
overflowToDisk="false">
<cacheEventListenerFactory />
</cache>

</ehcache>

recluster_ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cacheManagerPeerProviderFactory
/>

<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory />
</defaultCache>

<cache name="UserCache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="100000" timeToLiveSeconds="100000"
overflowToDisk="false">
<cacheEventListenerFactory />
</cache>



</ehcache>


代码:

package cache.echache.Init;

import java.net.URL;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class clustertest {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException{
// TODO Auto-generated method stub
//URL url = clustertest.class.getClassLoader().getResource("recluster_ehcache.xml");
CacheManager manager = new CacheManager("recluster_ehcache.xml");

//get Cache
Cache cache = manager.getCache("UserCache");
Thread.sleep(10000);
Element element = new Element("key1", "value1");
Element element1 = new Element("key12", "value1");
Element element2 = new Element("key13", "value1");
Element element3 = new Element("key14", "value1");
cache.put(element);
cache.put(element1);
cache.put(element2);
cache.put(element3);
System.out.println("Initial:\n"//+url.toString()
+"\n"+manager.getName()
+"\n"+cache.getName()
+" 's size = "+cache.getSize()
+"\n"+element.toString());


Element element01 = cache.get("key1");
System.out.println(element01.getValue());
Element element02 = cache.get("key12");
System.out.println(element02.getValue());
System.out.println("主机测试等待中.............");

while(true){
Thread.sleep(1000);
}
}

}


package cache.echache.Init;

import java.net.URL;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class clustertest0 {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException{
// TODO Auto-generated method stub
//URL url = clustertest.class.getClassLoader().getResource("recluster_ehcache.xml");

CacheManager manager = new CacheManager("recluster_ehcache_0.xml");

//get Cache
Cache cache = manager.getCache("UserCache");
Thread.sleep(10000);
/*Element element = new Element("key1", "value1");
Element element1 = new Element("key12", "value1");
Element element2 = new Element("key13", "value1");
Element element3 = new Element("key14", "value1");
cache.put(element);
cache.put(element1);
cache.put(element2);
cache.put(element3);
System.out.println("Initial:\n"//+url.toString()
+"\n"+manager.getName()
+"\n"+cache.getName()
+" 's size = "+cache.getSize()
+"\n"+element.toString()); */


/*Element element01 = cache.get("key1");
System.out.println(element01.getValue());
System.out.println("主机测试等待中............."); */

while(true){
Element element01 = cache.get("key1");
if(null!=element01){
System.out.println(element01.getValue());

}
Thread.sleep(1000);
}
}

}

读书人网 >编程

热点推荐