EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

Ehcache缓存的特点:
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现
 
Ehcache缓存的使用(1) – 安装ehcache
Ehcache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如项目已安装了Hibernate ,则不需要做什么,直接可以使用Ehcache 。
 
Ehcache缓存的使用(2) - 生成CacheManager
使用CacheManager 创建并管理Cache
1.创建CacheManager有4种方式:
A:使用默认配置文件创建
Java代码
CacheManager manager = CacheManager.create();
 
B:使用指定配置文件创建
Java代码
CacheManager manager = CacheManager.create("src/config/ehcache.xml");//eclipse中的路径格式。代码中使用new File()来加载配置文件。要因需调整
 
C:从classpath中找寻配置文件并创建
Java代码
URL url = getClass().getResource("/anothername.xml");
CacheManager manager = CacheManager.create(url);
 
D:通过输入流创建
Java代码
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
manager = CacheManager.create(fis);
} finally {
fis.close();
}
 
Ehcache缓存的使用(3) – 解读Ehcache配置文件ehcache.xml
重要的参数
<diskStore path="D:/work2/renhewww/cache"/>//此处的pach属性也可以使用java.io.tmpdir
<cache name=" sampleCache1"
maxElementsInMemory="1"
maxElementsOnDisk="10000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
diskPersistent="true"
timeToIdleSeconds="43200"
timeToLiveSeconds="86400"
memoryStoreEvictionPolicy="LFU"
/>

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" dynamicConfig="false">
<diskStore path="java.io.tmpdir"/>
<cache name="staticResourceCache"
maxElementsInMemory="1000"
timeToIdleSeconds="7200"
timeToLiveSeconds="7200">
</cache>
</ehcache>


属性解释:

必须属性:

name:设置缓存的名称,用于标志缓存,惟一

maxElementsInMemory:在内存中最大的对象数量

maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制

eternal:设置元素是否永久的,如果为永久,则timeout忽略

overflowToDisk:是否当memory中的数量达到限制后,保存到Disk

可选的属性:

timeToIdleSeconds:设置元素过期前的空闲时间

timeToLiveSeconds:设置元素过期前的活动时间

diskPersistent:是否disk store在虚拟机启动时持久化。默认为false

diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒

memoryStoreEvictionPolicy:策略关于Eviction

缓存子元素:

cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire

bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。

Ehcache缓存的使用(4) – 创建Cache
通过CacheManager创建Cache
Cache cache = manager.getCache("sampleCache1");
 
Ehcache缓存的使用(5) – 利用cache存取数据
存储数据
Element element = new Element("key1", "value1");
cache.put(new Element(element);
获取数据
Element element = cache.get("key1");
 
 

缓存的创建,采用自动的方式

CacheManager singletonManager = CacheManager.create();

singletonManager.addCache("testCache");

Cache test = singletonManager.getCache("testCache");

或者直接创建Cache

CacheManager singletonManager = CacheManager.create();

Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);

manager.addCache(memoryOnlyCache);

Cache test = singletonManager.getCache("testCache");

删除cache

CacheManager singletonManager = CacheManager.create();

singletonManager.removeCache("sampleCache1");

在使用ehcache后,需要关闭

CacheManager.getInstance().shutdown()

caches 的使用

Cache cache = manager.getCache("sampleCache1");

执行crud操作

Cache.get(Object key),
Cache.put(new Element(Object key, Object value)),
Cache.remove(Object key)。

Cache cache = manager.getCache("sampleCache1");

Element element = new Element("key1", "value1");

cache.put(element);

//update

Cache cache = manager.getCache("sampleCache1");

