ehcache FAQ中提到
Remember that a value in a cache element is globally accessible from multiple threads. It is inherently not thread safe to modify the value . It is safer to retrieve a value, delete the cache element and then reinsert the value.

The UpdatingCacheEntryFactory does work by modifying the contents of values in place in the cache. This is outside of the core of ehcache and is targeted at high performance CacheEntryFactories for SelfPopulatingCaches.

ehcache 是线程安全的吗?
如果一个频繁修改的表,会涉及到多线程,适合放入ehcache吗?(目前就是因为数据库IO压力过大,才想放入缓存)

ehcache查找时key:ID,那么如果我不是以ID为查询条件的话,是否就没有使用到缓存,
例如:我用电话号码来查询用户,发现缓存似乎没有用到,如何才能解决此问题?

Ehcache的类层次模型主要为三层,最上层的是CacheManager,这是操作Ehcache的入口。
可以通过CacheManager.getInstance()获得一个单子的CacheManger,或者通过CacheManger的构造函数创建一个新的CacheManger。
每个CacheManager都管理着多个Cache。
每个Cache都以一种类Hash的方式,关联着多个Element。而Element则是我们用于存放要缓存内容的地方。
在配置EhCache前需要引入两个开发包:ehcache-1.3.0.jar和commons-logging-1.04.jar
https://github.com/ehcache/ehcache3
http://www.ehcache.org/downloads/

Cache的配置很灵活,官方提供的Cache配置方式有好几种。你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。
你可以将Cache的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。以下是运行时配置的好处:
在同一个地方配置所有的Cache,这样很容易管理Cache的内存和磁盘消耗。
发布时可更改Cache配置。
可再安装阶段就检查出配置错误信息,而避免了运行时错误。

CacheManager配置
DmulticastGroupPort=4446,这样可以配置监听端口。

DiskStore配置
如果你使用的DiskStore(磁盘缓存),你必须要配置DiskStore配置项。如果不配置,Ehcache将会使用java.io.tmpdir。
diskStroe的“path”属性是用来配置磁盘缓存使用的物理路径的,Ehcache磁盘缓存使用的文件后缀名是.data和.index。
<disStore path=”java.io.tmpdir”/>

name:Cache的唯一标识
maxElementsInMemory:内存中最大缓存对象数。
maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。
eternal:Element是否永久有效,一但设置了,timeout将不起作用。
overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。
timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。
diskPersistent:是否缓存虚拟机重启期数据。(这个虚拟机是指什么虚拟机一直没看明白是什么,有高人还希望能指点一二)。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。
你可以设置为FIFO(先进先出)或是LFU(较少使用)。这里比较遗憾,Ehcache并没有提供一个用户定制策略的接口,仅仅支持三种指定策略,感觉做的不够理想。

http://www.cnblogs.com/discuss/articles/1866901.html

配置文件
例子:ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache maxElementsInMemory="2" eternal="false"
timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
<cache name="sampleCache1" maxElementsInMemory="5" eternal="false"
overflowToDisk="false" timeToIdleSeconds="1" timeToLiveSeconds="1"
memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>

注:
在ehcache的配置文件里面必须配置defaultCache。
每个<cache>标签定义一个新的cache,属性的含义基本上可以从名字上得到,详细的说明可以参考上面的链接。

所以使用EHCache大概的步骤为: 
第一步:生成CacheManager对象 
第二步:生成Cache对象 
第三步:向Cache对象里添加由key,value组成的键值对的Element元素 
示例程序:
例子:

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element; import java.util.List; /**
* Created by MyWorld on 2016/1/20.
*/
public class EHCacheDemo {
public static void main(String[] args) {
CacheManager manager = new CacheManager("ehcache.xml");
Cache cache = manager.getCache("sampleCache1");
for (int i = 0; i < 6; i++) {
Element e = new Element("key" + i, "value" + i);
cache.put(e);
} List keys = cache.getKeys();
for (Object key : keys) {
System.out.println(key + "," + cache.get(key));
}
}
}

注:程序的流程也是比较明晰的,首先是获取一个CacheManager,这是使用Ehcache的入口,然后通过名字获取某个Cache,然后就可以对Cache存取Element。Cache使用类Hash的方式来管理Element。
事件处理
说明:可以为CacheManager添加事件监听,当对CacheManager增删Cache时,事件处理器将会得到通知。要配置事件处理,需要通过ehcache的配置文件来完成。
配置文件:ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache maxElementsInMemory="2" eternal="false"
timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
<cache name="sampleCache1" maxElementsInMemory="5" eternal="false"
overflowToDisk="false" timeToIdleSeconds="1" timeToLiveSeconds="1"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory class="ehcache.test.CELF"/>
</cache>
</ehcache>

注:通过<cacheManagerEventListenerFactory>来注册事件处理器的工厂类。
代码:
package ehcache.test;
import java.util.Properties;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.CacheEventListener;
import net.sf.ehcache.event.CacheEventListenerFactory;
public class CELF extends CacheEventListenerFactory {
@Override
public CacheEventListener createCacheEventListener(Properties properties) {
return new CEL();
}
}
class CEL implements CacheEventListener {
public void dispose() {}
public void notifyElementEvicted(Ehcache cache, Element element) {}
public void notifyElementExpired(Ehcache cache, Element element) {}
public void notifyElementPut(Ehcache cache, Element element)
throws CacheException {
System.out.println(element.getKey() + " was added.");
}
public void notifyElementRemoved(Ehcache cache, Element element)
throws CacheException {
System.out.println(element.getKey() + " was removed.");
}
public void notifyElementUpdated(Ehcache cache, Element element)
throws CacheException {}
public void notifyRemoveAll(Ehcache cache) {}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
注:这里的代码与之前的类似,由此可见Ehcache的事件处理采用的是一种类plugin方式,也就是说,事件处理的添加是以不修改源代码为前提的。

http://blog.chinaunix.net/uid-20577907-id-2834484.html

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");  //这个路径的写法是eclipse中的格式。源码是通过new File的方法来初始化配置文件

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();
}

