Dónal
Dónal

Reputation: 187529

Grails query cache is not used

I'm trying to cache the following query that is called from a controller:

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

I have enabled the 2nd-level cache for the Book class, by adding

static mapping = {
    cache true
}

to Book.groovy. I also have the following configured in 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.

Every time I load the page, the Book.countByApproved(true) query is logged, so I assume this means that the results are not being retrieved from the query cache? I'm running everything locally, so there's no possibility that the cache is being missed because the cached query result has been invalidated (by the actions of another user).

Upvotes: 4

Views: 2643

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

I'll look at the JIRA issue you filed, but it looks like a problem with dynamic finders, since HQL works:

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

as does a criteria query:

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

Upvotes: 7

Related Questions