EHcache缓存框架详解
EhCache是一个纯Java的进程内缓存框架,具有快速、精干等特点,也是Hibernate中默认的CacheProvider。
归纳一下它大概具有一下几个特点:
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现
那么我们在开发中到底如何运用EhCache框架呢?
获取Ehcache相关jar包及帮助文档。
下载地址: http://ehcache.org/code
相关文档地址: http://ehcache.org/apidocs/
/**
* maxElementsInMemory:缓存中允许创建的最大对象数
* eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
* timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,两次访问时间的最大时间间隔值, 这只能在元素不是永久驻留时有效,
* 如果该值是 0 就意味着元素可以停顿无穷长的时间。
* timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,
* 如果该值是0就意味着元素可以停顿无穷长的时间。 overflowToDisk:内存不足时,是否启用磁盘缓存。
* memoryStoreEvictionPolicy:缓存满了之后的淘汰算法
*
* @param args
*/
public static void main(String[] args) {
// 创建一个缓存管理器对象
CacheManager cacheManager = CacheManager.create();
// 命名缓存管理器
cacheManager.setName("testCacheManager");
// 创建一个指定缓存名称的缓存对象
Cache cache = new Cache("testCache", 4, false, false, 1, 1);
// cache.setDisabled(true);
// 将缓存对象添加至缓存管理器
cacheManager.addCache(cache); // cacheManager.shutdown(); System.out.println("判断缓存管理器中是否存在指定的缓存对象:"
+ cacheManager.cacheExists("testCache"));
DiskStorePathManager disStoreManager = cacheManager
.getDiskStorePathManager();
System.out.println("获取当前配置文件硬盘路径:"
+ disStoreManager.getFile("testCache.xml")); Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "tom");
map.put("sex", "男");
map.put("age", 1);
// 注意:如果当前缓存对象设置了内存中最大缓存keyValue对象的话,如果超出时,则后面的覆盖前面的keyValue对象
cache.put(new Element("cache1", map));
cache.put(new Element("cache2", map));
cache.put(new Element("cache3", map)); Element element = new Element("cache4", map);
element.setTimeToLive(1); cache.put(element); String[] cacheNames = cacheManager.getCacheNames();
for (int i = 0; i < cacheNames.length; i++) {
System.out.println("缓存" + i + ":" + cacheNames[i]);
} // System.out.println("当前活动的缓存配置文件内容:\n"
// + cacheManager.getActiveConfigurationText());
System.out.println("缓存管理器对象是否命名:" + cacheManager.isNamed()); Cache testCahe = cacheManager.getCache("testCache"); System.out.println("缓存的状态:" + testCahe.getStatus());
System.out.println("缓存对象平均获取时间:" + testCahe.getAverageGetTime());
System.out.println("获取缓存对象占用内存空间大小:" + testCahe.getMemoryStoreSize());
System.out.println("获取缓存对象大小:" + testCahe.getSize());
System.out.println("缓存是否关闭:" + testCahe.isDisabled());
System.out.println("判断某一个缓存key是否存在在缓存中"
+ testCahe.isKeyInCache("cache3"));
System.out.println("判断某一个缓存值是否缓存在对象中:" + testCahe.isValueInCache(map)); // 验证缓存对象是否禁用
if (!testCahe.isDisabled()) {
System.out.println("判断缓存中某个对象是否过期:"
+ testCahe.isExpired(testCahe.get("cache3")));
} else {
System.out.println(testCahe.getName() + "缓存已关闭");
}
System.out.println("判断某一个key是否缓存在内存中:"
+ testCahe.isElementInMemory("cache1"));
System.out.println("判断某一个key是否缓存在磁盘中:"
+ testCahe.isElementOnDisk("cache1")); System.out.println("\n"); List cacheKey = cache.getKeys();
for (int i = 0; i < cacheKey.size(); i++) {
Element cacheElement = testCahe.get(cacheKey.get(i));
System.out.println("Key:" + cacheKey.get(i) + ",value:"
+ cacheElement.getObjectValue());
}
}
EHcache缓存框架详解的更多相关文章
- Shiro 安全框架详解二(概念+权限案例实现)
Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...
- Shiro 安全框架详解一(概念+登录案例实现)
shiro 安全框架详细教程 总结内容 一.RBAC 的概念 二.两种常用的权限管理框架 1. Apache Shiro 2. Spring Security 3. Shiro 和 Spring Se ...
- mina框架详解
转:http://blog.csdn.net/w13770269691/article/details/8614584 mina框架详解 分类: web2013-02-26 17:13 12651人 ...
- [Cocoa]深入浅出 Cocoa 之 Core Data(1)- 框架详解
Core data 是 Cocoa 中处理数据,绑定数据的关键特性,其重要性不言而喻,但也比较复杂.Core Data 相关的类比较多,初学者往往不太容易弄懂.计划用三个教程来讲解这一部分: 框架详解 ...
- iOS 开发之照片框架详解(2)
一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...
- mapreduce框架详解
hadoop 学习笔记:mapreduce框架详解 开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感 ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)
本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...
- iOS 开发之照片框架详解
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...
随机推荐
- 关于JavaScript和html的随笔
最近听了一些关于JavaScript和html的讲课和读了一些书籍.因为我是给项目做网站知道的,所以要特别的注意和努力.JavaScript是一门挺好用的脚本语言,比较简单灵活,在这上面我深有体会,因 ...
- red hat enterprise 6安装tftp服务
1--->检查是否安装tftp rpm -qa tftp* 2--->安装tftp yum install -y tftp-server 3--->chkconfig --list| ...
- 为什么 input 元素能用 width 属性
前几天在是写网页的时候发现input可以设置宽高,很疑惑,上网查了之后发现: 简单的认为 input 就是 inline 元素是片面的.元素默认以何种 display 属性值显示出来,这个其实更多的时 ...
- JDom2的Xpath使用
/** * XPath获取属性值 * @param root * @param xPath * @return */ public static String getXPathAttributeVal ...
- 安装ant问题
1 设置path, 在windwos 7下要重启 2 重启后,cmd----ant -version Unable to locate tools.jar. Expected to find i ...
- 移动安全初探:窃取微信聊天记录、Hacking Android with Metasploit
在这篇文章中我们将讨论如何获取安卓.苹果设备中的微信聊天记录,并演示如何利用后门通过Metasploit对安卓设备进行控制.文章比较基础.可动手性强,有设备的童鞋不妨边阅读文章边操作,希望能激发大家对 ...
- line-height的一点粗浅认识
line-height:有5种不同类型的单位. line-height: normal|inherit(继承)|%(百分比)|20px(像素值)|1.2(倍数); 将行高和字体大小结合: body { ...
- ResultSet 结果集带回来的一些信息
ResultSet.getMetaData() 得到结果集的结构信息,比如字段数.字段名等. ResultSet.getMetaData().getTableName(1) 就可以返回表名. Resu ...
- 在Android Studio和Android Eclipse 更改现有项目里的SDK版本
一,在Eclipse下改项目里的SDK的版本方法有几种,都比较简单:1.右键单击项目--->properties---->Resource----->Android在Project ...
- Palindrome Pairs -- LeetCode 336
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...