单独使用 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 ...
随机推荐
- PowerShell实现基于SharePoint的网站HomePage Auto-Configure Solution
Home Page Web Parts Auto-Configuration PS:该项目为公司项目,我还是给他的名字屏蔽掉吧,这是我用PowerShell写的一个自动化升级工具,此为三部自动化工具的 ...
- 被linux线程基础折磨的点滴——还是没得到完美的答案,但至少得到了所需
#include<sys/types.h> #include<unistd.h> #include<stdio.h> #include<stdlib.h> ...
- perl 计算方差中值平均数 Statistics::Descriptive;
http://search.cpan.org/~shlomif/Statistics-Descriptive-3.0612/lib/Statistics/Descriptive.pm use Stat ...
- plot-sin-02
draw sin 02 设置数据区域的边界线颜色 设置坐标轴的位置 code #!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as ...
- 【Unity】关于发射子弹、导弹追踪的逻辑
做个笔记,之后补上. 一.发射子弹 网上搜到的基本是两种方法: 给子弹物体添加一个力 AddForce. 子弹物体挂一个运动脚本,Update中毎帧向前运动.通过调整子弹生成点的Transform来控 ...
- 【C#/WPF】Button按钮动态设置Background背景颜色
学习笔记: 在XAML中给Button设置颜色大家都懂的,本篇只是记录用C#代码动态生成的按钮设置Background背景颜色. new一个Button,设置Background时可看到该属性类型是S ...
- 在WMware新建一个虚拟机
- mysql中使用正则表达式查询
正则表达式功能确实很强大,那天专门抽空学学,这里就暂时在mysql查询中用用. 正则表达式强大而灵活,可以应用于非常复杂的查询. 选项 说明(自动加匹配二字) 例子 匹配值示例 ^ 文本开始字符 '^ ...
- u-boot 2011.09 使用自己的board 以及config.h
一个新的方案,用的UBOOT 可能和上一个方案是同一个,但是配置有可能不一样,今天记录一下通过修改配置文件使用新的 board 文件以及 config.h 进入 u-boot 2011.09 // 打 ...
- datanode启动失败
当我动态加入一个hadoop从节点的之后,出现了一个问题: [root@hadoop current]# hadoop-daemon.sh start datanode starting datano ...