Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to use cache in my servlets application.

 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any thoughts about how to implement caching technique in Servlets?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic