Why to use caching solution like ehcache when we have Hashmap
Caching has become a mandatory layer in modern day applications. It helps in boosting the performance of applications as there would be fewer database/network calls which are time consuming. There are many caching solutions available in the market like EHCache, JBoss cache, OSCache, Redis, JCS etc. Most of these caching solutions store data in key value format. But sometimes we do wonder why do we need such solutions when we can do the same using various data structures like Hashmap, ConcurrentHashmap, LinkedHashmap etc. In simple words what is EHCache doing which we can’t do by using plain simple Hashmap.
get link 1. Max size cap
You can’t have your cache keep on growing otherwise it might consume all your heap memory. So you would like to have a cap of maximum elements the cache can have as well as maximum memory the whole cache can consume. Once the limit is reached cache will start cleaning itself based on the algorithm specified which can be LRU (least recently used), LFU(least frequently used),FIFO and many more.
buy priligy canada 2. Max age and Idle time
These custom solutions let you specify what is the maximum time a given element should stay in the cache as well as maximum idle time before the given element should be removed.
Sometimes you would like to persist the cache either to disk or some database. Many caching providers provide such solutions. You can also specify if memory cache exceeds a certain number, start overflowing to the disk
4. Distributed cache
Imagine you have a cluster of 2 nodes. If you cache some element on node 1 and next request hits node 2 requesting the same element and it’s local cache won’t be having the value. So will you write the logic of replicating the cache across all the nodes?
5. Faster performance
Though these solutions mostly store key-value pairs but they claim to be having much faster implementation as compared to hashmaps
6. Dynamic configuration
You can change the various config values at the runtime. For example, initially you can keep max-age at higher value but as the load on your server increases, you can decrease this max-age cap to accommodate more objects into your heap memory.
So if you start implementing all these points by yourself, you would definitely end up writing few thousand lines of code and few 10k lines of test code.
Mar Java Mit Java