一:详细配置步骤
1,添加ehcache.xml文件
将ehcache.xml文件添加到src路径下面。ehcache.xml文件内容如下
<ehcache>
<diskStore path="java.io.tempdir" />
<defaultCache maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
<cache name="ehcacheName" maxElementsInMemory="10000"
eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000"
overflowToDisk="true" />
</ehcache>
2,添加spring配置文件
在applicContext.xml文件中添加
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"></bean>
<!-- 声明cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory" ></bean>
二:使用
1,定义EHCache工具方法
public class EHCache {
private static final CacheManager cacheManager = new CacheManager();
private Cache cache;
public EHCacheService(){
this.cache=cacheManager.getCache("ehcacheName")
}
public Cache getCache() {
return cache;
}
public void setCache(Cache cache) {
this.cache = cache;
}
/*
* 通过名称从缓存中获取数据
*/
public Object getCacheElement(String cacheKey) throws Exception {
net.sf.ehcache.Element e = cache.get(cacheKey);
if (e == null) {
return null;
}
return e.getValue();
}
/*
* 将对象添加到缓存中
*/
public void addToCache(String cacheKey, Object result) throws Exception {
Element element = new Element(cacheKey, result);
cache.put(element);
}
}
2,测试
public class Test{
EHCache ehCache = new EHCache();
public void Test(){
//测试将json对象存入缓存中
JSONObject obj = new JSONObject();
obj.put("name","lsz");
ehCache.addToCache("cache_json",obj);
//从缓存中获取
JSONObject getobj = (JSONObject)ehCache.getCacheElement("cache_json");
System.out.println(getobj.toString());
}
}
三:问题解决
1,框架环境是自己搭建的,添加ehcache后运行出错:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/cache]
Offending resource: class path resource [applicationContext.xml]
出现这种问题,原因是因为在applicationContext.xml文件中 多加了
<cache:annotation-driven cache-manager="cacheManager" /> 将其去掉即可
2,框架需要添加jar包
spring-context-support-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
分享到:
相关推荐
cache、ehcache等缓存使用,实现存对象读对象等等
当我们不指定具体其他第三方实现的时候,Spring Boot的Cache模块会...而实际生产使用的时候,因为我们可能需要更多其他特性,往往就会采用其他缓存框架,所以接下来我们会分几篇分别介绍几个常用优秀缓存的整合与使用。
从sebService获得数据,分页显示,如果不用缓存刚每显示都很慢。配置文件自动清空缓存。
mybatis添加ehcache缓存支持,基于maven项目管理
EHCache缓存的说明文档是到处找来的内容,都有参考链接指向原地址。有三个测试项目也在里面:一个整合了Struts2或Hibernate,一个整合了MyBatis3,这两个是我做的;另一个ehcachetest是下载了別人的。
Ehcache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存,Java EE 和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个 gzip 缓存 servlet 过滤器,支持 REST 和 SOAP api...
EHcache缓存框架,ehcache介绍与说明,Ehcache详细,EHcache集群环境配置
Ehcache缓存技术 Ehcache缓存技术 Ehcache缓存技术 Ehcache缓存技术 Ehcache缓存技术
对数据缓存技术的一个总结,文档比较全面的阐述了缓存类型,功能特性和相关技术。
ehcache缓存页面 ehcache缓存页面
(见下图,为了减少get这几条网络传输,我们会在每个应用服务器上增加本地的ehcache缓存作为二级缓存,即第一次get到的数据存入ehcache,后面output输出即可从本地ehcache中获取,不用再访问redis了,所以就减少了...
Ehcache缓存配置说明帮助你如何使用Ehcache
ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache...
Ehcache缓存配置
ehcache缓存的jar包和配置文件。
hibernate最核心的 就是缓存技术
springboot2.0使用ehcache缓存,这个压缩包时一个简单的demo。
ehcache缓存依赖的jar
ehcache缓存
本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-...