【问题标题】:Grails query cache is not used不使用 Grails 查询缓存
【发布时间】:2013-05-13 16:34:18
【问题描述】:

我正在尝试缓存从控制器调用的以下查询:

def approvedCount = Book.countByApproved(true, [cache: true])

我已通过添加为Book 类启用了二级缓存

static mapping = {
    cache true
}

Book.groovy。我还在DataSource.groovy中配置了以下内容

hibernate {
  cache.use_second_level_cache = true
  cache.use_query_cache = true
  cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}

在同一个文件中,我通过在 dataSource 块中添加 logSql=true 来启用查询日志记录。

每次我加载页面时,都会记录Book.countByApproved(true) 查询,所以我认为这意味着没有从查询缓存中检索结果?我在本地运行所有内容,因此不可能因为缓存的查询结果已失效(由另一个用户的操作)而丢失缓存。

【问题讨论】:

    标签: hibernate caching grails grails-orm


    【解决方案1】:

    我会查看您提交的 JIRA issue,但它看起来像是动态查找器的问题,因为 HQL 有效:

    Book.executeQuery(
       'select count(*) from Book where name=:name',
       [name: 'foo'], [cache: true])
    

    与标准查询一样:

    def count = Book.createCriteria().count {
       eq 'name', 'foo'
       cache true
    }
    

    【讨论】:

    • 谢谢,我已链接到 JIRA,以便其他人可以关注它的进展
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2014-03-29
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多