File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes how to implement a global cache in Tomcat? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to implement a global cache in Tomcat?" Watch "how to implement a global cache in Tomcat?" New topic

how to implement a global cache in Tomcat?

bujin wang
Ranch Hand

Joined: Jun 21, 2003
Posts: 48
My application provides some xml parsing functionalities. The parsed xml turns to a object. Most of the code is generic java code. The calling part is from java servlet(JSP).
The parsing process is time cusuming and I fount that the parsed object could be reused(read-only). So I try to implement a caching mechanism which will put the object into cache if we found out that the xml is the same. As we can see, the cache has to across requests and sessions. I can use a singleton which contains a Hashtable(static) to store the objects and use a timer to purge the objects if the objects are too old(e.g. 1 hour). As I was told that that could be a dangerous implementations in that we could easily got memory leak.
What should I be causious? Is there a problem using singleton with Tomcat?
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
In general, singletons are a poor choice for most applications (search for "singleton" in the "OO, Patterns and UML" forum to read lots of discussion).
This is especially true when the context of your code already provides a fine alternative. Every application running in a servlet container has available to it an "application context" (also known as "servlet context"), which can be used to store any objects that you wish - such objects are available to all servlets, JSPs and filters in the application.
I strongly suggest that you either put your parsed objects directly into the application context, or that you put a Map into the application context and store your parsed objects into that. Memory leaks are unlikely, as the container is responsible for emptying and destroying the application context if the application is unloaded or the container is shut down.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ken Robinson
Ranch Hand

Joined: Dec 23, 2003
Posts: 101
There are several ways to cache any object.
Using the ServletContext is valid as long as you are content managing the life cycle of objects yourself.
Check out for a widely used implementation of caching. You can configure things like objects lifespans (how long does it live) and even allow caching across a cluster.
There are several caching APIs out there and JSR-107 (JCache) is in process (forever now it seems) to standardize this.
Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
Caching objects with WeakHashMap:
Object caching article:
I agree. Here's the link:
subject: how to implement a global cache in Tomcat?
It's not a secret anymore!