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. EHCache 初步使用指南

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 下图是 Ehcache 在应用程序中的位置: 主要的特性有: 1. 快 ...

  2. 缓存之EHCache

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

  3. java如何从缓存中读取数据_Java 缓存技术之 ehcache中读取出所有缓存名称列表数组...

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

  4. 2021-10-12Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  5. Spring 使用Cache(转)

    从3.1开始Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事物管理的支持.Spring Cache是作用在方法上的,其核心思想是:当我们在调用一个缓存方法时会把该方法参数 ...

  6. cacheable 表达式_Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  7. cacheable更新_Spring之缓存注解@Cacheable

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  8. SpringCache整合EhCache2和EhCache3(JCache)

    简介 在项目中想使用EhCache作为本地内存,而EhCache又存在这两个大版本,一个是2一个是3 .两者的区别很大,很容易在整合时踩到坑: 网上大多数(几乎全部) 整合都是基于EhCache2版本 ...

  9. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

最新文章

  1. word2vec 中的数学原理详解(二)预备知识
  2. 开源WebGIS实施方案(一):开篇 [转]
  3. Android 核心已经从 Linux kernel 代码库中删除
  4. jedis jedispool Redistemplate
  5. 计算机专业太难不适合女生学?来看 N 多小姐姐的回应
  6. 耦合式是什么意思_定向耦合器是如何工作的?能怎样应用?看完就全明白了
  7. 社区发现算法之——Louvain
  8. 10个必备的AutoCAD插件
  9. 『2021最新~解决』The push refers to a repository [docker.io/
  10. 线性代数笔记【空间向量】
  11. Axure RP8 进度条
  12. OpenHarmony hdc_std无反应的解决办法
  13. 没有对公账户怎么开通认证微信公众号?
  14. 移动商务,还是短信商务?
  15. AFI - all in - 逍遥棋 - 游戏规则
  16. 接口测试实战项目02:根据接口文档测试
  17. 解决联想拯救者Y9000X触控板失灵问题
  18. 黑莓BES系统架构介绍
  19. rtx2060什么水平_老平台升级RTX2060,——性能,功耗,静音都一步到位!
  20. 英语学习笔记(一)语法

热门文章

  1. jsonSuggest插件如何在使用前激活
  2. Spring Boot 、Spring Data JPA、Hibernate集成
  3. Visual Studio 20年
  4. Chapter5_初始化与清理_enum关键字
  5. iOS transform解决连续多次旋转缩放,实现图片旋转缩放效果
  6. IOS中的JSON数据的解析
  7. 在Laravel外使用Eloquent(一)
  8. BZOJ3799 : 字符串重组
  9. navbar build by Bootstrap3.x + RubyOnRails +Haml
  10. Stream Part.4