aspose file tools*
The moose likes Java in General and the fly likes Caching Java Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Caching Java Objects" Watch "Caching Java Objects" New topic
Author

Caching Java Objects

Manji Mango
Greenhorn

Joined: Mar 14, 2007
Posts: 3
I have a fair idea about caching and the only possible way i can think of Caching java objects is to put them in a Hashmap. Are there other any means of doing the same. Can nebody help me with some URL's.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on servlets.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Now that you're here in the intermediate forum ... Yes, a HashMap is often at the core of caches. Figure out some kind of naming scheme so you can find things later. Make it a static variable in some cache class so all instances share the same map. Have a strategy for removing objects or preventing unbridled growth.

Be aware that you get one copy of classes and static variables per classloader that loads the class, so it is possible to have more than one in memory. If two caches out of sync with the latest change would be fatal, you'll have to be extra careful in frameworks or apps that use custom loaders.


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
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Maps are good for many types of cache, but other data structures may suit some applications.

It's my view that "cache" refers to temporary storage in a fast store of recently-accessed information, available from a slow source. But I see the word used often just for a data store, where there isn't actually any slower alternative store; in my view, that's not a "cache".

Proper caches can often be helped by the java.lang.ref package. In particular, SoftReference is good for caches, because you can use it to store information on the heap, letting Java itself decide whether there's enough heap available to do that. When it gets low on heap, Java automatically disposes the cached data.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Manji Mango
Greenhorn

Joined: Mar 14, 2007
Posts: 3
Originally posted by Stan James:
Now that you're here in the intermediate forum ... Yes, a HashMap is often at the core of caches. Figure out some kind of naming scheme so you can find things later. Make it a static variable in some cache class so all instances share the same map. Have a strategy for removing objects or preventing unbridled growth.

Be aware that you get one copy of classes and static variables per classloader that loads the class, so it is possible to have more than one in memory. If two caches out of sync with the latest change would be fatal, you'll have to be extra careful in frameworks or apps that use custom loaders.


Ok, but then i am curious to know what is the need for JBoss Cache or EH Cache in Hibernate. Every application can use the same above technique.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't know those well enough to know what features they add. Do they have policies for aging and purging, synchronization between cache and persistant store, locking, transactions and rollbacks, synchronization between JVMs, failover, anything cool? I'd much sooner "buy" those features than write them myself.
Manji Mango
Greenhorn

Joined: Mar 14, 2007
Posts: 3
Originally posted by Stan James:
I don't know those well enough to know what features they add. Do they have policies for aging and purging, synchronization between cache and persistant store, locking, transactions and rollbacks, synchronization between JVMs, failover, anything cool? I'd much sooner "buy" those features than write them myself.


Thanks Stan
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
There has been a LOT of open-source activity in this area.
I suggest you look at WhirlyCache.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Caching Java Objects