Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Caching  RSS feed

 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a TOC object instance that is associated with a set of pages.
Actually, I have many TOC object instances, each associated with their own set of pages.
The TOC object has an ArrayList of WebPage instances. There are methods like next(), hasNext(), current(), et c.

How can I create the TOC instance just once? I can store it in application scope but then each time I add a page, I'll have to restart the container. I don't like this.
What should I do?
[ March 02, 2004: Message edited by: Garrett Smith ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you feel you need to retart the container?
How is the info persisted? DB? XML? Or do you have it hard-coded into the class?
If the latter, I'd recommend a DB or XML approach. That way, you could update the TOC dynamically without having to restart.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Garrett,
I also wonder why adding a new JSP page require to restart the container.
Basically, if the TOC is seldom changes, it should not affect the container, even you store it in the application scope.
Could you give us more information, so that we can figure out your problem better?
Nick.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Garrett,
In fact, even you have chances to update the TOC inside the application, I guess you can use application.setAttribute() to set it again into the application scope after each update.
Or as what Bear said, using DB or XML so that the TOC can be updated and retrieved via DB or XML, so that any changes can take effect immediately.
Nick.
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to use a REAL example here...
Let's say you go to a page: within the TOC.
http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp

Here's my source code for that page:


Consider this snippet from toc.jsp:


Now I'm thinking: "Why recreate this TOC instance and concatenate the output strings?" and "Why not store the value in a property with an accessor?" "Why not use lazy initialization for the TOC instance and store that in application scope?"
I think it would be easier on the server to cache the TOC. I'll need an unique ID for the TOC, perhaps a constructor for this:

and a getID Method:

and privatize the constructor:


Or maybe:


toc.setWebpages already checks to see if toc.webpages != null and if toc.webpages != null, it returns void (it does nothing).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't change my answer if you are looking for a robust implementation. Hard-coding the TOC in the JSP is clearly not the best architecture, even though it allows to update the TOC without a server bounce because JSP pages are reloaded automatically.
This sort of thing should be abstracted, and if it's an application-wide datum, a bean cached in the application context is suitable. However, as you noted, hard-coding the values in the bean means a server restart to reload any updated bean.
Hence, the data should be persisted to the filesystem (DB, XML, flat file, or anything else) and the "TOC cache" bean should get its information from there, and with the ability to reload when the info is updated on disk.
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so I make a bean for toc.
Should I use setProperty for the pages? This would require calling setProperty for each page in the toc.
Then there's the matter of setting the HTML which will be output to the client.

I'm still going to need a scriptlet for this. Unless you have a better idea.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the bean need to know anything about HTML? It should simply provide the TOC list in a UI-agnostic manner.
I'm still going to need a scriptlet for this. Unless you have a better idea.

JSTL
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't have to have an html property. But it could make for a lighter server load.
If I don't have an html property, I'll need iterate through the toc bean's webpages and output a link for each one. That's basically what I'm doing now.
If I do have an HTML property, then I'll check to see if the bean's html property has been set. If it hasn't, I can iterate through the toc bean's webpages and then set the html property to the string I concatenated during the iteration.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!