【问题标题】:hibernate ehcache timeToLiveSeconds not working休眠 ehcache timeToLiveSeconds 不起作用
【发布时间】:2013-09-25 17:13:35
【问题描述】:

我在 Spring DataHibernateehcache 环境中使用查询缓存。 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();
    }
}

我的缓存配置(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

参考文献

refresh/reset EHCache regions

【问题讨论】:

    标签: spring hibernate caching ehcache


    【解决方案1】:

    基本上你会遇到这里讨论的 ehcache autoflush 问题。

    Problem with auto flush in ehcache

    这里已经讨论过您的问题的解决方案。希望它对你有所帮助,因为我不是 ehcache pro .. :P

    How Configuration Affects Element Flushing and Eviction

    【讨论】:

    • 请不要在答案中使用 txtspk。
    • @DarkHorse 甚至没有添加&lt;cache name="merchantServices" eternal="false" timeToIdleSeconds="240" timeToLiveSeconds="240" memoryStoreEvictionPolicy="LFU"&gt; &lt;/cache&gt; 工作。不过感谢您的链接。
    猜你喜欢
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2017-08-12
    • 2012-12-11
    • 2011-07-13
    相关资源
    最近更新 更多