单独使用 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 ...
随机推荐
- 【转】Hadoop安全实践
前言 在2014年初,我们将线上使用的 Hadoop 1.0 集群切换到 Hadoop 2.2.0 稳定版, 与此同时部署了 Hadoop 的安全认证.本文主要介绍在 Hadoop 2.2.0 上部署 ...
- 一款纯css3实现的动画加载导航
之前为大家介绍了好几款导航菜单,今天为给大家再带来一款纯css3实现的动画加载导航.该导航出现的时候以动画的形式出现.效果图如下: 在线预览 源码下载 实现的代码. html代码: <ul ...
- Mac升级yosemite后无法登陆问题
Mac升级yosemite后无法登陆问题 今天心血来潮准备玩玩最新的苹果系统10.10,代号是yosemite.去官网申请了beta版的測试资格,然后在app store下载了一晚上得 ...
- ftp 长传报错553 可能是选的目录不对
ftp> put /root/20180711tmp.txt /cc.txt local: /root/20180711tmp.txt remote: /cc.txt 200 PORT comm ...
- GPU硬件加速原理 /转
现代浏览器大都可以利用GPU来加速页面渲染.每个人都痴迷于60桢每秒的顺滑动画.在GPU的众多特性之中,它可以存储一定数量的纹理(一个矩形的像素点集合)并且高效地操作这些纹理(比如进行特定的移动.缩放 ...
- Hibernate-【查询】
01.HQL查询方式 02.QBC查询方式 02.原始SQL查询方式
- MongoDB 简介及安装
什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...
- html中的label配合checkbox,redio用法
<input id="a1" type="checkbox" name="a" value="33023" /&g ...
- C++ GDI+调用
封装了一个GDI+的使用类 GdiPluss.h #pragma once #include <comdef.h> #include <gdiplus.h> using nam ...
- android boot.img unpack pack
每次编译boot.img都要花比较长的时间,有时候只是更改其中的配置文件. 如果能够将boot.img解压,更改之后再打包的话,就能节省时间. boot.img tools是别人写好的工具,能很好的解 ...