gpt4 book ai didi

Spring+Hibernate+Ehcache让query_cache永不过期

转载 作者:行者123 更新时间:2023-12-02 17:42:15 24 4
gpt4 key购买 nike

我正在尝试在我的 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 中,它是我在 documentation 中找到的默认位置

2) 我替换了这个:

org.hibernate.cache.StandardQueryCache 

org.hibernate.cache.internal.StandardQueryCache

还有这个

org.hibernate.cache.UpdateTimestampsCache

通过

org.hibernate.cache.spi.UpdateTimestampsCache

3) 我将 UpdateTimestampsCachetimeToIdleSecondstimeToLiveSeconds 设置为 0

关于Spring+Hibernate+Ehcache让query_cache永不过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19257670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com