简体   繁体   English

Grails查询缓存被忽略

[英]Grails query cache is ignored

I have an application which is read-mostly, so most queries use the Hibernate 2nd-level query cache. 我有一个主要是读取的应用程序,因此大多数查询都使用Hibernate二级查询缓存。 I have one query that must be executed every time a page loads, so I invoke it from the layout GSP like this: 我有一个必须在每次加载页面时执行的查询,所以我从布局GSP中调用它,如下所示:

${Book.countByApproved(true, [cache: true])}

I have enabled the 2nd-level cache for the Book class, by adding 我通过添加为Book类启用了二级缓存

static mapping = {
    cache true
}

to Book.groovy . Book.groovy I also have the following configured in DataSource.groovy 我还在DataSource.groovy配置了以下内容

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

In this same file I've enabled query logging by adding logSql=true in the dataSource block. 在同一个文件中,我通过在dataSource块中添加logSql=true来启用查询日志记录。

Every time I load a page, the Book.countByApproved(true) query is logged, so I assume this means that the results are not being retrieved from the query cache? 每次加载页面时, Book.countByApproved(true)记录Book.countByApproved(true)查询,因此我认为这意味着未从查询缓存中检索结果? I'm running everything locally, so there's no possibility that the cache is being missed because the cached query results have expired. 我在本地运行所有内容,因此缓存查询结果已过期,因此不可能丢失缓存。

I don't know if this is relevant, but I'm using the Hibernate Filters plugin and have a default filter configured for the Book class. 我不知道这是否相关,但我正在使用Hibernate Filters插件并为Book类配置了默认过滤器。 Perhaps there's some issue with using hibernate filters and the query cache? 使用hibernate过滤器和查询缓存可能存在一些问题?

If I'm not mistaken. 如果我没错的话。 there's a question exactly equals to yours, and the advice was to not use querys in GSPs. 有一个问题与你的问题完全相同,建议不要在GSP中使用查询。 Try this in a filter and see if the cache works. 在过滤器中尝试此操作,查看缓存是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM