我在Spring Data、Hibernate、ehcache环境中使用查询缓存。ehcache 依赖项是compile 'org.hibernate:hibernate-ehcache:3.3.1.GA'
ehcache 依赖树是
+--- org.hibernate:hibernate-ehcache:3.3.1.GA
| +--- org.hibernate:hibernate-core:3.3.1.GA -> 3.6.6.Final (*)
| +--- net.sf.ehcache:ehcache:1.2.3
| | \--- commons-collections:commons-collections:2.1 -> 3.2.1
| \--- org.slf4j:slf4j-api:1.5.2 -> 1.6.5
我有一个resources/ehcache.xml
配置timeToLiveSeconds
为5 分钟。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="/home/prayag/cache_"/>
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="true"
diskPersistent="true"
timeToLiveSeconds="300"
diskExpiryThreadIntervalSeconds="300"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
缓存有效,但 5 分钟后,缓存不会自动刷新,也不会显示新的更改。
甚至没有遵循删除缓存的代码。
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
CacheManager cacheManager = CacheManager.getInstance();
String[] names = cacheManager.getCacheNames();
for (String name : names) {
if (name.equals("merchantServices")) {
Cache cache = cacheManager.getCache(name);
cache.removeAll();
}
}
我的缓存配置(jpa-context.xml)与 cacheName MercerServices 是
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"
default-autowire="byName">
<tx:annotation-driven />
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="merchantServices"/>
</set>
</property>
</bean>
</beans>
在diskStore缓存文件如下,
prayag@prayag:~/cache_$ ls -l
total 4
-rw-rw-r-- 1 prayag prayag 0 Sep 20 13:31 org.hibernate.cache.StandardQueryCache.data
-rw-rw-r-- 1 prayag prayag 0 Sep 20 15:26 org.hibernate.cache.StandardQueryCache.index
-rw-rw-r-- 1 prayag prayag 2268 Sep 20 15:24 org.hibernate.cache.UpdateTimestampsCache.data
-rw-rw-r-- 1 prayag prayag 0 Sep 20 15:26 org.hibernate.cache.UpdateTimestampsCache.index
org.hibernate.cache.UpdateTimestampsCache.data
有以下内容。
prayag@prayag:~/cache_$ vi org.hibernate.cache.UpdateTimestampsCache.data
¬í^@^Esr^@^Vnet.sf.ehcache.Element.e^H<90>Iî-<9d>^B^@^LJ^@^LcreationTimeZ^@^GeternalJ^@^HhitCountJ^@^NlastAccessTimeJ^@^NlastUpdateTimeZ^@^KlifespanSetJ^@^TnextToLastAccessTimeI^@
timeToIdleI^@
timeToLiveJ^@^GversionL^@^Ckeyt^@^RLjava/lang/Object;L^@^Evalueq^@~^@^Axp^@^@^AA:°<84>Ä^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^AA:°<84>Ä^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A,^@^@^AA:°<84>Ät^@^GServicesr^@^Njava.lang.Long;<8b>ä<90>Ì<8f>#ß^B^@^AJ^@^Evaluexr^@^Pjava.lang.Number<86>¬<95>^]^K<94>à<8b>^B^@^@xp^@^T^S«^HL0^@^@°^@¬í^@^Esr^@^Vnet.sf.ehcache.Element.e^H<90>Iî-<9d>^B^@^LJ^@^LcreationTimeZ^@^GeternalJ^@^HhitCountJ^@^NlastAccessTimeJ^@^NlastUpdateTimeZ^@^KlifespanSetJ^@^TnextToLastAccessTimeI^@
timeToIdleI^@
timeToLiveJ^@^GversionL^@^Ckeyt^@^RLjava/lang/Object;L^@^Evalueq^@~^@^Axp^@^@^AA:°<84>Ä^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^AA:°<84>Ä^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A,^@^@^AA:°<84>Ät^@^Lservice_tagssr^@^Njava.lang.Long;<8b>ä<90>Ì<8f>#ß^B^@^AJ^@^Evaluexr^@^Pjava.lang.Number<86>¬<95>^]^K<94>à<8b>^B^@^@xp^@^T^S«^HL0^@^T^S¥[\°^@¬í^@^Esr^@^Vnet.sf.ehcache.Element.e^H<90>Iî-<9d>^B^@^LJ^@^LcreationTimeZ^@^GeternalJ^@^HhitCountJ^@^NlastAccessTimeJ^@^NlastUpdateTimeZ^@^KlifespanSetJ^@^TnextToLastAccessTimeI^@
timeToIdleI^@
timeToLiveJ^@^GversionL^@^Ckeyt^@^RLjava/lang/Object;L^@^Evalueq^@~^@^Axp^@^@^AA:±%^Q^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^AA:±%^Q^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A,^@^@^AA:±%^Qt^@
SessionLogsr^@^Njava.lang.Long;<8b>ä<90>Ì<8f>#ß^B^@^AJ^@^Evaluexr^@^Pjava.lang.Number<86>¬<95>^]^K<94>à<8b>^B^@^@xp^@^T^S«^RQ^@^@^@¬í^@^Esr^@^Vnet.sf.ehcache.Element.e^H<90>Iî-<9d>^B^@^LJ^@^LcreationTimeZ^@^GeternalJ^@^HhitCountJ^@^NlastAccessTimeJ^@^NlastUpdateTimeZ^@^KlifespanSetJ^@^TnextToLastAccessTimeI^@
timeToIdleI^@
timeToLiveJ^@^GversionL^@^Ckeyt^@^RLjava/lang/Object;L^@^Evalueq^@~^@^Axp^@^@^AA:°<84>Ä^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^AA:°<84>Ä^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A,^@^@^AA:°<84>Ät^@^SServiceAccessStatussr^@^Njava.lang.Long;<8b>ä<90>Ì<8f>#ß^B^@^AJ^@^Evaluexr^@^Pjava.lang.Number<86>¬<95>^]^K<94>à<8b>^B^@^@xp^@^T^S«^HL0^@¬í^@^Esr^@^Vnet.sf.ehcache.Element.e^H<90>Iî-<9d>^B^@^LJ^@^LcreationTimeZ^@^GeternalJ^@^HhitCountJ^@^NlastAccessTimeJ^@^NlastUpdateTimeZ^@^KlifespanSetJ^@^TnextToLastAccessTimeI^@
timeToIdleI^@
timeToLiveJ^@^GversionL^@^Ckeyt^@^RLjava/lang/Object;L^@^Evalueq^@~^@^Axp^@^@^AA:°<84>Ä^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^AA:°<84>Ä^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A,^@^@^AA:°<84>Ät^@^HBankInfosr^@^Njava.lang.Long;<8b>ä<90>Ì<8f>#ß^B^@^AJ^@^Evaluexr^@^Pjava.lang.Number<86>¬<95>^]^K<94>à<8b>^B^@^@xp^@^T^S«^HL0^@¬í^@^Esr^@^Vnet.sf.ehcache.Element.e^H<90>Iî-<9d>^B^@^LJ^@^LcreationTimeZ^@^GeternalJ^@^HhitCountJ^@^NlastAccessTimeJ^@^NlastUpdateTimeZ^@^KlifespanSetJ^@^TnextToLastAccessTimeI^@
timeToIdleI^@
timeToLiveJ^@^GversionL^@^Ckeyt^@^RLjava/lang/Object;L^@^Evalueq^@~^@^Axp^@^@^AA:¼ó1^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^AA:¼ó1^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A,^@^@^AA:¼ó1t^@^KUserSessionsr^@^Njava.lang.Long;<8b>ä<90>Ì<8f>#ß^B^@^AJ^@^Evaluexr^@^Pjava.lang.Number<86>¬<95>^]^K<94>à<8b>^B^@^@xp^@^T^S«Ï3^@^@
我的更多配置在之前的帖子Spring Data + Hibernate Query Caching not working