preload
Tackling Bottlenecks
Mar 31

In the last post I wrote about ways to tackle bottlenecks and to increase the performance of an application. When dealing with a high traffic application servicing tons of concurrent users all requesting the same data, intelligent caching can boost the speed in which requests are processed. As a result, lots of load is taken away from the database.

Querying a database makes much sense when the data requested changes a lot. But imagine the database is backing a blog or news website where the data requested from the database is the same in hundreds or even thousands of cases. The most frequent query sent to the database in such an environment is probably for loading the article on page one, which - once published - probably won’t change a lot. This doesn’t hurt the Database if your private blog has 100 unique visitors per day, but if the frontpage of a popular news website is attacked by 100.000 unique visitors per hour then you are in trouble. You need to use caching in order to take away stress from your database server.

An excellent, widely used open source cache solution which is easy to use and yet powerful is OSCache (http://www.opensymphony.com/oscache/). Besides features like clustering, a servlet filter for caching entire JSP pages or persistent caching it comes with a JSP tag library that enables you to include caching into your jsp based web application so easily it hurts.

All you have to do is:
1)       Add the OSCache jar file to your classpath, put the tld file into the WEB-INF directory of the webapp and declare the OSCache taglib in your jsp page:

<jsp:root version="1.2" xmlns:jsp="http://java.sun.com/JSP/Page"
      xmlns:cms="cms-taglib" xmlns:cmsu="cms-util-taglib"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
    xmlns:cache="urn:jsptld:oscache.tld">

2)       mark portions of the jsp-page to be cached in your jsp page with cache tags:

<cache:cache key="${cacheKey}" time="0" language="${language}" time=”1800>
Content to be cached
</cache:cache>

In this example the cached portion of the page is stored under the key defined in the variable cacheKey, it supports multiple languages and expires after 1800 seconds. There are a few other attributes available for configuring the cache tag. More information can be found here http://www.opensymphony.com/oscache/wiki/JSP%20Tags.html

A cache with an invalidation timeout as seen in the above example is great for many situations, but sometimes you need full control on when a cache entry needs invalidating. OSCache offers an API to manage the cache in your Java code. This is an example of how to invalidate it in a Java web application:

ServletContext context = httpServletRequest.getSession().getServletContext();
ServletCacheAdministrator admin = ServletCacheAdministrator.getInstance(context);
Cache cache = admin.getAppScopeCache(context);
cache.flushEntry(key)

Piece of cake!

Take care,

Alex

PS: OSCache is under the OpenSymphony Software License, Version 1.1 which is derived and fully compatible with the Apache Software License.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • MisterWong

2 Responses to “Powerful yet easy caching with OSCache”

  1. Neal Brasil Says:

    java rocks!!

  2. Movies Forum %0B Says:

    :`~ I am really thankful to this topic because it really gives up to date information ,”`

Leave a Reply