我在 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分钟后,缓存无法自动刷新,并且不会显示任何新的更改。
代码to remove cache无效。
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();
}
}
带有cacheName merchantServices的<?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
中答案 0 :(得分:-1)
基本上你正面临着ehcache autoflush问题,这里讨论的问题。
Problem with auto flush in ehcache
这里已经讨论过您的问题的解决方案。希望它可以帮助你,因为我不是一个ehcache专业人士..:P