Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Caching Java Objects

 
Manji Mango
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manji Mango
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There has been a LOT of open-source activity in this area.
I suggest you look at WhirlyCache.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic