Caching in Grails?

browser-cache, caching, grails, grails-2.0

Solution

Vast question depending on your needs.To cache domain objects you can use the Hibernate cache like this:

class Book {
    …
    static mapping = {
        cache true
    }
}

And configure Hibernate second level cache in `grails-app/conf/DataSource.groovy`:

hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true
    cache.provider_class='org.hibernate.cache.EhCacheProvider'
}

Grails Documentation and caching guide.

You can also cache your controllers and services using Grails cache plugin based on Spring cache:

@Cacheable('message')
   Message getMessage(String title) {
      println 'Fetching message'
      Message.findByTitle(title)
   }

You'll find the excellent documentation here.

If you want to cache rendered page you can also have a look at the gsp template rendering cache plugin.

Problem

Ist there something like a best practice for what is a good methodology to implement caching with grails? What plugins should be used and which parts of the page should be cached and how?

Original source