Spring + Hibernate + Ehcache使query_cache永不过期

时间:2013-10-08 20:26:34

标签: spring hibernate ehcache

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

我尝试了以下配置,缓存正在运行但120秒后,即使 timeToIdleSeconds timeToLiveSeconds 设置为更大的值,我的可缓存查询也会访问数据库那么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中所做的配置不予考虑 怎么了?这是错误的地方吗?

2 个答案:

答案 0 :(得分:5)

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

1)我删除了:

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

我把ehcache.xml放在 src / main / java 中,这是我在documentation

中找到的默认位置

2)我替换了这个:

org.hibernate.cache.StandardQueryCache 

通过

org.hibernate.cache.internal.StandardQueryCache

AND this

org.hibernate.cache.UpdateTimestampsCache

通过

org.hibernate.cache.spi.UpdateTimestampsCache

3)我将 timeTimestampsCache timeToIdleSeconds timeToLiveSeconds 设置为 0

答案 1 :(得分:1)

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

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

它运作得很好。 (ehcache.xml位于资源目​​录中)