ehcache 缓存使用
来源:互联网 发布:电脑卡在欢迎知乎 编辑:程序博客网 时间:2024/05/14 05:24
一:详细配置步骤
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
- ehcache缓存的使用
- ehcache 缓存的使用
- Ehcache缓存的使用
- ehcache 缓存的使用
- spring使用ehcache缓存
- Ehcache 缓存使用
- ehcache 缓存使用
- ehcache缓存使用
- web缓存--Ehcache使用
- ehcache 缓存使用
- ehcache缓存的使用
- ehcache 缓存使用
- ehcache缓存使用
- ehcache 缓存使用
- ehcache 缓存使用
- Ehcache缓存的使用
- ehcache 缓存使用
- 缓存技术 oscache、ehcache使用之ehcache
- 通过Lua彻底明白了回调函数
- 将原有的MyEclipse中的项目转成maven项目----在原项目上修改路径配置
- mysql EXISTS用法
- 第14周-- 求矩阵对角线元素之和
- ASP.net 控件实现数据级联
- ehcache 缓存使用
- 第14周项目1 折腾二维数组1
- 在ArcEngine中的点元素如何使用其本身提供的各种符号特征
- 学习笔记之Oracle(简单应用)
- 希尔排序算法
- 嵌入式开发bug解决办法(持续更新中......)
- [linux]ubutnu12.04 下安装jdk1.7
- goagent不能用的处理办法(timeout)
- 为什么全世界只有13台根域名解析服务器