ehcache 缓存
一:详细配置步骤
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 缓存的更多相关文章
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- 深入探讨在集群环境中使用 EhCache 缓存系统
EhCache 缓存系统简介 EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate 中默认的 CacheProvider. 下图是 EhCache 在应用 ...
- EhCache缓存
EhCache缓存 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache是一种广泛使用的开源Java分布式缓 ...
- 我们究竟什么时候可以使用Ehcache缓存
一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...
- mybatis0210 mybatis和ehcache缓存框架整合
.1mybatis和ehcache缓存框架整合 一般不用mybatis来管理缓存而是用其他缓存框架在管理缓存,因为其他缓存框架管理缓存会更加高效,因为别人专业做缓存的而mybatis专业做sql语句的 ...
- (转)深入探讨在集群环境中使用 EhCache 缓存系统
简介: EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate 中默认的 CacheProvider.本文充分的介绍了 EhCache 缓存系统对集群环境的 ...
- 我们究竟什么时候可以使用Ehcache缓存(转)
一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...
- JAVAEE——BOS物流项目12:角色、用户管理,使用ehcache缓存,系统菜单根据登录人展示
1 学习计划 1.角色管理 n 添加角色功能 n 角色分页查询 2.用户管理 n 添加用户功能 n 用户分页查询 3.修改Realm中授权方法(查询数据库) 4.使用ehcache缓存权限数据 n 添 ...
- Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存
一 基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 --> <bean cl ...
- Springboot整合Ehcache缓存
Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...
随机推荐
- 二模 (8) day2
第一题: 题目描述: 有 n 个炸弹,有些炸弹牵了一根单向引线(也就是说引线只有在这一端能被炸弹点燃),只要引爆了这个炸弹,用引线连接的下一个炸弹也会爆炸.每个炸弹还有个得分,当这个炸弹被引爆后就能得 ...
- Redis系列-存储篇hash主要操作函数小结
阳光透过玻璃,洒在身上,一杯暖茶在手,说不尽的安逸自得,让我有种想再写篇blog的冲动.上篇主要谈了string,这里谈谈hash吧!hash是一些列key value(field value)的映射 ...
- ie9,10 uploadify cleanUp bug
起因:ie多次加载uploadify3.2版本这个组件的时候,出现了SCRIPT5007: 缺少对象. From:http://blog.163.com/xiangfei209@126/blog/s ...
- Quartz2D
http://donbe.blog.163.com/blog/static/138048021201052093633776/ 详解 代码如下: DJView 绘制线段 基本图形 // // DJVi ...
- Spring学习笔记之整合struts
1.现有项目是通过 <action path="/aaaaAction" type="org.springframework.w ...
- php的预定义数组
PHP预定义变量数组 1.$_SERVER 变量由Web服务器设定或者直接与当前的脚本的执行环境相关联 $_SERVER超级全局变量包含由web服务器创建的信息,它提供了服务器和客户配置及当前请求环境 ...
- ios页面间传递参数四种方式
ios页面间传递参数四种方式 1.使用SharedApplication,定义一个变量来传递. 2.使用文件,或者NSUserdefault来传递 3.通过一个单例的class来传递 4.通过Dele ...
- ArrayList的使用方法【转载】
*** Source URL: http://i.yesky.com/bbs/jsp/view.jsp?articleID=889992&forumID=150 *** 1.什么是ArrayL ...
- lightoj1082 线段树
//Accepted 5596 KB 396 ms //线段树求区间最小值 #include <cstdio> #include <cstring> #include < ...
- c++高质量编程手册
怡化主管强烈要求我读这本书.... 笔记尚未完成,持续更新呗.. 第1章 高质量软件开发之道 1.1 软件质量基本概念 1.1.1 如何理解软件的质量:功能性和非公能性 1.1.2 提高软件质量的基本 ...