卸载CacheManager ,关闭Cache 
manager.shutdown();

2)使用Caches

取得配置文件中预先 定义的sampleCache1设置,通过CacheManager生成一个Cache
Cache cache = manager.getCache("sampleCache1");  
设置一个名为test 的新cache,test属性为默认

CacheManager manager = CacheManager.create();
manager.addCache("test");

设置一个名为test 的新cache,并定义其属性

CacheManager manager = CacheManager.create();
Cache cache = new Cache("test", 1, true, false, 5, 2);
manager.addCache(cache);

往cache中加入元素

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

从cache中取得元素
Element element = cache.get("key1");  
http://macrochen.blogdriver.com/macrochen/869480.html

使用Cache实例来操纵缓存了,主要的方法是:
Cache.get(Object key),
Cache.put(new Element(Object key, Object value)),
Cache.remove(Object key)。

ehCache浅谈(转)的更多相关文章

  1. ehcache的heap、off-heap、desk浅谈

    ehcache的heap.off-heap.desk浅谈   答: 从读取速度上比较:heap > off-heap > disk heap堆内内存: heap表示使用堆内内存,heap( ...

  2. Spring缓存框架原理浅谈

    运维在上线,无聊写博客.最近看了下Spring的缓存框架,这里写一下 1.Spring 缓存框架 原理浅谈 2.Spring 缓存框架 注解使用说明 3.Spring 缓存配置 + Ehcache(默 ...

  3. 浅谈 Fragment 生命周期

    版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. Fragment 是在 Android 3.0 中 ...

  4. 浅谈 LayoutInflater

    浅谈 LayoutInflater 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/View 文中如有纰漏,欢迎大家留言指出. 在 Android 的 ...

  5. 浅谈Java的throw与throws

    转载:http://blog.csdn.net/luoweifu/article/details/10721543 我进行了一些加工,不是本人原创但比原博主要更完善~ 浅谈Java异常 以前虽然知道一 ...

  6. 浅谈SQL注入风险 - 一个Login拿下Server

    前两天,带着学生们学习了简单的ASP.NET MVC,通过ADO.NET方式连接数据库,实现增删改查. 可能有一部分学生提前预习过,在我写登录SQL的时候,他们鄙视我说:“老师你这SQL有注入,随便都 ...

  7. 浅谈WebService的版本兼容性设计

    在现在大型的项目或者软件开发中,一般都会有很多种终端, PC端比如Winform.WebForm,移动端,比如各种Native客户端(iOS, Android, WP),Html5等,我们要满足以上所 ...

  8. 浅谈angular2+ionic2

    浅谈angular2+ionic2   前言: 不要用angular的语法去写angular2,有人说二者就像Java和JavaScript的区别.   1. 项目所用:angular2+ionic2 ...

  9. iOS开发之浅谈MVVM的架构设计与团队协作

    今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...

随机推荐

  1. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序 总结

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序 总结         在SP2013中,工作流已经从SP Server中脱离 ...

  2. Nginx将请求分发到各web应用

    介绍了VMWare12虚拟机.Linux(CentOS7)系统安装.部署Nginx1.6.3代理服务做负载均衡.接下来介绍通过Nginx将请求分发到各web应用处理服务. 一.Web应用开发 1.as ...

  3. Windows phone 8 学习笔记(8) 定位地图导航

    原文:Windows phone 8 学习笔记(8) 定位地图导航 Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模 ...

  4. Dreamer 3.0 支持json、xml、文件上传

    自己写的框架,功能类似Struts2.x 下载地址:http://pan.baidu.com/share/link?shareid=3273223286&uk=470382596 新增功能: ...

  5. net Mvc模块化开发

    Asp.net Mvc模块化开发之“部分版本部分模块更新(上线)” 项目开发从来就不是一个简单的问题.更难的问题是维护其他人开发的项目,并且要修改bug.如果原系统有重大问题还需要重构. 怎么重构系统 ...

  6. erlang shell表格数据对齐

    近期在erlang shell做一些測试,为了让測试结果数据显得更直观,想对齐须要打印的数据,做成像表格一样的效果. 開始的想法是在数据中插入tab. 当然,erlang也有对tab的支持,但实际效果 ...

  7. C++第11周(春)项目1 - 存储班长信息的学生类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目1 - 存储班长信息的学生类] clas ...

  8. 行人检测(Pedestrian Detection)资源整合

    一.纸 评论文章分类: [1] D. Geronimo, and A. M.Lopez. Vision-based Pedestrian Protection Systems for Intellig ...

  9. C++ - Identifier not found

     This is because forward declaration in C++: Compiler needs to know function prototype when functi ...

  10. Prototype Pattern 原型模式

    7.6 原型模式总结 原型模式作为一种快速创建大量相同或相似对象的方式,在软件开发中应用较为广泛,很多软件提供的复制(Ctrl + C)和粘贴(Ctrl + V)操作就是原型模式的典型应用,下面对该模 ...