<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)
很明显是访问了两次数据库,但是 我不知道为什么缓存无效,求大牛指教。。。