• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap - in-memory caching - multi-threaded environment ?

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am planning to use HashMap my in-memory caching in my application. Our application server is clustered. Whether this HashMap will work in multi-threaded environment ? If not, can i change my implemnetation to HashTable for multi-threading support. How is the performance using HashMap/HashTable in a clusteered environment ?
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are talking about 2 separate issues here. Being thread-safe is when you code needs to be changed by more than one process simultaneously in one JVM i.e. in a multi-threaded environment. Having your objects safe in a clustered environment is a different matter. I haven't looked at this in a while, so I don't know if built-in jvm support exists now or not. I would suggest not rolling your own and looking for some form of open source or commercial object cache that will do what you want. I googled 'distributed object cache'. This should get you started.

http://java-source.net/open-source/cache-solutions
http://jakarta.apache.org/jcs/
http://www.manageability.org/blog/stuff/distributed-cache-java
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kri Shan, By stating in-memory caching, do you mean having a static instance variable of HashMap? if so, you might need to synchronize the part accessing/updating this Map. Or you can try using http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Collections.html#synchronizedMap(java.util.Map)

HashTable is synchronized and can be used in multi-thread environment but it is kind of getting old.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could just use java.util.concurrent.ConcurrentHashMap and be done with it (for the concurrent part, not the clustered part). Any modern clustered Map will probably implement ConcurrentMap anyway, so you'll just have to change the implementation.
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can i use eCache or Oracle coherent caching for storing database values ?
 
Frank Pavageau
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ehcache is commonly used to cache the results of database queries: for example, it's frequently used as the second level cache for Hibernate. It's easily plugable, has lots of configuration options (region sizes, overflow to disk, time to live, time to idle, eviction strategy, etc.), can be extended (self-populating caches) and clustered with Terracotta.

As far as I know, Oracle Coherence can be used too, but in that case I hope you wouldn't use it only as a local cache, since Ehcache easily provides the same service for free.
reply
    Bookmark Topic Watch Topic
  • New Topic