不使用 Grails 查询缓存

Dón*_*nal 4 grails caching hibernate grails-orm

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

def approvedCount = Book.countByApproved(true, [cache: true])
Run Code Online (Sandbox Code Playgroud)

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

static mapping = {
    cache true
}
Run Code Online (Sandbox Code Playgroud)

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'
}
Run Code Online (Sandbox Code Playgroud)

在这同样的文件我已通过添加启用查询日志logSql=truedataSource块。

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

Bur*_*ith 6

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

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

和标准查询一样:

def count = Book.createCriteria().count {
   eq 'name', 'foo'
   cache true
}
Run Code Online (Sandbox Code Playgroud)