This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Caching an Object

 
rastas biggs
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rastas biggs
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool that is a big help thanks guys.

RB
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic