This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Caching objects in a session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Caching objects in a session" Watch "Caching objects in a session" New topic
Author

Caching objects in a session

Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi,
I've been doing a bit of reading about JSP and have a question about keeping objects in memory. Here goes...
I have an object which is created from a whole load of data in a database. The object is essentially content for the website, but because of the nature of the content, I need to load all of the content in order to get the necessary bits out that the user has specifically requested. There is a reason for this, so bear with me.
Anyway, the content does not change very often, but the requests for the object are obviously numerous (every page request in fact).
So here's my question: is there any way to cache the object so that it only goes back to the database when the content has been updated? Basically, I want to hold the object in memory so that it doesn't need to be re-created between page requests - only if the content has been changed. Ideally, also, I don't want the object to be related to a user session, but global read-only (if that makes sense).
The software is not built in J2EE and I can't move to J2EE to solve this problem.
Any help is much appreciated. I realise it's not a very specific question.
Thanks in advance,
Steve
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Servlet containers provide an "application" context for just this kind of purpose.
in your servlet you can use
getServletContext().setAttribute("name", theCachedObject);
to put the cached data into the "application context", and
getServletContext().getAttribute("name");
to fetch it back whenyou need it later.
There is one instance of this application context for each application loaded in the container.
If you are using JSP, you have an "application" variable already available to provide access to this context:
<%= application.getAttribute("name") %>
Is this the kind of thing you were looking for?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
You may have some wrapper facade object on top of data access object(DAO, the object that holds the DB connection and performs the DB SQL updates). The wrapper object may contain the "bits and pieces, which may be looked upon as facets of the DB info" as value objects (singletons, which have get/set methods for the info they hold). You can commit the updated value objects via the DAO to the database for every update, or as a batch, or at the time the application is being brought down, as per your requirement.
a little addition on similar lines to Frank. In J2EE, you may need to implement contextListener objects that listen to the context events like context being initialized and destroyed. That is where you load the DB connections and all that stuff. But i'm pretty certain that this doesn't address your concern as you will not be able to J2EE
Hope that helps.
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
In any case that the database is updated, you must re-cache that data object.
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
It depends on who all is updating the database. If it is only you, you can just keep the cache updated and commit at the end of it all. If the DB is distributed, then you need to re-cache as somebody else might be updating it, as you were.
Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi guys,
Thanks a lot for the information - it's very helpful.
Basically, the database is updated by the users and can be done relatively frequently (ie. some users won't updated the material for months, though some may update within minutes - admittedly only in short spurts followed by long periods with no change). If it is updated, the whole dataset for the object is replaced with the new information.
To give a bit of background, the content comes through a web service and is published from a desktop application. The user will likely publish their content, check it on the site, correct any last changes, publish again, check and then leave it for some time (likely weeks or months).
I've not really played with the application scope and I'm not sure how you first instantiate the object. Also, I'm not sure how you'd notify the object that the dataset has changed.
Thanks again for your help. You've given me a lot to think about.
Cheers,
Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Caching objects in a session
 
Similar Threads
Tag Library and Struts Framework
Request ActionForm validate
Sync block and variable references
session.setAttribute for session persistence
js references/garbage collection/other questions