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分享给大家。

  1. package test;
  2. import net.sf.ehcache.Cache;
  3. import net.sf.ehcache.CacheManager;
  4. import net.sf.ehcache.Element;
  5. /**
  6. * 第一步:生成CacheManager对象
  7. * 第二步:生成Cache对象
  8. * 第三步:向Cache对象里添加由key,value组成的键值对的Element元素
  9. * @author
  10. */
  11. public class Test {
  12. public static void main(String[] args) {
  13. //指定ehcache.xml的位置
  14. String fileName="E:\\1008\\workspace\\ehcachetest\\ehcache.xml";
  15. CacheManager manager = new CacheManager(fileName);
  16. //取出所有的cacheName
  17. String names[] = manager.getCacheNames();
  18. for(int i=;i<names.length;i++){
  19. System.out.println(names[i]);
  20. }
  21. //根据cacheName生成一个Cache对象
  22. //第一种方式:
  23. Cache cache=manager.getCache(names[]);
  24. //第二种方式,ehcache里必须有defaultCache存在,"test"可以换成任何值
  25. // Cache cache = new Cache("test", 1, true, false, 5, 2);
  26. // manager.addCache(cache);
  27. //向Cache对象里添加Element元素,Element元素有key,value键值对组成
  28. cache.put(new Element("key1","values1"));
  29. Element element = cache.get("key1");
  30. System.out.println(element.getValue());
  31. Object obj = element.getObjectValue();
  32. System.out.println((String)obj);
  33. manager.shutdown();
  34. }
  35. }

单独使用 EHCache的更多相关文章

  1. Java缓存学习之六:Spring单独使用EhCache

      通过在Application Context中配置EhCacheManagerFactoryBean和EhCacheFactoryBean,我们就可以把对应的EhCache的CacheManage ...

  2. Spring使用Cache、整合Ehcache

    http://haohaoxuexi.iteye.com/blog/2123030 Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring ...

  3. ehCache浅谈(转)

    ehcache FAQ中提到 Remember that a value in a cache element is globally accessible from multiple threads ...

  4. 缓存之EHCache(转)

    一.简介非常简单,而且易用. ehcache 是一个非常轻量级的缓存实现,而且从1.2 之后就支持了集群,而且是hibernate 默认的缓存provider.ehcache 是一个纯Java的进程内 ...

  5. Ehcache

    前言:设计一套缓存框架需要关注的要素 本文来源:RayChase  的<设计一套缓存框架需要关注的要素> 最近关注了一些缓存框架的特性和实现,包括OSCache.JCS.Ehcache.M ...

  6. Java 缓存技术之 ehcache

    1. EHCache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中.如项目已安装了 ...

  7. Spring使用Cache、整合Ehcache(转)

    今天在做Spring使用Cache.整合Ehcache时发现一篇非常好的文章,原文地址 http://elim.iteye.com/blog/2123030 从3.1开始,Spring引入了对Cach ...

  8. 缓存之EHCache(一)

    源文: http://blog.csdn.net/l271640625/article/details/20528573 一.简介 非常简单,而且易用.     ehcache 是一个非常轻量级的缓存 ...

  9. 缓存ehcache

    应用场景: 当调用一个接口数据时:直连-缓存-数据库-远程调用(类似直连) 代码: // 从缓存 通过身份证号码查询是否存在征信报告 CisReportRoot fromCache = cacheSe ...

随机推荐

  1. druid使用

    https://blog.csdn.net/weinichendian/article/details/78503469

  2. HTML5 Canvas 梦幻的文字飞扬动画教程

    之前为大家介绍了一款HTML5/CSS3 3D文字特效 文字外翻效果.今天为大家带来的是html5 canvas实现的文字漂动动画效果.画面非常梦幻. 在线预览   源码下载 实现代码如下: html ...

  3. redis、mysql、和php原生array数组效率对比

    最近要做一个屏蔽词的功能,屏蔽词是保存在配置文件里,最开始是用php数组存储,然后查找. 后来随着屏蔽词的增多,速度明显的变慢了,于是考虑到用redis或mysql做. 于是写了一个测试,测试结果如下 ...

  4. java 支付宝wap支付初识

    最近突然想弄下支付宝的支付,因为感觉很好玩.中间遇到很多问题,查查找找,总算是整了两天给整出来了,这里为自己记录下. 第一步:直接去安卓支付宝的官方文档去,写的很清楚了已经,这里有源码https:// ...

  5. Tomcat Connector三种执行模式(BIO, NIO, APR)的比較和优化

    Tomcat Connector的三种不同的执行模式性能相差非常大,有人測试过的结果例如以下: 这三种模式的不同之处例如以下: BIO: 一个线程处理一个请求.缺点:并发量高时,线程数较多,浪费资源. ...

  6. FireFox火狐不能设置新标签页

    问题:已经在Tab Mix Plus中设置了新建标签页为自定义的网址,但依然没用. 百度很多都是让改about:config中的browser.newtab.url,然而这并没卵用. 办法:https ...

  7. C/C++-中的sort排序用法

    转载于:http://www.cnblogs.com/luorende/p/6121906.htmlSTL中就自带了排序函数sortsort 对给定区间所有元素进行排序 要使用此函数只需用#inclu ...

  8. js导出execl

    var idTmr; function ExportExcel(tableid) {//整个表格拷贝到EXCEL中 var curTbl = document.getElementById(table ...

  9. [mysql] MySQL Order By Rand()效率【转载】

    最近由于需要大概研究了一下MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RA ...

  10. t-SNE可视化(MNIST例子)

    如下所示: import pickle as pkl import numpy as np from matplotlib import pyplot as plt from tsne import ...