aspose file tools*
The moose likes Servlets and the fly likes Trying to use cache in my servlets application. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Trying to use cache in my servlets application." Watch "Trying to use cache in my servlets application." New topic
Author

Trying to use cache in my servlets application.

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
Sometime there are some data saved in database that I really want to put in some cache/ memory because every HTTP request uses this data and I think it will save some memory if I can get the data from database once and save it somewhere so each HTTP request doesn't need to query again and again from database.

I'm trying to use Apache JCS: http://jakarta.apache.org/jcs/index.html
But What I'm confused about is that (based on my understanding), Servlet
serve request-per-request. I'm confused as Who should maintain this cache.

Any thoughts?

One other thing that I'm wondering is: why should I use caching open-source solution rather than a simple hashtable in the memory.

Thanks in advance for the advices!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61191
    
  66

Originally posted by Susan Smith:
... and I think it will save some memory if I can get the data from database once and save it somewhere so each HTTP request doesn't need to query again and again from database.
Caching will not save memory -- it will, in fact, use more memory since the cached data needs to be held in memory. If minimizing memory usage is your goal, caching would be like throwing gasoline on a fire to put it out.

If you want to minimize DB access, then caching is a viable means.

One other thing that I'm wondering is: why should I use caching open-source solution rather than a simple hashtable in the memory.
Well, the JCS front page lists its advantages. If you don't need any of that. maybe a simple hash table is all you need.
[ July 29, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
Caching will not save memory, it will in fact use more memory since the cached data needs to be held in memory. If minimizing memory usage is your goal, caching would be like throwing gasoline on a fire to put it out.


You're right ... I was meaning to day save resources/ database access.
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
Any thoughts about how to implement caching technique in Servlets?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61191
    
  66

I wouldn't implement such caching anywhere near the UI. Such caching would occur at the lowest level possible.

That way most of the application simply interacts with an interface that allows it to obtain the data. Whether the data comes form the cache or comes from the DB shouldn't be a concern of anything except the lowest possible layer that fetches data.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Susan Smith:
Any thoughts about how to implement caching technique in Servlets?


There is no one answer for this.
How to cache data depends entirely on the nature of the data and your application.

If it's something simple like user information (name, login, favorite links within your app etc) then storing that information in session when the user logs in can save you from having to perform database transactions with every hit that that user makes.

Other things like real time reports that involve a lot of computation can get more complicated.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
One example:
I have a table called Timezone (two columns: City name and each timezone).
This depends on user request (i.e.: user will supply city name). So rather than having the application query Timezone database every time (even though small table), I'm thinking caching it somewhere probably more efficient.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61191
    
  66

Ah, if it needs to be per-user, caching it in the session might be appropriate. Again, I would limit the exposure of this cache behind an interface. Have one method that the rest of the app can call to get the data. The details of whether it is taken from the cache, or if the cache needs to be updated from the DB are hidden behind this interface.
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
But let say I have 1000 user entering in at the same time.
500 of them could actually requesting the same data (supplying the same city name).
Caching it in the session will be available only per user, right?

I was thinking probably the 500 users supplying the same city name should get data from the same cache?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I agree. putting it in a hashMap in the session is not a bad idea. it's not information that is changing, so once retrieved it can be cached in the session.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Susan Smith:
But let say I have 1000 user entering in at the same time.
500 of them could actually requesting the same data (supplying the same city name).
Caching it in the session will be available only per user, right?

I was thinking probably the 500 users supplying the same city name should get data from the same cache?


This is why it's hard to give you one, simple, answer for this.
If you know this is likely to happen a lot, then it might make more sense to keep a cache in context scope, or, as Bear said, separate it from the web tiers altogether.

When and how to cache is very application specific.
The goal is to get the 'cache hit' to 'database hit' ratio as high as possible without your cache growing out of control. If your app is already up and running, you might want to start profiling it and logging all of your database transactions. Over time, you should be able to compile some trends and patterns that would help you to determine what and how much to cache.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to use cache in my servlets application.