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使用,以及集群配置
4星 · 超过85%的资源 需积分: 20 130 浏览量
2014-08-10
23:26:50
上传
评论
收藏 4.65MB RAR 举报
xh199110
- 粉丝: 9
- 资源: 3
最新资源
- apk.tw_LineLite_v8a_v.2.17.1_sign.apk
- Elasticsearch实战:构建高效搜索系统的秘诀.zip
- HTML+CSS+JS网页设计:从入门到精通.zip
- 数据库课程设计:从理论到实践的全面指南.zip
- Python闭包:深入理解与应用场景解析.zip
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