单独使用 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相关推荐
- EHCache 初步使用指南
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 下图是 Ehcache 在应用程序中的位置: 主要的特性有: 1. 快 ...
- 缓存之EHCache
转载自 http://blog.csdn.net/l271640625/article/details/20528573 一.简介 非常简单,而且易用. ehcache 是一个非常轻量级的缓存 ...
- java如何从缓存中读取数据_Java 缓存技术之 ehcache中读取出所有缓存名称列表数组...
1. EHCache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中.如项目已安装了 ...
- 2021-10-12Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- Spring 使用Cache(转)
从3.1开始Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事物管理的支持.Spring Cache是作用在方法上的,其核心思想是:当我们在调用一个缓存方法时会把该方法参数 ...
- cacheable 表达式_Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- cacheable更新_Spring之缓存注解@Cacheable
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- SpringCache整合EhCache2和EhCache3(JCache)
简介 在项目中想使用EhCache作为本地内存,而EhCache又存在这两个大版本,一个是2一个是3 .两者的区别很大,很容易在整合时踩到坑: 网上大多数(几乎全部) 整合都是基于EhCache2版本 ...
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
最新文章
- word2vec 中的数学原理详解(二)预备知识
- 开源WebGIS实施方案(一):开篇 [转]
- Android 核心已经从 Linux kernel 代码库中删除
- jedis jedispool Redistemplate
- 计算机专业太难不适合女生学?来看 N 多小姐姐的回应
- 耦合式是什么意思_定向耦合器是如何工作的?能怎样应用?看完就全明白了
- 社区发现算法之——Louvain
- 10个必备的AutoCAD插件
- 『2021最新~解决』The push refers to a repository [docker.io/
- 线性代数笔记【空间向量】
- Axure RP8 进度条
- OpenHarmony hdc_std无反应的解决办法
- 没有对公账户怎么开通认证微信公众号?
- 移动商务,还是短信商务?
- AFI - all in - 逍遥棋 - 游戏规则
- 接口测试实战项目02:根据接口文档测试
- 解决联想拯救者Y9000X触控板失灵问题
- 黑莓BES系统架构介绍
- rtx2060什么水平_老平台升级RTX2060,——性能,功耗,静音都一步到位!
- 英语学习笔记(一)语法
热门文章
- jsonSuggest插件如何在使用前激活
- Spring Boot 、Spring Data JPA、Hibernate集成
- Visual Studio 20年
- Chapter5_初始化与清理_enum关键字
- iOS transform解决连续多次旋转缩放,实现图片旋转缩放效果
- IOS中的JSON数据的解析
- 在Laravel外使用Eloquent(一)
- BZOJ3799 : 字符串重组
- navbar build by Bootstrap3.x + RubyOnRails +Haml
- Stream Part.4