You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Hazelcast 3.11与Hibernate 5.3,如何配置查询缓存区域?

Absolutely, you can configure query cache regions using Hazelcast maps—your issue comes down to using incorrect region names and misconfigured parameter keys. Let's break this down step by step.

1. Correct Default Query Cache Region Name

In Hibernate 5.3, the default query results cache region is named org.hibernate.cache.internal.StandardQueryCache. This is the exact map name Hazelcast uses for the default query cache, so your earlier attempts with org.hibernate.cache.internal.* (wildcards don't work for map names in Hazelcast 3.11 unless you explicitly configure wildcard maps, which isn't necessary here) and default-query-results-region (this isn't the standard default name) won't target the right region.

If you're using custom named query cache regions (e.g., via @Cacheable(cacheRegion = "my-custom-query-region")), just swap the default name with your custom region name in the Hazelcast config.

2. Fix Hazelcast Configuration Parameters

Your previous config used incorrect parameter names for TTL and TTI. In Hazelcast 3.11, the correct keys require a -seconds suffix. Here's the corrected YAML configuration for the default query cache region:

hazelcast:
  map:
    org.hibernate.cache.internal.StandardQueryCache:
      eviction-policy: LFU
      in-memory-format: BINARY
      max-size: 1000
      max-size-policy: PER_NODE
      time-to-live-seconds: 10
      time-to-idle-seconds: 10

If you prefer XML configuration (common for Hazelcast setups):

<hazelcast>
  <map name="org.hibernate.cache.internal.StandardQueryCache">
    <eviction-policy>LFU</eviction-policy>
    <in-memory-format>BINARY</in-memory-format>
    <max-size policy="PER_NODE">1000</max-size>
    <time-to-live-seconds>10</time-to-live-seconds>
    <time-to-idle-seconds>10</time-to-idle-seconds>
  </map>
</hazelcast>

3. Don't Forget the Update Timestamps Cache

Query cache relies on the update timestamps cache to validate if cached results are still fresh. You should configure this region too (default name: org.hibernate.cache.internal.UpdateTimestampsCache):

hazelcast:
  map:
    org.hibernate.cache.internal.UpdateTimestampsCache:
      eviction-policy: LRU
      in-memory-format: BINARY
      max-size: 5000
      max-size-policy: PER_NODE
      time-to-live-seconds: 300
      time-to-idle-seconds: 300

4. Verify Hibernate Query Cache is Enabled

Double-check your Hibernate config to ensure query caching is turned on:
In hibernate.cfg.xml:

<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastCacheRegionFactory</property>

Or in application.properties (if using Spring Boot):

hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory

Why Your Previous Configs Failed

  • org.hibernate.cache.internal.*: Hazelcast map names don't support arbitrary wildcards in standard configurations, so this didn't match any actual region.
  • default-query-results-region: This isn't the standard default region name for Hibernate's query cache in 5.3.
  • Incorrect parameter names: time-to-live and time-to-idle aren't valid Hazelcast map config keys—you need to use the -seconds suffix versions.

内容的提问来源于stack exchange,提问作者Oleg Kuts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 09:37:46