-
spring整合ehcache缓存不了数据5
大部分按照这个帖子上面说的做的http://www.cnblogs.com/lcuzhanglei/archive/2012/05/31/2528124.html,
但是数据就是缓存不了,每次测试都访问数据库。网上也没搜到答案
这个是我的ehcache.xml配置文件<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect"> <diskStore path="d:\\temp\\cache"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="demoCache" maxElementsInMemory="10000" maxElementsOnDisk="1000" eternal="false" overflowToDisk="true" diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /> </ehcache>
spring配置文件<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-lazy-init="true"> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>classpath:ehcache.xml</value> </property> </bean> <bean id="deptCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager" ref="cacheManager"/> <property name="beanName" value="democache"/> </bean> <bean id="cacheInterceptor" class="com.xiaolu.interceptor.MethodCacheInterceptor"> <property name="cache" ref="deptCache"/> </bean> <bean id="methodCachePointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="cacheInterceptor"/> <property name="patterns"> <list> <value>.*getAllDept</value> </list> </property> </bean> <bean id="deptService" class="com.xiaolu.service.DeptService"/> <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="deptService"/> <property name="interceptorNames"> <list> <value>methodCachePointcutAdvisor</value> </list> </property> </bean> </beans>
监听器package com.xiaolu.interceptor; import java.io.Serializable; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; public class MethodCacheInterceptor implements MethodInterceptor,InitializingBean { private Cache cache; @Override public Object invoke(MethodInvocation invocation) throws Throwable { String targetName = invocation.getThis().getClass().getName(); String methodName = invocation.getMethod().getName(); Object[] arguments = invocation.getArguments(); Object result = null; String cacheKey = getCacheKey(targetName, methodName, arguments); System.out.println("cacheKey:"+cacheKey); Element element = null; System.out.println("-----1-----"); synchronized(this){ element = cache.get(cacheKey); if(null == element){ result = invocation.proceed(); System.out.println("-----2----"); element = new Element(cacheKey, (Serializable)result); cache.put(element); } } return element.getValue(); } @Override public void afterPropertiesSet() throws Exception { Assert.notNull(cache, "A cache is required. Use setCache(Cache) to provide one."); } private String getCacheKey(String targetName,String methodName,Object[] arguments){ StringBuilder sb = new StringBuilder(); sb.append(targetName).append(".").append(methodName); if((arguments.length !=0)&&(arguments != null)){ for(int i=0;i<arguments.length;i++){ sb.append(".").append(arguments[i]); } } return sb.toString(); } public void setCache(Cache cache) { this.cache = cache; } }
访问数据库的类package com.xiaolu.service; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.xiaolu.pojo.Dept; public class DeptService { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; public List<Dept> getAllDept(){ String sql = "select * from dept"; List<Dept> list = new ArrayList<Dept>(); try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger"); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()){ Dept dept = new Dept(); dept.setDeptno(rs.getInt("deptno")); dept.setDname(rs.getString("dname")); dept.setLoc(rs.getString("loc")); list.add(dept); } } catch (Exception e) { e.printStackTrace(); } return list; } }
实体类package com.xiaolu.pojo; import java.io.Serializable; public class Dept implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private int deptno; private String dname; private String loc; public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } }
测试代码package test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xiaolu.pojo.Dept; import com.xiaolu.service.DeptService; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); DeptService service = (DeptService) context.getBean("proxyFactoryBean"); List<Dept> list = service.getAllDept(); for(Dept dept:list){ System.out.println(dept.getDname()); } } }
2013年3月13日 18:42
3个答案 按时间排序 按投票排序
-
采纳的答案
你测试方法有问题,在测试类里面new出ClassPathXmlApplicationContext,但是你下面用来从数据库取数据在同一个ClassPathXmlApplicationContext下只有一次,怎么可能会有缓存呢?,等于是每次都是new的不同的ClassPathXmlApplicationContext,每次都是new的不同的cache对象,你需要在测试中连续取几次数据。这样就可以看到结果
2013年3月14日 21:23
-
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); DeptService service = (DeptService) context.getBean("proxyFactoryBean"); for(int i=0;i<5;i++){ List<Dept> list = service.getAllDept(); for(Dept dept:list){ System.out.println(dept.getDname()); } }
在数据库查询方法中 添加日志信息 看查询了几次数据库2013年3月15日 13:54
-
缓存时间设置太短了
timeToIdleSeconds="300"
timeToLiveSeconds="600"
============================================
timeToIdleSeconds="30000"
timeToLiveSeconds="60000"2013年3月14日 10:39
相关推荐
基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!
配置ehcache缓存,存储内存的设置,与spring 的整合等
这是一个spring整合ehcache的完整用例,导入包后直接可以运行。ehcache.jar要导入1.2以上的,否则会报错。
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
以java配置类方式 整合EhCache来实现页面整体缓存及页面局部缓存。详情见博客http://blog.csdn.net/poorCoder_/article/details/56483954
spring整合了ehcache,实现了缓存功能。附带读取properties文件信息功能。
Ehcache 整合Spring 使用页面、对象缓存
缓存可以提高查询数据性能, 对同一批数据进行多次查询时, 第一次查询走数据库, 查询数据后,将数据保存在内存中,第二次以后查询 可以直接从内存获取数据,而不需要 和数据库进行交互。
本文先通过Ehcache独立应用的范例来介绍它的基本使用方法,然后再介绍与Spring整合的方法。
SpringBoo2.x整合Ehcache3.x作为缓存底层存储。
sping整合ehcache实现二级缓存,从根本上了解spring缓存机制,自己实现不用愁
在Spring+Hibernate集成环境中使用EhCache缓存做的开发测试,在线程中打开数据库查询数据,及关闭数据库后查询数据的结果.
Spring4.1+ehcache2.10.2实现缓存功能!
使用springboot 集成ehcache,里面附带数据库建库脚本,controller,service以及对ehcache集成,操作的详细样例,使用spring注解形式。
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认CacheProvider。这篇文章主要介绍了Spring Boot整合EhCache的步骤详解,需要的朋友可以参考下
当我们不指定具体其他第三方实现的时候,Spring Boot的Cache模块会使用ConcurrentHashMap来存储。而实际生产使用的时候,因为我们可能需要更多其他特性,往往就会采用其他缓存框架,所以接下来我们会分几篇分别介绍...
【EhCache】Java缓存框架使用EhCache结合Spring AOP EhCache是一个纯Java的进程内缓存框架,具有如下特点: 1. 快速简单,非常容易和应用集成。 2.支持多种缓存策略 。 3. 缓存数据有两级:内存和磁盘,因此无需...
NULL 博文链接:https://bijian1013.iteye.com/blog/2263270
项目简介:此项目只是简单的集成spring+springmvc+shiro+ehcahce 二: 步骤说明: 1:项目集成spring 在web.xml中配置spring容器的监听器。 2:项目集成springmvc 在web.xml中配置前端控制器 3:项目集成...