File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Caching an Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Caching an Object" Watch "Caching an Object" New topic
Author

Caching an Object

rastas biggs
Ranch Hand

Joined: Sep 24, 2004
Posts: 41
Is this possible and if so is it resource expensive?

I want to cahe an object in memory, if a client requests the object if it is on the heap the reference value for that object is passed to the client.

This sort of an object with constant values. rather than creating an new object the existing object and values are passed to the client ( in this case a jsp).

Can any one help and give me some example as a starting point?

Matt
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Caching is an important strategy in clients and servers alike. You could probably come up with a decision tree to help you decide what to cache. Is the data read-only? Updated infrequently? Shared by many users or processes? Not too huge?

You can use static variables to hold caches. You might have something like:

That's a lazy cache - there's nothing in it until the first client asks for one, then it stays there forever. You could also load a cache at startup. My current project has both kinds. We have mechanisms to clear the lazy loads and reload the initial loads without restarting the system, just in case something change during the day.

Other cool cache options include purging "least recently used" or oldest items to save memory, "write through" on put that lets clients put new items in cache and update the database at the same time. If you have multiple load-balanced servers you may or may not have to worry about keeping caches in synch.

Google for Java Cache. There are some commercial products and open source goodies to study. Hope that helps!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
If your cache may fill memory, look into the java.lang.ref package for ways to save references to your objects while not preventing garbage collection.


Mike Gershman
SCJP 1.4, SCWCD in process
rastas biggs
Ranch Hand

Joined: Sep 24, 2004
Posts: 41
Cool that is a big help thanks guys.

RB
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Caching an Object
 
Similar Threads
Passed ... :-) ....
Passed
Passed SCJD.
Passed!
Passed!