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.
Installation
grails install-plugin template-cache
Usage
<cache:render ...>
Instead of having the classic gsp tag
<g:render template="blocks/a-display" model="${[a:v] }" />
We just call
<cache:render cachekey="${v.id}" 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) %>
</cache:cache>
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):
templatecache.disabled=true
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"
maxElementsInMemory=3
}
Cache report
You want to see cache statistics? Just head to http://localhost:8080/your-app/templatecacheReport
And that's all for today!
Thank you for sharing this information. I find this information is easy to understand and very useful. Thumbs up!
ReplyDeletekuşadası
ReplyDeletemilas
çeşme
bağcılar
siirt
NYKP