gpt4 book ai didi

hibernate - Hibernate 2级缓存不适用于GORM 6.1.11

转载 作者:行者123 更新时间:2023-12-02 14:48:19 25 4
gpt4 key购买 nike

问题:

我有一个条件查询,其中在使用cache(true)时使用了Hibernate的二级缓存:

User user = User.createCriteria().get {
eq("id", 1l)
cache(true)
}

当此代码两次被命中时,第一次它会进行数据库查询,但是当下一次执行相同的代码时,它不应为查询而访问数据库,而应从缓存中获取它。但这没有发生。

日志:
2019-08-29 23:25:06.023 DEBUG --- [nio-8080-exec-1] o.h.cache.internal.StandardQueryCache    : Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache
2019-08-29 23:25:06.023 DEBUG --- [nio-8080-exec-1] o.h.c.e.i.r.EhcacheGeneralDataRegion : key: sql: select this_.id as id1_0_0_, this_.version as version2_0_0_, this_.first_name as first_na3_0_0_, this_.last_name as last_nam4_0_0_, this_.email as email5_0_0_ from user this_ where this_.id=?; parameters: 1, ; transformer: org.hibernate.transform.CacheableResultTransformer@110f2
2019-08-29 23:25:06.027 DEBUG --- [nio-8080-exec-1] o.h.c.e.i.r.EhcacheGeneralDataRegion : Element for key sql: select this_.id as id1_0_0_, this_.version as version2_0_0_, this_.first_name as first_na3_0_0_, this_.last_name as last_nam4_0_0_, this_.email as email5_0_0_ from user this_ where this_.id=?; parameters: 1, ; transformer: org.hibernate.transform.CacheableResultTransformer@110f2 is null
2019-08-29 23:25:06.028 DEBUG --- [nio-8080-exec-1] o.h.cache.internal.StandardQueryCache : Query results were not found in cache
2019-08-29 23:25:06.030 DEBUG --- [nio-8080-exec-1] org.hibernate.SQL : select this_.id as id1_0_0_, this_.version as version2_0_0_, this_.first_name as first_na3_0_0_, this_.last_name as last_nam4_0_0_, this_.email as email5_0_0_ from user this_ where this_.id=?
2019-08-29 23:25:06.046 DEBUG --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl : HHH000117: HQL: [CRITERIA] select this_.id as id1_0_0_, this_.version as version2_0_0_, this_.first_name as first_na3_0_0_, this_.last_name as last_nam4_0_0_, this_.email as email5_0_0_ from user this_ where this_.id=?, time: 17ms, rows: 1
2019-08-29 23:25:06.047 DEBUG --- [nio-8080-exec-1] o.h.cache.internal.StandardQueryCache : Caching query results in region: org.hibernate.cache.internal.StandardQueryCache; timestamp=6418846948913152
2019-08-29 23:25:06.049 DEBUG --- [nio-8080-exec-1] o.h.c.e.i.r.EhcacheGeneralDataRegion : key: sql: select this_.id as id1_0_0_, this_.version as version2_0_0_, this_.first_name as first_na3_0_0_, this_.last_name as last_nam4_0_0_, this_.email as email5_0_0_ from user this_ where this_.id=?; parameters: 1, ; transformer: org.hibernate.transform.CacheableResultTransformer@110f2 value: [6418846948913152, 1]
Found com.wizpanda.test.User : 1
2019-08-29 23:25:06.099 INFO --- [nio-8080-exec-1] i.StatisticalLoggingSessionEventListener : Session Metrics {
45071 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
3767346 nanoseconds spent preparing 1 JDBC statements;
4620558 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
7925151 nanoseconds spent performing 1 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
4562453 nanoseconds spent performing 1 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}


环境:
  • 操作系统:MacOS,CentOS7
  • GORM版本: 6.1.11
  • Grails版本(如果使用Grails): 3.3.9
  • JDK版本: 1.8.0_91

  • 重现步骤:
  • 克隆此repo git clone git@github.com:wizpanda/test-l2-cache.git
  • 登录到MySQL数据库并创建一个新数据库create database test_l2_cache
  • 更改grails-app/conf/application.yml中的MySQL用户名和密码
  • 运行应用程序grails run-app./gradlew bootRun
  • 浏览到http://localhost:8080/并检查日志

  • 提出的问题: https://github.com/grails/grails-data-mapping/issues/1266

    最佳答案

    第一步,您需要启用Hibernate的二级缓存,如下所示:

    hibernate.cache.use_second_level_cache=true

    这样,您只为实体启用了二级缓存,而没有查询。

    如果您需要在查询/条件级别缓存:

    在Hibernate API中: query.setCacheable(true); //query is instance of org.hibernate.Query
    在JPA中: query.setHint("org.hibernate.cacheable", true) //query is instance of javax.persistence.Query

    关于hibernate - Hibernate 2级缓存不适用于GORM 6.1.11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57765320/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com