亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot 2.0 Hibernate 5 EhCache 3 與 JCache

Spring Boot 2.0 Hibernate 5 EhCache 3 與 JCache

慕姐8265434 2022-12-15 14:52:59
我正在嘗試使用 EhCache 將 Hibernate 設置為二級緩存,但 TTL 不工作。這是我的依賴項:<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-cache</artifactId></dependency><dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-jcache</artifactId></dependency><dependency>  <groupId>org.ehcache</groupId>  <artifactId>ehcache</artifactId></dependency><dependency>  <groupId>javax.cache</groupId>  <artifactId>cache-api</artifactId></dependency>這是我的 YAML 配置:spring:  jpa:    show-sql: true    properties:      hibernate:        dialect: Dialect        cache:          use_second_level_cache: true          region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory          use_query_cache: true  cache:    jcache:      config: classpath:ehcache.xml這是我的實體類的配置方式:@[email protected]@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)public class PersonEntity {  //}以及實體的 JpaRepository:public interface PersonRepository extends JpaRepository<PersonEntity, Integer> {  @org.springframework.data.jpa.repository.QueryHints({      @javax.persistence.QueryHint(name = "org.hibernate.cacheable", value = "true")  })  List<PersonEntity> findByName(String name);}我已將緩存配置為在 2 秒后過期,但調用findByName仍然使用緩存(在第一個之后沒有打印 SQL 查詢)。這是ehcache.xml文件:<?xml version="1.0" encoding="UTF-8"?><config xmlns="http://www.ehcache.org/v3">  <cache-template name="simple">    <expiry>      <ttl>2</ttl>    </expiry>    <heap>100</heap>  </cache-template>  <cache alias="com.sample.PersonEntity" uses-template="simple"/></config>編輯: 我做了一些調試。我在以下位置添加了一個斷點org.ehcache.jsr107.ExpiryPolicyToEhcacheExpiry:javax.cache.expiry.Duration duration = this.expiryPolicy.getExpiryForCreation();由于某種原因,這個持續時間是無限的。那么也許配置設置不正確?我知道正在讀取 xml,因為當我使它無效時(例如通過刪除堆標記)我得到一個錯誤。
查看完整描述

2 回答

?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

我想我找到了問題的原因 - 您沒有指定ehcache.xml文件的位置:


spring:

  jpa:

    properties:

      hibernate:

        javax.cache:

          provider: org.ehcache.jsr107.EhcacheCachingProvider

          uri: classpath:ehcache.xml

        cache:

          use_second_level_cache: true

          region.factory_class: jcache

          use_query_cache: true

在這種情況下,Hibernate 使用默認配置創建緩存。我的演示項目日志中的一個片段:


17:15:19 WARN [main] org.hibernate.orm.cache: HHH90001006: Missing cache[user] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.javax.cache.missing_cache_strategy' to 'create'.



查看完整回答
反對 回復 2022-12-15
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

當您@Cacheable在實體頂部設置注釋時,它會創建一個區域,其中KEY是ID實體的,Value是實體。上面的意思是,如果您通過密鑰訪問,您將命中緩存ID。如果您使用 spring 數據和 findById,它將命中緩存。如果您創建一個方法 findByName,則訪問將不是按鍵 trerefore,因此它不會命中您的Cacheable注釋定義的緩存區域。另一方面,它會命中查詢緩存,但查詢緩存位于完全不同的區域。從您的配置來看,您根本沒有配置查詢緩存。要使此方法完全命中任何緩存,您需要使用此屬性添加它:


spring:jpa:properties:hibernate:cache:use_query_cache: true

或者,您可以在存儲庫方法之上指定@Cacheable,這樣可以定義一個新區域。


您可以配置默認緩存,這應該捕獲 StandardQueryCahache。


<defaultCache 

    maxElementsInMemory="10000"

    eternal="false"

    timeToIdleSeconds="3600"

    timeToLiveSeconds="3600">

  </defaultCache>

在 EhCache2 中,您可以通過此元素配置標準查詢緩存:


  <cache

name="org.hibernate.cache.internal.StandardQueryCache"

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="3600"

timeToLiveSeconds="3600">

雖然不確定它在 ehcache 3 中的表現如何。我相信它應該是一樣的,因為 StandartQueryCache 類是 hibernate 包的一部分,而不是 ehcache 包的一部分。


我還認為你需要設置

hibernate.javax.cache.provider = org.ehcache.jsr107.EhcacheCachingProvider


查看完整回答
反對 回復 2022-12-15
  • 2 回答
  • 0 關注
  • 312 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號