2

我正在尝试在我的 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>

我使用以下方法包含了 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> 

注意:我已经尝试过: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]

我使用以下方法激活了二级缓存和 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>

编辑:

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

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

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

<cache usage="read-write" />

编辑: 我刚刚在启动我的应用程序时看到了这个:

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.

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

4

2 回答 2

5

主要问题是AbstractEhcacheRegionFactory没有找到ehcache.xml,所以我按照以下步骤操作:

1)我删除:

<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>

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

2)我替换了这个:

org.hibernate.cache.StandardQueryCache 

经过

org.hibernate.cache.internal.StandardQueryCache

和这个

org.hibernate.cache.UpdateTimestampsCache

经过

org.hibernate.cache.spi.UpdateTimestampsCache

3)我将UpdateTimestampsCachetimeToIdleSecondstimeToLiveSeconds设置为0

于 2013-10-08T22:41:07.377 回答
1

我按照你的建议做了,但我不想在源目录中移动 ehcache.xml,所以我使用了这个:

<prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop>

它工作得很好。(ehcache.xml 在资源目录中)

于 2014-04-20T08:03:27.457 回答