package com.ehcache;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.distribution.CacheManagerPeerProvider;
public class MyDataAccessClass {
private Ehcache cache=null;
private static MyDataAccessClass myDataAccessClass=new MyDataAccessClass();
private MyDataAccessClass() {
this.cache=getCache();
}
/* read some data, check cache first, otherwise read from sor */
public Object readSomeData(Object key) {
Element element;
Object value=null;
if ((element = cache.get(key)) != null) {
return element.getObjectValue();
}
// note here you should decide whether your cache
// will cache 'nulls' or not
// if (value = readDataFromDataStore(key)) != null) {
// cache.put(new Element(key, value));
// }
return value;
} /* write some data, write to sor, then update cache */
public void writeSomeData(Object key, Object value)
{
// writeDataToDataStore(key, value);
cache.put(new Element(key, value));
}
public static MyDataAccessClass getInstance(){
return myDataAccessClass;
}
private static Cache getCache(){
InputStream fis = MyDataAccessClass.class.getClassLoader().getResourceAsStream("ehcache.xml");
CacheManager manager=null;
try {
manager=CacheManager.create(fis);
// manager = CacheManager.newInstance(fis);
Cache oneCache = manager.getCache("myCache");
return oneCache;
} finally {
try {
if(fis!=null){
fis.close();
fis=null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
InputStream fis = MyDataAccessClass.class.getClassLoader().getResourceAsStream("ehcache.xml");
CacheManager manager=null;
try {
manager=CacheManager.create(fis);
} finally {
try {
if(fis!=null){
fis.close();
fis=null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Cache cache=manager.getCache("myCache");
CacheManagerPeerProvider provider = manager.getCacheManagerPeerProvider("RMI");
System.out.println(manager.getCacheManagerPeerProviders().size());
System.out.println(provider);
List list=provider.listRemoteCachePeers(cache);
System.out.println(list.size());
manager.shutdown();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
ehcache使用,以及集群配置
共16个文件
xml:4个
java:3个
jar:3个
4星 · 超过85%的资源 需积分: 20 54 下载量 75 浏览量
2014-08-10
23:26:50
上传
评论
收藏 4.65MB RAR 举报
温馨提示
此为ehcache的使用以及集群的使用,具体需要两台机子,将ehcache 的hostName替换相应的ip即可
资源推荐
资源详情
资源评论
收起资源包目录
ehcache.rar (16个子文件)
ehcache
.project 383B
src
ehcache2.xml 57KB
ehcache.xml 1KB
com
ehcache
MyDataAccessClass.java 3KB
Test.java 220B
Test2.java 1KB
lib
slf4j-jdk14-1.6.6.jar 9KB
ehcache-2.8.3.jar 5.38MB
slf4j-api-1.6.6.jar 26KB
.settings
org.eclipse.jdt.core.prefs 629B
.classpath 487B
bin
ehcache2.xml 57KB
ehcache.xml 1KB
com
ehcache
Test2.class 3KB
MyDataAccessClass.class 3KB
Test.class 475B
共 16 条
- 1
资源评论
- 亡灵乌鸦2016-09-01可以用,但是没有集群同步数据的内容,没参考价值
- 疯狂de攻城狮2021-02-22不错 可以用 谢谢分享
- siyueabc2016-07-08看了一下,不确定是否有用
xh199110
- 粉丝: 9
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功