一:详细配置步骤

1,添加ehcache.xml文件

将ehcache.xml文件添加到src路径下面。ehcache.xml文件内容如下

  1. <ehcache>
  2. <diskStore path="java.io.tempdir" />
  3. <defaultCache maxElementsInMemory="1000" eternal="false"
  4. timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
  5. <cache name="ehcacheName" maxElementsInMemory="10000"
  6. eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000"
  7. overflowToDisk="true" />
  8. </ehcache>

2,添加spring配置文件

在applicContext.xml文件中添加

  1. <bean id="cacheManagerFactory"
  2. class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  3. p:configLocation="classpath:ehcache.xml"></bean>
  4. <!-- 声明cacheManager -->
  5. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
  6. p:cacheManager-ref="cacheManagerFactory" ></bean>

二:使用

1,定义EHCache工具方法

  1. public class EHCache {
  2. private static final CacheManager cacheManager = new CacheManager();
  3. private Cache cache;
  4. public EHCacheService(){
  5. this.cache=cacheManager.getCache("ehcacheName")
  6. }
  7. public Cache getCache() {
  8. return cache;
  9. }
  10. public void setCache(Cache cache) {
  11. this.cache = cache;
  12. }
  13. /*
  14. * 通过名称从缓存中获取数据
  15. */
  16. public Object getCacheElement(String cacheKey) throws Exception {
  17. net.sf.ehcache.Element e = cache.get(cacheKey);
  18. if (e == null) {
  19. return null;
  20. }
  21. return e.getValue();
  22. }
  23. /*
  24. * 将对象添加到缓存中
  25. */
  26. public void addToCache(String cacheKey, Object result) throws Exception {
  27. Element element = new Element(cacheKey, result);
  28. cache.put(element);
  29. }
  30. }

2,测试

  1. public class Test{
  2. EHCache ehCache = new EHCache();
  3. public void Test(){
  4. //测试将json对象存入缓存中
  5. JSONObject obj = new JSONObject();
  6. obj.put("name","lsz");
  7. ehCache.addToCache("cache_json",obj);
  8. //从缓存中获取
  9. JSONObject getobj = (JSONObject)ehCache.getCacheElement("cache_json");
  10. System.out.println(getobj.toString());
  11. }
  12. }

三:问题解决

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 缓存的更多相关文章

  1. [原创]mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  2. 深入探讨在集群环境中使用 EhCache 缓存系统

    EhCache 缓存系统简介 EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate 中默认的 CacheProvider. 下图是 EhCache 在应用 ...

  3. EhCache缓存

    EhCache缓存 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache是一种广泛使用的开源Java分布式缓 ...

  4. 我们究竟什么时候可以使用Ehcache缓存

    一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...

  5. mybatis0210 mybatis和ehcache缓存框架整合

    .1mybatis和ehcache缓存框架整合 一般不用mybatis来管理缓存而是用其他缓存框架在管理缓存,因为其他缓存框架管理缓存会更加高效,因为别人专业做缓存的而mybatis专业做sql语句的 ...

  6. (转)深入探讨在集群环境中使用 EhCache 缓存系统

    简介: EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate 中默认的 CacheProvider.本文充分的介绍了 EhCache 缓存系统对集群环境的 ...

  7. 我们究竟什么时候可以使用Ehcache缓存(转)

    一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...

  8. JAVAEE——BOS物流项目12:角色、用户管理,使用ehcache缓存,系统菜单根据登录人展示

    1 学习计划 1.角色管理 n 添加角色功能 n 角色分页查询 2.用户管理 n 添加用户功能 n 用户分页查询 3.修改Realm中授权方法(查询数据库) 4.使用ehcache缓存权限数据 n 添 ...

  9. Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存

    一  基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 -->    <bean cl ...

  10. Springboot整合Ehcache缓存

    Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...

随机推荐

  1. UVa 11021 - Tribles

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. 二模 (3) day1

    第一题: 题目描述: 一个数列定义如下:f(1) = 1,f(2) = 1,f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.给定 A,B 和 n 的值,要求计算 ...

  3. 注册表 ReadBool类型和 ReadInteger 的关系

    function TRegistry.ReadBool(const Name: string): Boolean; begin Result := ReadInteger(Name) <> ...

  4. 结构化视角看django

    分析一个软件的整体框架,我认为应该从静态和动态两方面入手.静态方面,看它有哪些用例,即有哪些功能模块:动态方面,看主流程如何连接上这些模块 静态方面:分View.Model.Template.Sess ...

  5. 网易云课堂 OCP

    数据库DBA任务: 管理数据库可用性 设计并创建数据库 管理物理结构 管理基于设计的存储 管理安全性 网络管理 备份与恢复 数据库调整与优化 关系型数据库(RDBMS) 多个表数据之间存在着关系 关系 ...

  6. MSP430G2553之timerA产生PWM

    总结:选SMCLK(可以测出来)         若选ACLK,经示波器PWM时有时无 举例一: #include <MSP430G2553.h> #define CPU_F ((doub ...

  7. iOS闪烁效果工具 GlitchLabel

    GlitchLabel 用于给文字添加闪烁效果,效果图如下: 开发环境: iOS 7.0+ Swift 2.2 Xcode 7 示例代码:https://www.yuedecaifu.com 1 2 ...

  8. VS2013开发Android App 环境搭建

    下载并安装vs2013,(安装时发现多了with blend,百度后有人说是设计师用版本,这是不对的,害我花费不少时间查找程序员用版本).我安装的是Microsoft Visual Studio Ul ...

  9. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  10. iOS线程

    昨天在项目中使用到了以前所没有使用过的线程,今天有时间来简单的学习一下. 一.线程的创建分为三种方法 (id)init; // designated initializer (id)initWithT ...