cache.put(new Element("key1", "value1");

//This updates the entry for "key1"

cache.put(new Element("key1", "value2");

//get Serializable

Cache cache = manager.getCache("sampleCache1");

Element element = cache.get("key1");

Serializable value = element.getValue();

//get non serializable

Cache cache = manager.getCache("sampleCache1");

Element element = cache.get("key1");

Object value = element.getObjectValue();

//remove

Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1")
cache.remove("key1"); 

http://www.cnblogs.com/hubcarl/p/3257774.html

Java的进程内缓存框架:EhCache (转)的更多相关文章

  1. Java的进程内缓存框架:EhCache

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.   Ehcache缓存的特点: 1. 快速. 2. 简单. 3. 多种 ...

  2. 一文深入了解史上最强的Java堆内缓存框架Caffeine

    它提供了一个近乎最佳的命中率.从性能上秒杀其他一堆进程内缓存框架,Spring5更是为了它放弃了使用多年的GuavaCache 缓存,在我们的日常开发中用的非常多,是我们应对各种性能问题支持高并发的一 ...

  3. spring+springMVC+JPA配置详解(使用缓存框架ehcache)

    SpringMVC是越来越火,自己也弄一个Spring+SpringMVC+JPA的简单框架. 1.搭建环境. 1)下载Spring3.1.2的发布包:Hibernate4.1.7的发布包(没有使用h ...

  4. spring boot:使用spring cache+caffeine做进程内缓存(本地缓存)(spring boot 2.3.1)

    一,为什么要使用caffeine做本地缓存? 1,spring boot默认集成的进程内缓存在1.x时代是guava cache 在2.x时代更新成了caffeine, 功能上差别不大,但后者在性能上 ...

  5. 缓存框架EhCache的简单使用

    缓存框架EhCache的简单使用: 1.Spring和EhCache框架整合 1.1导入jar包 <dependencies> <dependency> <groupId ...

  6. Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解

    随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...

  7. 缓存框架Ehcache相关

    单点缓存框架   只能针对单个jvm中,缓存容器存放jvm中,每个缓存互不影响  Ehcache gauva chache 内置缓存框架 jvm缓存框架 分布式缓存框架(共享缓存数据)  Redis ...

  8. SpringMVC集成缓存框架Ehcache

    在互联网应用中,应用并发比传统企业及应用会高出很多.解决并发的根本在于系统的响应时间与单位时间的吞吐量.思路可分为:一减少系统的不必要开支(如缓存),二是提高系统单位时间内的运算效率(如集群). 在硬 ...

  9. Java进程内缓存

    今天和同事聊到了缓存,在Java中实现进程缓存.这里主要思想是,用一个map做缓存.缓存有个生存时间,过期就删除缓存.这里可以考虑两种删除策略,一种是起一个线程,定期删除过期的key.第二个是,剔除模 ...

随机推荐

  1. Determining IP information for eth1... failed; no link present. Check cable? 解决办法

    有时候会遇到这种问题 解决办法为 进入网卡配置,将 BOOTPROTO 改为 none 然后 ifconfig –a 查看可以得到 eth1 已经可以寻到 iP 地址,如下: Service netw ...

  2. 一百万数据索引实例測试--mysql

    推荐书籍:http://pan.baidu.com/s/1sjJIyRV 任务描写叙述: 如果一高频查询例如以下  SELECT * FROM user WHERE area='amoy' AND s ...

  3. C的xml编程文章链接

    官方地址:http://www.xmlsoft.org/ http://hi.baidu.com/singyea/item/ed4d1c335a9527b8633aff82 http://os.chi ...

  4. 【架构之路之WCF全析(一)】--服务协定及消息模式

    上周微软开公布会说.NET支持全然跨平台和并开放Core源代码的新闻,让我们顿时感到.NET要迎来它的春天.尽管早在几年前.NET就能开发Android和IOS,可是这次的跨平台把Linux都放到了微 ...

  5. iOS一些推荐的学习路径发展

    iOS论坛里有朋友要求回答帖子,帖子的标题是: 想学IOS开发高阶一点的东西,从何開始,然后我吧啦吧啦回答写了非常多.既然敲了那么多字,我就把我写的回复也贴到博客里来分享.希望能对大家有帮助.欢迎大家 ...

  6. EJB体系结构

    为了适应企业的快速发展.缩短企业信息系统的设计和开发周期.降低构建信息系统的成本,Sun公司制订了Java2 SDK Enterprise Edition(J2EE)规范,定义基于组件的方式设计.开发 ...

  7. TextView 使用自定义的字体和亮点

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/40478815 如今非常多应用中喜欢使用自己定义字体,今天我就来实如今TextView中使用自 ...

  8. Chromium

    Chromium多进程架构 多进程架构 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//Start_Here_Bac ...

  9. Delphi XE7 发布时间

    Delphi7 XE7 我可以下载: http://altd.embarcadero.com/download/radstudio/xe7/delphicbuilder_xe7_win.iso 安装包 ...

  10. 移植kl档,但调用默认Generic.kl解决的方法

    从飞机到现在移植模型来开发遥控器按键,调查发现,大部分的功能键始终没有任何效果. 跑 dumpsys input 要查看当前的关键kl, 查找KeyLayoutFile它原来是默认Generic.kl ...