Skip to content

Caching the rending result

branaway edited this page Sep 14, 2010 · 1 revision

Theo outcome from the Japid template rendering can be cached to serve requests much faster.

Play 1.1 will introduce an annotation named CacheFor to be used with controller actions. Japid rendering works perfectly with the new annotation. In the past,, one has to use CacheableRenderer family in actions to get caching support, which is a lot more convoluted than the simple CacheFor annotation.

More importantly, the CacheFor annotation works perfectly with the #{invoke action..} tag in Japid templates.

For an exmple, the following are two actions in the controller Application, which are annotated with cache control


@CacheFor(“10s”)
public static void action1() {
renderJapid();
} @CacheFor(“5s”) public static void action2(String m) { renderText("Hello " + m); }

Here is the template for action1:


Welcome to action 1! Here is the output from action 2: #{invoke Application.action2(“action1”) /}

When one requests for http://localhost:9000/application/action1, the action1 is called and the static content in the template is cached for 10 seconds. However, the dynamic invoke part will be called every time a request is served, which carries a 5 seconds cache circle. What end users will see is most of the page will remains the same for 10 seconds while part of it will change for every 5 seconds.

Note: the parameter of the action2 (actually the result of toString() for each of the parameters) will be part of the key for the cache, therefore different parameters will trigger different cache, which is the expected behavior.