Friday, December 3, 2010

InfoQ: Distributed Caching with JBoss Cache: Q&A with Manik Surtani

Why would I choose a local cache over, let's say, a HashMap?

Many would say that Maps are a starting point of a cache (an argument used by the JSR-107 JCACHE expert group to make javax.cache.Cache extend Map, in fact). But while maps are great for storing simple key/value pairs, they fall short on a lot of other features that may be necessary in a cache, such as memory management (eviction), passivation and persistence, and a finer grained locking model (a HashMap, for one thing, is not thread-safe at all. And ConcurrentHashMaps use a coarse level of locking that will not allow non-blocking readers or even multiple readers). And then there are the "enterprise" features of a proper cache, including JTA compatibility, and the ability to attach listeners.

So, while a Map is a good starting point, if anyone feels they need to implement or manage any of the above features themselves then they probably should be using a cache and not a Map.



InfoQ: Distributed Caching with JBoss Cache: Q&A with Manik Surtani