[url]http://blog.csdn.net/gtuu0123/archive/2009/12/13/4997349.aspx[/url]
Ehcache利用Terracotta方式同步缓存 收藏
1.下载并安装Terracotta
http://www.terracotta.org/
2.启动Terracotta
bin/start-tc-server.bat 或 bin/start-tc-server.sh
3.代码
Set值类:
view plaincopy to clipboardprint?
package my.test.terracotta;
import java.io.InputStream;
import my.test.ehcache1.EHCacheTest;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class TestTerracottaSet {
public TestTerracottaSet() {
InputStream is = EHCacheTest.class.getResourceAsStream("/my/test/terracotta/ehcache.xml");
CacheManager cacheManager = new CacheManager(is);
Cache cache = cacheManager.getCache("sampleTerracottaCache");
for (int i = 0; i < 1001; i++) {
cache.put(new Element("key" + i, "value" + i));
}
}
public static void main(String[] args) throws Exception {
new TestTerracottaSet();
}
}
Get值类:
view plaincopy to clipboardprint?
package my.test.terracotta;
import java.io.InputStream;
import my.test.ehcache1.EHCacheTest;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class TestTerracottaGet {
public TestTerracottaGet() {
InputStream is = EHCacheTest.class.getResourceAsStream("/my/test/terracotta/ehcache.xml");
CacheManager cacheManager = new CacheManager(is);
Cache cache = cacheManager.getCache("sampleTerracottaCache");
for (int i = 0; i < 1001; i++) {
Element elem = cache.get("key" + i);
if (elem == null) {
System.out.println("key" + i + " not found");
} else {
System.out.println("key" + i + "=" + elem.getValue());
}
}
}
public static void main(String[] args) throws Exception {
new TestTerracottaGet();
}
}
配置文件:修改官方样例配置文件,添加
view plaincopy to clipboardprint?
<terracottaConfig url="localhost:9510"/>
view plaincopy to clipboardprint?
<cache name="sampleTerracottaCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="1800"
overflowToDisk="false">
<terracotta/>
</cache>
这是利用Terracotta服务器来进行缓存同步
4.运行
(1)先运行TestTerracottaSet类
(2)再运行TestTerracottaGet类
(3)运行结果:
key0=value0
key1=value1
key2=value2
key3=value3
key4=value4
key5=value5
key6=value6
key7=value7
key8=value8
key9=value9
key10=value10
......
key1000=value1000
可以通过Terrocotta控制台查看缓存情况,如图:
[img]http://dl.iteye.com/upload/attachment/189088/95bc51fa-cfe9-33a1-aec7-758b68df9057.jpg[/img]
Ehcache利用Terracotta方式同步缓存 收藏
1.下载并安装Terracotta
http://www.terracotta.org/
2.启动Terracotta
bin/start-tc-server.bat 或 bin/start-tc-server.sh
3.代码
Set值类:
view plaincopy to clipboardprint?
package my.test.terracotta;
import java.io.InputStream;
import my.test.ehcache1.EHCacheTest;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class TestTerracottaSet {
public TestTerracottaSet() {
InputStream is = EHCacheTest.class.getResourceAsStream("/my/test/terracotta/ehcache.xml");
CacheManager cacheManager = new CacheManager(is);
Cache cache = cacheManager.getCache("sampleTerracottaCache");
for (int i = 0; i < 1001; i++) {
cache.put(new Element("key" + i, "value" + i));
}
}
public static void main(String[] args) throws Exception {
new TestTerracottaSet();
}
}
Get值类:
view plaincopy to clipboardprint?
package my.test.terracotta;
import java.io.InputStream;
import my.test.ehcache1.EHCacheTest;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class TestTerracottaGet {
public TestTerracottaGet() {
InputStream is = EHCacheTest.class.getResourceAsStream("/my/test/terracotta/ehcache.xml");
CacheManager cacheManager = new CacheManager(is);
Cache cache = cacheManager.getCache("sampleTerracottaCache");
for (int i = 0; i < 1001; i++) {
Element elem = cache.get("key" + i);
if (elem == null) {
System.out.println("key" + i + " not found");
} else {
System.out.println("key" + i + "=" + elem.getValue());
}
}
}
public static void main(String[] args) throws Exception {
new TestTerracottaGet();
}
}
配置文件:修改官方样例配置文件,添加
view plaincopy to clipboardprint?
<terracottaConfig url="localhost:9510"/>
view plaincopy to clipboardprint?
<cache name="sampleTerracottaCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="1800"
overflowToDisk="false">
<terracotta/>
</cache>
这是利用Terracotta服务器来进行缓存同步
4.运行
(1)先运行TestTerracottaSet类
(2)再运行TestTerracottaGet类
(3)运行结果:
key0=value0
key1=value1
key2=value2
key3=value3
key4=value4
key5=value5
key6=value6
key7=value7
key8=value8
key9=value9
key10=value10
......
key1000=value1000
可以通过Terrocotta控制台查看缓存情况,如图:
[img]http://dl.iteye.com/upload/attachment/189088/95bc51fa-cfe9-33a1-aec7-758b68df9057.jpg[/img]