simple caching

I have an application which recieves request from a client to send back an xml via a servlet. The information is retrieved from the database and then XMLBeans are used to marshall this info into an xml file.

Currently there are about 15 different xmls which are formed as a result of this process. Now as database connection is an expensive operation and one would not like to go through to the database each time to grab the information, the plan is to somehow cache these xml and setup a time to live property for this cache after which the cache is going to refresh each time.

I am thinking of creating a HashMap of these xml within the init method of the servlet. Where there would be a timer thread setup to refresh the data every x amount of time. Once this HashMap is populated over there I don't have to grab info from the DB each time and since it is readily available I can just get it from the cache itself.

What do you think about this and do you feel there could be a better way of doing this.

[1032 byte] By [LisaMa] at [2007-11-27 6:50:36]
# 1
Are the XML files likely to change during the lifetime of the application?
filestreama at 2007-7-12 18:24:41 > top of Java-index,Java Essentials,Java Programming...
# 2
the schema doesnot but the xml data does on a regular basis, may be every few hours
LisaMa at 2007-7-12 18:24:41 > top of Java-index,Java Essentials,Java Programming...
# 3
Why not use some sort of observer pattern as well, as it could improve even more connections to the database.MeTitus
Me_Titusa at 2007-7-12 18:24:41 > top of Java-index,Java Essentials,Java Programming...
# 4

> Why not use some sort of observer pattern as well,

> as it could improve even more connections to the

> database.

>

> MeTitus

for that would there have to be some triggers on the database side and if yes do you have any examples for that, since my DB skills are pretty crippled

LisaMa at 2007-7-12 18:24:41 > top of Java-index,Java Essentials,Java Programming...
# 5

>>for that would there have to be some triggers on the database side and if yes do you have any examples for that, since my DB skills are pretty crippled

You are right about it...

Search for "observer pattern java" google, if I had any piece of code which I dont I would show it to you ;)

http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/ObserverObservable.htm

But basically the obverver/observable works like triggers in a database with listeners but in the java world, so instead of having to connect to the Database to check whether the data was changed, but using this pattern your could co be notified.

MeTitus

Message was edited by:

Me_Titus

Me_Titusa at 2007-7-12 18:24:41 > top of Java-index,Java Essentials,Java Programming...
# 6
Check this one http://technology.amis.nl/blog/?p=1123MeTitus
Me_Titusa at 2007-7-12 18:24:41 > top of Java-index,Java Essentials,Java Programming...