Mola de arranque 2,0 hibernação 5 EhCache 3 com JCache

George:

Eu estou tentando configurar Hibernate com EhCache como o cache de segundo nível, mas o TTL não está funcionando.

Aqui estão as minhas dependências:

<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>

Aqui está a minha configuração 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

Veja como minha classe Entity é configurado:

@Entity
@javax.persistence.Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class PersonEntity {
  //
}

E o JpaRepository para a entidade:

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);
}

Eu configurei o cache para expirar em 2 segundos, mas chamando findByNameainda usa o cache (não há consultas SQL impressos após o primeiro).

Aqui está o ehcache.xmlarquivo:

<?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>

EDIT: Eu fiz alguma depuração. Eu adicionei um break point em org.ehcache.jsr107.ExpiryPolicyToEhcacheExpiry:

javax.cache.expiry.Duration duration = this.expiryPolicy.getExpiryForCreation();

Esta duração é infinito por algum motivo. Então, talvez a configuração não está definida corretamente? Eu sei que o xml está sendo lido porque quando eu invalidá-lo (através da remoção de tag pilha por exemplo) eu recebo um erro.

digite descrição da imagem aqui

Cepr0:

Acho que encontrei a causa do problema - você não especificou a localização do ehcache.xmlarquivo:

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

neste caso, Hibernate cria um cache com a configuração padrão. Um fragmento do meu log projeto de demonstração:

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'.

Acho que você gosta

Origin http://43.154.161.224:23101/article/api/json?id=206426&siteId=1
Recomendado
Clasificación