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 ...
随机推荐
- 二模 (2) day2
第一题: 题目描述: 在一个长方形框子里,最多有 N(0≤N≤6)个相异的点.在其中任何-个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能放 ...
- ASP.NET MVC学习之路由篇(2)
7.解决与物理路径的冲突 当发送一个请求至ASP.NET MVC时,其实会检查网站中存不存在这个请求的物理路径文件,如果存在的话,就会直接将这个物理文件返回.但是有时候我们需要它执行控制器的某个方法, ...
- Validform自定义提示效果-使用自定义弹出框
$(function(){ $.Tipmsg.r=null; $("#add").Validform({ tiptype:function(msg){ layer.msg(msg) ...
- ionic build --release android
ionic bulid android ionic build --release android keytool -genkey -v -keystore demo.keystore -alias ...
- hibernate框架
在之前的DAO开发中,对关系型数据库进行增删改查都是直接通过sql语句,需要人工的进行对象和表之间的转换.而Hibernate提供了对象和表之间进行映射的框架,使得这种转换更加方便. 1.ORM概念 ...
- bzoj 2595 斯坦纳树
题目大意: 选定一些格子保证景点对应的格子通过这些格子连通,保证选定的所有格子对应的权值和最小 这是相当于理解为将所有点形成的最小生成树 这里点的个数很少,所以可以对每一个点进行状态压缩 f[st][ ...
- Tasklist and TaskKill
C:\Users\Administrator>tasklist /? TASKLIST [/S system [/U username [/P [password]]]] [/M ...
- MATLAB中mexFunction函数的接口规范(转载)
MEX文件的调用极为方便,其调用方式与MATALAB的内建函数完全相同,只需要在命令窗口内输入对应的文件名称即可. C语言MEX程序代码文件有计算子例程(Computational routine)和 ...
- Line计划今年全面进军中国市场:建立本地团队
北京时间6月13日下午消息,<华尔街日报>报道称,移动消息应用Line计划于今年晚些时候进军中国市场.Line将在中国建立本地团队,开发内容和功能,从而进一步开拓中国这一全球最大的移动市场 ...
- pyqt5 笔记(三)py2exe 实现代码打包exe
python3.4 安装64位的版本 py2exe 下载地址: https://pypi.python.org/pypi/py2exe/0.9.2.0#downloads cmd——>进入pyf ...