Spring + Hibernate + Ehcache使query_cache永不过期

Hid*_*lgo 1 spring hibernate ehcache

我正在尝试,在我的Spring(3.2)+ Hibernate(4.2)应用程序中,使query_cache永不过期

我尝试了以下配置,缓存正在运行但120秒后,我的可缓存查询命中数据库,即使timeToIdleSecondstimeToLiveSeconds设置为大于120的值

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
     updateCheck="false">  

 <defaultCache
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="3600"
    overflowToDisk="true"
    maxElementsOnDisk="10000000"
    diskPersistent="false"
    diskExpiryThreadIntervalSeconds="1800"  />

<cache name="org.hibernate.cache.StandardQueryCache"
    maxEntriesLocalHeap="25"
    eternal="false"
    timeToIdleSeconds="1800"        
    timeToLiveSeconds="3600">
        <persistence strategy="localTempSwap"/>
</cache>

<cache name="org.hibernate.cache.UpdateTimestampsCache"
    maxEntriesLocalHeap="5000"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="3600"
    eternal="false">
        <persistence strategy="localTempSwap" />
</cache>
</ehcache>
Run Code Online (Sandbox Code Playgroud)

我包括Ehcache使用:

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.6.6</version>
    </dependency>        

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>4.2.6.Final</version>
    </dependency>     

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency> 
Run Code Online (Sandbox Code Playgroud)

注意:我已经尝试过:timeToIdleSeconds ="0"和timeToLiveSeconds ="0"但没有运气,我得到相同的行为,120秒并清除缓存.这是完整的日志:

21:52:11,128 DEBUG StandardQueryCache:131 - Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache

21:52:11,129 DEBUG EhcacheGeneralDataRegion:69 - key: sql: select this_.townID as townID1_33_1_, this_.name as name2_33_1_, this_.zip_code as code_pos3_33_1_, this_.regionID as regionID4_33_1_, region2_.regionId as regionId1_27_0_, region2_.name as name2_27_0_ from town this_ left outer join region region2_ on this_.regionID=region2_.regionId; parameters: ; transformer: org.hibernate.transform.CacheableResultTransformer@13a525

21:52:11,129 DEBUG EhcacheGeneralDataRegion:76 - Element for key sql: select this_.townID as townID1_33_1_, this_.name as name2_33_1_, this_.zip_code as code_pos3_33_1_, this_.regionID as regionID4_33_1_, region2_.regionId as regionId1_27_0_, region2_.name as name2_27_0_ from town this_ left outer join region region2_ on this_.regionID=region2_.regionId; parameters: ; transformer: org.hibernate.transform.CacheableResultTransformer@13a525 is null

21:52:11,129 DEBUG StandardQueryCache:137 - Query results were not found in cache

Hibernate: select this_.townID as townID1_33_1_, this_.name as name2_33_1_, this_.zip_code as code_pos3_33_1_, this_.regionID as regionID4_33_1_, region2_.regionId as regionId1_27_0_, region2_.name as name2_27_0_ from town this_ left outer join region region2_ on this_.regionID=region2_.regionId

21:52:11,137 DEBUG StandardQueryCache:104 - Caching query results in region: org.hibernate.cache.internal.StandardQueryCache; timestamp=5657678361137154

21:52:11,138 DEBUG EhcacheGeneralDataRegion:100 - key: sql: select this_.townID as townID1_33_1_, this_.name as name2_33_1_, this_.zip_code as code_pos3_33_1_, this_.regionID as regionID4_33_1_, region2_.regionId as regionId1_27_0_, region2_.name as name2_27_0_ from town this_ left outer join region region2_ on this_.regionID=region2_.regionId; parameters: ; transformer: org.hibernate.transform.CacheableResultTransformer@13a525 value: [5657678361137154, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Run Code Online (Sandbox Code Playgroud)

我使用以下命令激活了二级缓存和query_cache:

<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop> 
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>                
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
Run Code Online (Sandbox Code Playgroud)

编辑:

我的查询是使用Criteria API生成的:

public List<Town> readAll() {
  Criteria crit = getCurrentSession().createCriteria(Town.class);
  crit.setCacheable(true);
  return crit.list();
}
Run Code Online (Sandbox Code Playgroud)

我正在使用读写作为CacheConcurrencyStrategy.所以我是我的实体城镇我有这个:

<cache usage="read-write" />
Run Code Online (Sandbox Code Playgroud)

编辑: 我刚开始申请时看到了这个:

22:10:45,570  WARN ConfigurationFactory:136 - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%207.0/work/Catalina/localhost/projet/loader/ehcache-failsafe.xml
22:00:49,812  INFO UpdateTimestampsCache:61 - HHH000250: Starting update timestamps cache at region: org.hibernate.cache.spi.UpdateTimestampsCache
22:00:49,818  WARN AbstractEhcacheRegionFactory:180 - HHH020003: Could not find a specific ehcache configuration for cache named [org.hibernate.cache.spi.UpdateTimestampsCache]; using defaults.
Run Code Online (Sandbox Code Playgroud)

我认为我在WEB-INF/ehcache.xml中所做的配置不被认为是错的?这是错误的地方吗?

Hid*_*lgo 5

主要问题是AbstractEhcacheRegionFactory找不到ehcache.xml,所以我接下来是这些步骤:

1)我删除了:

<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
Run Code Online (Sandbox Code Playgroud)

我把ehcache.xml放在src/main/java中,这是我在文档中找到的默认位置

2)我替换了这个:

org.hibernate.cache.StandardQueryCache 
Run Code Online (Sandbox Code Playgroud)

通过

org.hibernate.cache.internal.StandardQueryCache
Run Code Online (Sandbox Code Playgroud)

还有这个

org.hibernate.cache.UpdateTimestampsCache
Run Code Online (Sandbox Code Playgroud)

通过

org.hibernate.cache.spi.UpdateTimestampsCache
Run Code Online (Sandbox Code Playgroud)

3)I设置timeToIdleSecondstimeToLiveSecondsUpdateTimestampsCache0