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

Spring Data: Hibernate query cache not working for derived queries

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

我正在使用 Spring Boot 2.2.1、Kotlin 1.3.50 和带有 JCache 扩展的 Caffeine 作为我的底层 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`, 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#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`, version=`null`] : txTimestamp=`6457308159795200`, 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_.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: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), 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: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`, 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), 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: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`, version=`null`] : txTimestamp=`6457308263133184`, 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), 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: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=All, root=file:/C:/Users/groebroe101209/files/all/), LibraryDto(id=3, 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)
}

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