单独使用 EHCache
1. EHCache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如项目已安装了Hibernate ,则不需要做什么。。直接可以使用Ehcache
Cache 存储方式 :内存或磁盘
2. 单独使用 EHCache : 是使用CacheManager 创建并管理Cache
1.创建CacheManager有4种方式:
- A:使用默认配置文件创建
CacheManager manager = CacheManager.create();
- B:使用指定配置文件创建
CacheManager manager = CacheManager.create("src/config/ehcache.xml");
- C:从classpath中找寻配置文件并创建
URL url = getClass().getResource("/anothername.xml");
CacheManager manager = CacheManager.create(url);
- D:通过输入流创建
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
manager = CacheManager.create(fis);
} finally {
fis.close();
}
2、卸载CacheManager ,关闭Cache
- manager.shutdown();
3、使用Caches ,取得配置文件中预先 定义的sampleCache1设置,通过CacheManager生成一个Cache
- Cache cache = manager.getCache("sampleCache1");
4、往cache中加入元素
- Element element = new Element("key1", "value1");
- cache.put(new Element(element);
总结使用大概步骤为:
第一步:生成CacheManager对象
第二步:生成Cache对象
第三步:向Cache对象里添加由key,value组成的键值对的Element元素
一个demo分享给大家。
- package test;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Element;
- /**
- * 第一步:生成CacheManager对象
- * 第二步:生成Cache对象
- * 第三步:向Cache对象里添加由key,value组成的键值对的Element元素
- * @author
- */
- public class Test {
- public static void main(String[] args) {
- //指定ehcache.xml的位置
- String fileName="E:\\1008\\workspace\\ehcachetest\\ehcache.xml";
- CacheManager manager = new CacheManager(fileName);
- //取出所有的cacheName
- String names[] = manager.getCacheNames();
- for(int i=;i<names.length;i++){
- System.out.println(names[i]);
- }
- //根据cacheName生成一个Cache对象
- //第一种方式:
- Cache cache=manager.getCache(names[]);
- //第二种方式,ehcache里必须有defaultCache存在,"test"可以换成任何值
- // Cache cache = new Cache("test", 1, true, false, 5, 2);
- // manager.addCache(cache);
- //向Cache对象里添加Element元素,Element元素有key,value键值对组成
- cache.put(new Element("key1","values1"));
- Element element = cache.get("key1");
- System.out.println(element.getValue());
- Object obj = element.getObjectValue();
- System.out.println((String)obj);
- manager.shutdown();
- }
- }
单独使用 EHCache的更多相关文章
- Java缓存学习之六:Spring单独使用EhCache
通过在Application Context中配置EhCacheManagerFactoryBean和EhCacheFactoryBean,我们就可以把对应的EhCache的CacheManage ...
- Spring使用Cache、整合Ehcache
http://haohaoxuexi.iteye.com/blog/2123030 Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring ...
- ehCache浅谈(转)
ehcache FAQ中提到 Remember that a value in a cache element is globally accessible from multiple threads ...
- 缓存之EHCache(转)
一.简介非常简单,而且易用. ehcache 是一个非常轻量级的缓存实现,而且从1.2 之后就支持了集群,而且是hibernate 默认的缓存provider.ehcache 是一个纯Java的进程内 ...
- Ehcache
前言:设计一套缓存框架需要关注的要素 本文来源:RayChase 的<设计一套缓存框架需要关注的要素> 最近关注了一些缓存框架的特性和实现,包括OSCache.JCS.Ehcache.M ...
- Java 缓存技术之 ehcache
1. EHCache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中.如项目已安装了 ...
- Spring使用Cache、整合Ehcache(转)
今天在做Spring使用Cache.整合Ehcache时发现一篇非常好的文章,原文地址 http://elim.iteye.com/blog/2123030 从3.1开始,Spring引入了对Cach ...
- 缓存之EHCache(一)
源文: http://blog.csdn.net/l271640625/article/details/20528573 一.简介 非常简单,而且易用. ehcache 是一个非常轻量级的缓存 ...
- 缓存ehcache
应用场景: 当调用一个接口数据时:直连-缓存-数据库-远程调用(类似直连) 代码: // 从缓存 通过身份证号码查询是否存在征信报告 CisReportRoot fromCache = cacheSe ...
随机推荐
- 《CCNA(640-802)全套课程讲义》笔记
路由器: CLI(命令行界面)形式相对应的是GUI (图形用户界面) DTE:数据终端设备,指的是位于用户网络接口的用户端设备,通常情况下,路由器端为DTE端. DCE:数据通讯设备,为通信提供时钟 ...
- RTX——第10章 任务调度-抢占式、时间片和合作式
以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章教程为大家将介绍 RTX 操作系统支持的任务调度方式,抢占式,时间片和合作式,这部分算是RTX 操作 ...
- platform_driver_probe 函数解析
结构体列举 // 几个结构体 // include/linux/device.h struct bus_type { const char *name; // "platform" ...
- init进程接管孤儿进程的验证
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include<stdlib ...
- BM和KMP字符串匹配算法学习
BM和KMP字符串匹配算法学习 分类: 研究与学习 字符串匹配BM(Boyer-Moore)算法学习心得 http://www.cnblogs.com/a180285/archive/2011/12/ ...
- jffs2根文件系统制作
http://www.eetop.cn/blog/html/98/510998-20964.html 作者:刘洪涛,华清远见嵌入式学院高级讲师,ARM公司授权ATC讲师. JFFS2是Flash上应用 ...
- Linux服务器同步时间
进行Linux服务器的时间同步是一件需要注意的事情,不然,集群中的服务器时间不同将导致许多奇怪问题发生, 如果没有安装crontab,那么,使用yum install crontabs进行安装和启动, ...
- 单片机小白学步系列(二十) IO口原理
IO口操作是单片机实践中最基本最重要的一个知识,本篇花了比較长的篇幅介绍IO口的原理. 也是查阅了不少资料,确保内容正确无误,花了非常长时间写的. IO口原理原本须要涉及非常多深入的知识,而这里尽最大 ...
- r指定位置插入一列
y<-1:4 data1 <-data.frame(x1=c(1,3,5,7), x2=c(2,4,6,8),x3=c(11,12,13,14),x4=c(15,16,17,18)) da ...
- jquery实现简单瀑布流代码
测试环境:ie8 ff13.0.1 chrome22 可以将分页获取的内容依次填入四个div中,瀑布流的分页可以以多页(比如5页)为单位二次分页,这样可以减少后台算法的复杂度 <!DOCTYP ...