Friday, September 16, 2011

A Grails GSP template caching system

Caching a whole rendered page is not always the best options, because some minor parts of the page can change from one user to another (e.g. login info).
In other situations, different perspectives can have common components and caching them can dramatically reduce page rendering time (typically a search results).
Inspired from the very good UiPerformance and springcache and based on ehCache, we propose an idea to go a bit further and cache either template rendering or a piece of gsp.
grails install-plugin template-cache
<cache:render ...>
Instead of having the classic gsp tag
  <g:render template="blocks/a-display" model="${[a:v] }" />
We just call
  <cache:render cachekey="${}" template="blocks/a-display" model="${[a:v] }" />
Where cachekey attribute will be the key in the cache system, typically the bean id or whatever construct.
<cache:cache ...>
If a whole piece of a gsp page is to be cached, we can embed it around:
  <cache:cache key="my-header-${username}">
    <h2>This header is super big and takes 2s to be computed</h2>
    <% Thread.sleep(2000) %>
Extra attributes
A couple of attributes can be added to the tag:
  • cachename="pirateCache" to set a different cache from the default one (see config)
  • cachedisabled="true" to disable caching of this tag
Well, that quite exactly the same as springcache config (thanks for all that!).

Disable the cache (for development env, it might be a good idea):

 You can add custom caches to your resources.groovy (or Config.groovy)
  pirateCache(EhCacheFactoryBean) { bean ->
    cacheManager = ref("templatecacheCacheManager")
    cacheName = "pirateCache"
    // these are just examples of properties you could set
    eternal = false
    diskPersistent = false
    memoryStoreEvictionPolicy = "LRU"
Cache report
You want to see cache statistics? Just head to http://localhost:8080/your-app/templatecacheReport
And that's all for today!

No comments:

Post a Comment