I know this question is quite old, but besides the possibility of sharing the cache on several servers, there is another aspect that is not mentioned in other answers, and this is expiration.
If you store values ββin a HashMap and that the HashMap is tied to the application context, it will continue to grow in size if you do not expire the elements to some extent. Memcached expects an object to achieve maximum performance.
When an item is added to memcache, it can have an expiration time, such as 600 seconds. After the object has expired, it will remain there, but if another object requests it, it will clear it and return null.
Similarly, when memcached memory is full, it will look for the first expired item of sufficient size and expire to make room for the new item. Finally, it can also happen that the cache is full and does not expire, in which case it will replace the least used items.
stivlo
source share