我正在尝试缓存从控制器调用的以下查询:
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)
查询语句,因此我认为这意味着结果没有从查询缓存中检索出来?我正在本地运行所有内容,因此不存在缓存被其他用户的操作使缓存的查询结果无效的可能性。