- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
EHcache教程
HYPERLINK /hoojo/archive/2012/07/12/2587556.html Ehcache 整合Spring 使用页面、对象缓存
Ehcache在很多项目中都出现过,用法也比较简单。一 般的加些配置就可以了,而且Ehcache可以对页面、对象、数据进行缓存,同时支持集群/分布式缓存。如果整合Spring、Hibernate也非常 的简单,Spring对Ehcache的支持也非常好。EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分布式的 Cache,可以作为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩 提高响应速度。
一、准备工作
如果你的系统中已经成功加入Spring、Hibernate;那么你就可以进入下面Ehcache的准备工作。
1、 下载jar包
Ehcache 对象、数据缓存: HYPERLINK /downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gzbucket=tcdistributionsfile=ehcache-core-2.5.2-distribution.tar.gz /downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gzbucket=tcdistributionsfile=ehcache-core-2.5.2-distribution.tar.gz
Web页面缓存: HYPERLINK /downloads/destination?name=ehcache-web-2.0.4-distribution.tar.gzbucket=tcdistributionsfile=ehcache-web-2.0.4-distribution.tar.gz /downloads/destination?name=ehcache-web-2.0.4-distribution.tar.gzbucket=tcdistributionsfile=ehcache-web-2.0.4-distribution.tar.gz
2、 需要添加如下jar包到lib目录下
ehcache-core-2.5.2.jar
ehcache-web-2.0.4.jar 主要针对页面缓存
3、 当前工程的src目录中加入配置文件
ehcache.xml
ehcache.xsd
这些配置文件在ehcache-core这个jar包中可以找到
二、Ehcache基本用法
CacheManager cacheManager = CacheManager.create();
// 或者
cacheManager = CacheManager.getInstance();
// 或者
cacheManager = CacheManager.create(/config/ehcache.xml);
// 或者
cacheManager = CacheManager.create(http://localhost:8080/test/ehcache.xml);
cacheManager = CacheManager.newInstance(/config/ehcache.xml);
//
?
// 获取ehcache配置文件中的一个cache
Cache sample = cacheManager.getCache(sample);
// 获取页面缓存
BlockingCache cache = new BlockingCache(cacheManager.getEhcache(SimplePageCachingFilter));
// 添加数据到缓存中
Element element = new Element(key, val);
sample.put(element);
// 获取缓存中的对象,注意添加到cache中对象要序列化 实现Serializable接口
Element result = sample.get(key);
// 删除缓存
sample.remove(key);
sample.removeAll();
?
// 获取缓存管理器中的缓存配置名称
for (String cacheName : cacheManager.getCacheNames()) {
System.out.println(cacheName);
}
// 获取所有的缓存对象
for (Object key : cache.getKeys()) {
文档评论(0)