John Little
John Little

Reputation: 12423

How to timeout a domain class cache in Grails (i.e. specify max age)

Mostly static, frequently accessed domain classes such as countries and currencies can benefit greatly from caching.

class country {
    :
    static mapping = {
        cache  usage: 'read-only', include: 'non-lazy'
    }
}

However, once in a while, a new country is added or an existing one modified in a multi-server environment. The solution we are looking for is a cache timeout, so that after a configurable time per domain class, e.g. 10 minutes, it re-reads the data when a get is requested.

Currently, the only option is if you cache something, you have to restart all the servers. We don't want the complexity of a distributed cache, which broadcasts changes, just a cache aging option on a per domain basis.

Is this possible with grails 2.4.4, or do we need to implement our own caching layer? With MyBatis specifying the max age (aka cache timeout) is easy - hope grails has an undocumented feature for this.

Upvotes: 0

Views: 324

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75681

Restarting the server is very far from being the only option.

The configuration options for caching in the mapping block are quite limited, but that's at least in part because there's no standard configuration API, so you need to do it differently depending on the provider.

The default implementation is Ehcache, and it's pretty simple to configure. If Ehcache finds an ehcache.xml file in the root of the classpath, it will use that instead of its defaults. Non-Java files in src/java and non-Java/non-Groovy files in grails-app/conf are copied to a directory that's in the classpath during compilation, so putting your file there is the best bet for making it accessible.

Use this heavily commented example ehcache.xml file to get started.

If you want to remove one or more cached instances, Hibernate has an API for that. There are several "evict" methods that you can call on the sessionFactory bean, e.g. sessionFactory.evict(Book) removes all cached Book instances, sessionFactory.evict(Book, 5) removes the cached Book with id 5, etc.

There's a lot of relevant info in the slides from this SpringOne/2GX talk including configuring Ehcache and working with the 2nd-level cache API.

Upvotes: 1

Related Questions