簡體   English   中英

Spring + Hibernate + Ehcache使query_cache永不過期

[英]Spring+Hibernate+Ehcache make query_cache never expire

我正在嘗試在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中所做的配置不被認為是什么問題? 這是錯誤的地方嗎?

主要問題是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)I設置UpdateTimestampsCachetimeToIdleSecondstimeToLiveSeconds0

我按照您的建議做了,但是我不想在源目錄中移動ehcache.xml,所以我改用了以下方法:

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

而且效果很好。 (ehcache.xml在resources目錄中)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM