Spring数据:休眠查询缓存不适用于派生查询

我正在尝试为Spring Data中由方法名派生的查询设置Hibernate查询缓存,但是查询缓存从未使用过,甚至似乎没有激活。

我将使用带有JCache扩展名的Spring Boot 2.2.1,Kotlin 1.3.50caffeine作为我的基础Hibernate缓存。

我的存储库如下:

@Repository
interface libraryRepository : JpaRepository<library,Long> {
  @QueryHints(QueryHint(name = CACHEABLE,value = "true"))
  override fun findAll(sort: Sort): List<library>
}

二级缓存处于活动状态并且正在工作,并且已在库实体上正确设置:

@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE,region = "cache.library")
class library ( ... )

我的配置摘录:

spring:
  jpa.properties:
    javax.persistence.sharedCache.mode: ENABLE_SELECTIVE
    hibernate:
      generate_statistics: true
      session.events.log: true
  hibernate:
    cache:
      use_second_level_cache: true
      use_query_cache: true
      region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
    javax.cache.provider: com.github.benmanes.caffeine.jcache.spi.caffeineCachingProvider

但是,在查询libraryRepository.findAll(Sort.by("name"))时,总是会查询数据库。

关于查询缓存的日志中未提及任何内容。

首次调用(应将其放入缓存中):

2019-12-16 18:14:09.955 DEBUG 20400 --- [nio-8080-exec-1] org.hibernate.SQL                        : 
    select
        library0_.id as id1_3_,library0_.created_date as created_2_3_,library0_.last_modified_date as last_mod3_3_,library0_.name as name4_3_,library0_.root as root5_3_ 
    from
        library library0_ 
    order by
        library0_.name asc
2019-12-16 18:14:09.956 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteaccess  : Caching data from load [region=`cache.library` (accessType[read-write])] : key[org.gotson.komga.domain.model.library#73] -> value[CacheEntry(org.gotson.komga.domain.model.library)]
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteaccess  : Checking writeability of read-write cache item [timestamp=`6457308078116864`,version=`null`] : txTimestamp=`6457308159795200`,newVersion=`null`
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteaccess  : Cache put-from-load [region=`accessType[read-write]` (cache.library),key=`org.gotson.komga.domain.model.library#73`,value=`CacheEntry(org.gotson.komga.domain.model.library)`] failed due to being non-writable
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteaccess  : Caching data from load [region=`cache.library` (accessType[read-write])] : key[org.gotson.komga.domain.model.library#3] -> value[CacheEntry(org.gotson.komga.domain.model.library)]
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteaccess  : Checking writeability of read-write cache item [timestamp=`6457308078116864`,key=`org.gotson.komga.domain.model.library#3`,value=`CacheEntry(org.gotson.komga.domain.model.library)`] failed due to being non-writable
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteaccess  : Caching data from load [region=`cache.library` (accessType[read-write])] : key[org.gotson.komga.domain.model.library#33] -> value[CacheEntry(org.gotson.komga.domain.model.library)]
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteaccess  : Checking writeability of read-write cache item [timestamp=`6457308078116864`,newVersion=`null`
2019-12-16 18:14:09.958 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteaccess  : Cache put-from-load [region=`accessType[read-write]` (cache.library),key=`org.gotson.komga.domain.model.library#33`,value=`CacheEntry(org.gotson.komga.domain.model.library)`] failed due to being non-writable
2019-12-16 18:14:09.958 DEBUG 20400 --- [nio-8080-exec-1] o.h.stat.internal.StatisticsImpl         : HHH000117: HQL: select generatedAlias0 from library as generatedAlias0 order by generatedAlias0.name asc,time: 3ms,rows: 3
2019-12-16 18:14:09.989 DEBUG 20400 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json',given [*/*] and supported [application/json]
2019-12-16 18:14:10.025 DEBUG 20400 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing [[libraryDto(id=73,name=All,root=file:/C:/Users/groebroe101209/files/all/),libraryDto(id=3,name=C (truncated)...]
2019-12-16 18:14:10.123  INFO 20400 --- [nio-8080-exec-1] i.StatisticalLoggingSessionEventListener : Session Metrics {
    35800 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    279300 nanoseconds spent preparing 1 JDBC statements;
    154000 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    1256401 nanoseconds spent performing 3 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    3500 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}

第二个调用(应该调用缓存):

2019-12-16 18:14:35.180 DEBUG 20400 --- [nio-8080-exec-3] org.hibernate.SQL                        : 
    select
        library0_.id as id1_3_,library0_.root as root5_3_ 
    from
        library library0_ 
    order by
        library0_.name asc
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteaccess  : Caching data from load [region=`cache.library` (accessType[read-write])] : key[org.gotson.komga.domain.model.library#73] -> value[CacheEntry(org.gotson.komga.domain.model.library)]
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteaccess  : Checking writeability of read-write cache item [timestamp=`6457308078116864`,version=`null`] : txTimestamp=`6457308263133184`,newVersion=`null`
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteaccess  : Cache put-from-load [region=`accessType[read-write]` (cache.library),value=`CacheEntry(org.gotson.komga.domain.model.library)`] failed due to being non-writable
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteaccess  : Caching data from load [region=`cache.library` (accessType[read-write])] : key[org.gotson.komga.domain.model.library#3] -> value[CacheEntry(org.gotson.komga.domain.model.library)]
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteaccess  : Checking writeability of read-write cache item [timestamp=`6457308078116864`,value=`CacheEntry(org.gotson.komga.domain.model.library)`] failed due to being non-writable
2019-12-16 18:14:35.182 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteaccess  : Caching data from load [region=`cache.library` (accessType[read-write])] : key[org.gotson.komga.domain.model.library#33] -> value[CacheEntry(org.gotson.komga.domain.model.library)]
2019-12-16 18:14:35.182 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteaccess  : Checking writeability of read-write cache item [timestamp=`6457308078116864`,newVersion=`null`
2019-12-16 18:14:35.182 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteaccess  : Cache put-from-load [region=`accessType[read-write]` (cache.library),value=`CacheEntry(org.gotson.komga.domain.model.library)`] failed due to being non-writable
2019-12-16 18:14:35.182 DEBUG 20400 --- [nio-8080-exec-3] o.h.stat.internal.StatisticsImpl         : HHH000117: HQL: select generatedAlias0 from library as generatedAlias0 order by generatedAlias0.name asc,time: 2ms,rows: 3
2019-12-16 18:14:35.183 DEBUG 20400 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json',given [*/*] and supported [application/json]
2019-12-16 18:14:35.183 DEBUG 20400 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing [[libraryDto(id=73,name=C (truncated)...]
2019-12-16 18:14:35.186  INFO 20400 --- [nio-8080-exec-3] i.StatisticalLoggingSessionEventListener : Session Metrics {
    73400 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    41301 nanoseconds spent preparing 1 JDBC statements;
    180701 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    1124001 nanoseconds spent performing 3 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    4000 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
sunping19830220 回答:Spring数据:休眠查询缓存不适用于派生查询

我的yaml出现问题,没有正确缩进(!)

spring:
  jpa.properties:
    javax:
      persistence.sharedCache.mode: ENABLE_SELECTIVE
      cache.provider: com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider
    hibernate:
      generate_statistics: true
      session.events.log: false
      cache:
        use_second_level_cache: true
        use_query_cache: true
        region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
本文链接:https://www.f2er.com/2914348.html

大家都在问