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缓存框架详解的更多相关文章

  1. Shiro 安全框架详解二(概念+权限案例实现)

    Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...

  2. Shiro 安全框架详解一(概念+登录案例实现)

    shiro 安全框架详细教程 总结内容 一.RBAC 的概念 二.两种常用的权限管理框架 1. Apache Shiro 2. Spring Security 3. Shiro 和 Spring Se ...

  3. mina框架详解

     转:http://blog.csdn.net/w13770269691/article/details/8614584 mina框架详解 分类: web2013-02-26 17:13 12651人 ...

  4. [Cocoa]深入浅出 Cocoa 之 Core Data(1)- 框架详解

    Core data 是 Cocoa 中处理数据,绑定数据的关键特性,其重要性不言而喻,但也比较复杂.Core Data 相关的类比较多,初学者往往不太容易弄懂.计划用三个教程来讲解这一部分: 框架详解 ...

  5. iOS 开发之照片框架详解(2)

    一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...

  6. mapreduce框架详解

    hadoop 学习笔记:mapreduce框架详解 开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感 ...

  7. iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)

    本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...

  8. iOS 开发之照片框架详解

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...

  9. iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...

随机推荐

  1. c#缓存介绍(转)

    缓存主要是为了提高数据的读取速度.因为服务器和应用客户端之间存在着流量的瓶颈,所以读取大容量数据时,使用缓存来直接为客户端服务,可以减少客户端与服务器端的数据交互,从而大大提高程序的性能. 本章从缓存 ...

  2. https适配

    http://www.jianshu.com/p/f312a84a944c http://www.2cto.com/kf/201611/570823.html http://www.cnblogs.c ...

  3. vmware 安装 macos

    http://jingyan.baidu.com/article/ff411625b9011212e48237b4.html

  4. iShare.js分享插件

    iShare.js是一个小巧的分享插件,纯JS编写,不依赖任何第三方库,使用简便. 为啥写这个插件? 因为在搭建个人blog时(还没有搭建好(¯﹃¯)),对目前国内比较受欢迎的分享插件都不太满意,主要 ...

  5. 第一个structs+spring+hibernate的web程序

    1. 数据库: Column Type Comment id int(11) Auto Increment   name varchar(50) NULL   url varchar(255) NUL ...

  6. Struts2版本升级到struts2 2.3.15.1操作说明

    Struts2的官网公布了一个远程命令执行漏洞,官方强烈建议升级到2.3.15.1或者以上版本,该版本包含校正过的struts2核心库. 我们之前开发项目主要采用的Struts2版本是2.2.1,本文 ...

  7. Unable to load native-hadoop library for your platform

    #HADOOP VARIABLES START export JAVA_HOME=/home/yang/jdk1.7.0_80export HADOOP_HOME=/home/hadoop/hadoo ...

  8. linux通过端口号查找程序执行路径

    第一种: 查看ssh服务 [root@localhost shell]# netstat -anlp | grep :22tcp        0      0 0.0.0.0:22          ...

  9. BackTrack5-r3汉化

    进入BT系统图形模式,将语言包1和2拖进BT图形桌面. 所需文件包地址:http://pan.baidu.com/s/1i3ouc9v(64位更新包)将语言包1里的全部文件复制粘贴到:/var/cac ...

  10. C# 生成条形码图片,效果不错

    //首先引用 条码库BarcodeLib.dll using System; using System.Collections.Generic; using System.Linq; using Sy ...