Java

JSP Page/Fragment Caching for Dummies

A simple way to speed up performance in high-volume web applications is to load more static pages. (Enough with the technology rants, it’s time to write some more technical content.) In the average application, however, this proves difficult. Most pages that are served to users contain regions of both static and dynamic content. While it might make sense to cache the header and footer, the menu component may be different for each user, and the content area changes with every click. OpenSymphony has created OSCache, a caching component for J2EE web applications. OSCache is open source, and doesn’t carry a licensing fee.

Incorporating OSCache into your application is really quite trivial. At the simplest level, it involves adding a tag library to your application and adding a simple tag to the JSP in question. So once you have the .tld and dependent .jar files in your web app, it’s a simple matter of changing your .jsp as follows:

<cache:cache>
 <JSP content to be cached>
</cache:cache>

Instantly, you’ve made this page indefinitely cached for all users. Only the content in the body of the tag is cached, so you can have incredible granularity in determining what is andis not cached. In the case of a page that contains some user customizations, you can restrict the caching to be specific to each user:

<cache:cache scope=”session”>
 <JSP content to be cached>
</cache:cache>

Further options are available to customize the duration and refresh behavior of the cached content, as well as a tag for conditional use of the cache. Cache size, location, and algorithm can also be modified. While options are nice, the nicest thing about OSCache is the simplicity of getting it set up and working. Significant performance gains can be quickly realized by using a single tag to prevent your app server from tediously generating the same content again and again.

Advertisements