2 banyehaozi banyehaozi 于 2014.09.17 11:51 提问

spring MVC 3.1 ehcache 缓存无效
 <bean id="cacheManagerFactory"
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<bean id="cacheManager"  class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory" />
</bean>

我的spring 中配置的ehcache

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">  



<diskStore path="java.io.tmpdir"/>
<defaultCache
   maxElementsInMemory="1000"
   eternal="false"
   timeToIdleSeconds="120"
   timeToLiveSeconds="120"
   overflowToDisk="false"/>
<cache name="theCache"

   maxElementsInMemory="2000"
   eternal="false"
   overflowToDisk="true"
   diskPersistent="true"/>
<cache name="cacheTest"

   maxElementsInMemory="2000"
   eternal="false"
   overflowToDisk="true"
   diskPersistent="true"/>
</ehcache>

ehcache.xml 的配置

下面就是我的service 层

    @Cacheable(value="theCache",key="'customer'")
public Customer GetCustomerByCid(int cid){
    System.out.print("----------------------------------------------------------");
    return cd.GetCustomerByCid(cid);
}

结果 测试点击两次查询tomcat打印:
第一次:
DEBUG - Last-Modified value for [/jxc/customer/GetCustomerByCid] is: -1
----------------------------------------------------------DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@63921a] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8, UserName=root@7-PC, MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8, UserName=root@7-PC, MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: select * from Customer where cid=?
DEBUG - ==> Parameters: 18(Integer)

第二次:

DEBUG - Last-Modified value for [/jxc/customer/GetCustomerByCid] is: -1
----------------------------------------------------------DEBUG - Creating a new SqlSession
DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19cdf24] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8, UserName=root@7-PC, MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8, UserName=root@7-PC, MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: select * from Customer where cid=?
DEBUG - ==> Parameters: 18(Integer)

很明显是访问了两次数据库,但是 我不知道为什么缓存无效,求大牛指教。。。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!