EHcache缓存框架详解
2024-09-23 07:12:07
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());
}
}
最新文章
- leetcode:1-5题代码整理
- asp.net webForm登录授权
- MyFirstgame 拼图
- iOS开发--遇到ARGB/RGBA怎么办
- 理解Java接口
- Qt5+VS2012编程
- [Effective JavaScript 笔记]第54条:将undefined看做“没有值”
- 【JAVA、C++】LeetCode 012 Integer to Roman
- Hadoop学习记录(1)|伪分布安装
- Codeforces Round #190 (Div. 2) E. Ciel the Commander 点分治
- 关于Android WindowManager显示悬浮窗的动画效果
- PHP性能优化-编译级别的缓存
- 09-IOSCore - 应用互动、UIImagePickerController
- 用命令行在github新建一个项目
- Keil C动态内存管理机制分析及改进(转)
- [设计]GUI设计规范的规范
- antlr v4 使用指南连载4——词法规则入门之黄金定律
- 语法的二义性和token的超前扫描
- C# Httpclient客户端操作
- 写给创业者:如何打造让用户离不开的App
热门文章
- Ext JS 4 新特性2:配置项属性(config)之一
- jQuery从无知到无所不知
- BZOJ 4521 手机号码
- js/jquery 回调函数的定义方法
- Record:Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView
- Java(五)
- 开篇----JavaScript细节的那些事儿
- Android SDK ADT下载地址
- MSP430 IO 使用
- git format-patch &; git apply &; git clean