问题描述
我的spring中配置的ehcache<beanid="cacheManagerFactory"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><propertyname="configLocation"value="classpath:ehcache.xml"/></bean><beanid="cacheManager"class="org.springframework.cache.ehcache.EhCacheCacheManager"><propertyname="cacheManager"ref="cacheManagerFactory"/></bean>
ehcache.xml的配置<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"><diskStorepath="java.io.tmpdir"/><defaultCachemaxElementsInMemory="1000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"/><cachename="theCache"maxElementsInMemory="2000"eternal="false"overflowToDisk="true"diskPersistent="true"/><cachename="cacheTest"maxElementsInMemory="2000"eternal="false"overflowToDisk="true"diskPersistent="true"/></ehcache>
下面就是我的service层@Cacheable(value="theCache",key="'customer'")publicCustomerGetCustomerByCid(intcid){System.out.print("----------------------------------------------------------");returncd.GetCustomerByCid(cid);}
结果测试点击两次查询tomcat打印:第一次:DEBUG-Last-Modifiedvaluefor[/jxc/customer/GetCustomerByCid]is:-1----------------------------------------------------------DEBUG-CreatinganewSqlSessionDEBUG-SqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@63921a]wasnotregisteredforsynchronizationbecausesynchronizationisnotactiveDEBUG-FetchingJDBCConnectionfromDataSourceDEBUG-JDBCConnection[jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8,UserName=root@7-PC,MySQL-ABJDBCDriver]willnotbemanagedbySpringDEBUG-oooUsingConnection[jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8,UserName=root@7-PC,MySQL-ABJDBCDriver]DEBUG-==>Preparing:select*fromCustomerwherecid=?DEBUG-==>Parameters:18(Integer)第二次:DEBUG-Last-Modifiedvaluefor[/jxc/customer/GetCustomerByCid]is:-1----------------------------------------------------------DEBUG-CreatinganewSqlSessionDEBUG-SqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@19cdf24]wasnotregisteredforsynchronizationbecausesynchronizationisnotactiveDEBUG-FetchingJDBCConnectionfromDataSourceDEBUG-JDBCConnection[jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8,UserName=root@7-PC,MySQL-ABJDBCDriver]willnotbemanagedbySpringDEBUG-oooUsingConnection[jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8,UserName=root@7-PC,MySQL-ABJDBCDriver]DEBUG-==>Preparing:select*fromCustomerwherecid=?DEBUG-==>Parameters:18(Integer)很明显是访问了两次数据库,但是我不知道为什么缓存无效,求大牛指教。。。