hibernate ehcache timeToLiveSeconds not working

2019-09-01 21:18发布

I am using query cache in Spring Data, Hibernate, ehcache environment. The ehcache dependency is compile 'org.hibernate:hibernate-ehcache:3.3.1.GA'

The ehcache dependency tree is

+--- 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

I have a resources/ehcache.xml with timeToLiveSeconds configured to 5 minutes.

<?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>

Caching works but after 5 minutes, cache doesn't get auto-flushed and no new changes are shown.

Not even following code to remove cache works.

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();
    }
}

My Caching configuration(jpa-context.xml) with cacheName merchantServices is

<?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>

Caching files at diskStore are as below,

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 has following content.

 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^@^@

More of my configuration is here in earlier post Spring Data + Hibernate Query Caching not working

References

refresh/reset EHCache regions

1条回答
三岁会撩人
2楼-- · 2019-09-01 21:36

Basically you are facing ehcache autoflush problem which is discussed over here.

Problem with auto flush in ehcache

There is a solution to your problem which has been discussed over here. hope it helps you coz I aint an ehcache pro.. :P

How Configuration Affects Element Flushing and Eviction

查看更多
登录 后发表回答