wood burning stoves 2.0*
The moose likes Java in General and the fly likes Caching objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Caching objects" Watch "Caching objects" New topic
Author

Caching objects

Dennis Kopylenko
Greenhorn

Joined: Apr 14, 2006
Posts: 2
I am designing a way to cache objects which may or may not get persisted. The objects are tied to a user(id) and one user can have multiple objects floating around needing to be cached.

Is the best way to cache in memory is to just put the objects in a hash map or a more elegant utility has already been created for this...which takes care of many to many mapping, flushing the cache, etc.?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

A HashMap is usually pretty good for this.



Synchronization is a concern with multi-threaded systems. I usually just synchronize the get & put methods on the cache, keep the map completely private, never let anybody else see an iterator or keyset or valueset.


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
Dennis Kopylenko
Greenhorn

Joined: Apr 14, 2006
Posts: 2
Thanks for replying, Stan.

I also happened to look at ehcache on sf and I was wondering what is the primary usecase for choosing ehcache over a custom solution?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
With just a quick look at the ehcache front page it seems to have many nifty features that I would not try to write myself. If you need the distributed or disk backed features, pick it for sure. I plan to read up on it further to see if it would replace a chunk of a vendor framework we use.

Under the Do The Simplest Thing That Can Possibly Work (for now) principle, you might choose a simple hashmap to start. Hide the implementation well behind your own interface and you can swap in ehcache or another distributed cache later if you find you need the features.
 
Consider Paul's rocket mass heater.
 
subject: Caching objects
 
Similar Threads
Data Caching Ideas
synchronization suggestion needed
Dependency Injection v Service Locator
Query on Cache for EJB3.0 entities on JBoss
do i have to use session here ?