This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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?
Bujin
Frank Carver
Sheriff

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 frankcarver.me ~ 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 http://www.opensymphony.com/oscache/ 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:
http://www.onjava.com/pub/a/onjava/2001/07/09/optimization.html
Object caching article:
http://www.onjava.com/pub/a/onjava/2003/12/23/caching.html
 
 
subject: how to implement a global cache in Tomcat?
 
Similar Threads
connection pool container scope
connection pool container scope
Safely reloading object cache while possible multiple clients using "old" objects.
Should I use a SL SessionBean or JavaBean
JAXP with DOM parser or JDOM - Which to use?