2013-10-08 27 views
1

我想,在我的春節(3.2)+ Hibernate的(4.2)的應用程序,以使query_cache永不過期Spring + Hibernate的+的Ehcache使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.CacheableRe[email protected] 

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: [email protected] 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: [email protected] 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> 

編輯:

我的查詢使用標準的API生成:

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

我正在使用讀寫方式作爲CacheConcurrencyStrat埃及。所以,我在我的實體鎮我有這樣的:

<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不被視爲 出了什麼問題?這是錯誤的地方?

+0

您是否向查詢查詢的表中寫入數據?你確定查詢與前一個查詢完全相同,參數完全相同嗎? –

+0

@JBNizet我更新了我的問題 – Hidalgo

+0

我的測試場景很簡單:我觸發了我的JSF頁面,其中顯示城鎮列表,查詢被執行,所以實體被放到緩存中,1分鐘後,我刷新頁面,我看到我的日誌中沒有選擇查詢(緩存正在工作),並且我等待了2分鐘,然後再次刷新,結果:在緩存中找不到查詢結果。我會用更多的日誌更新我的問題。 – Hidalgo

回答

5

的主要問題是,ehcache.xml中沒有被AbstractEhcacheRegionFactory發現,所以我folowed下列步驟操作:

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 

org.hibernate.cache.UpdateTimestampsCache 

來自

org.hibernate.cache.spi.UpdateTimestampsCache 

3)我設置timeToIdleSecondstimeToLiveSeconds的 UpdateTimestampsCache

1

我做了你的建議,但我並不想移動ehcache.xml中的源目錄,所以相反我用這個:

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

它工作得很好。 (ehcache.xml位於資源目​​錄中)

相關問題