Ehcache利用Terracotta方式同步缓存

[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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值