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 cache a collection of values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to cache a collection of values" Watch "how to cache a collection of values" New topic

how to cache a collection of values

Dave Bosky
Ranch Hand

Joined: Dec 16, 2003
Posts: 72
I call a stored procedure that returns about 9000 rows of information which consist of a part number and part description that I use to populate a dropdown select box in a form.

Instead of calling the procedure and returning 9000 rows each time the page is displayed, how can I cache this collection of values that gets returned to my jsp page?

Someone mentioned that I use a servicelocator and singleton pattern to solve the problem. Is this the proper way to accomplish my goal of caching the returned collection? Are there better ways to do this?

Any thoughts would be great!

Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102
Yes, we used ServiceLocator pattern with Singleton collection object to achieve the same.

But when you do this you have to be aware of two things.

First, when you return the collection through the exposed API, you return a cached unmodifiable collection, so that if nobody can change the content of the collection. Used the unmodifiableXXX method of java.util.Collections class to make the collection unmodifiable.

Second, if there is scope for change in underlying database, you have to provide a means to update your cached data.

James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
You may want to check out my TtlReference class from this topic. It will allow you to cache the collection, but the reference will "time out" after a set period of time.

James Carman, President<br />Carman Consulting, Inc.
I agree. Here's the link:
subject: how to cache a collection of values
It's not a secret anymore!