Reputation: 2002
I've gone through javax.cache.Cache
to understand it's usage and behavior. It's stated that,
JCache is a Map-like data structure that provides temporary storage of application data.
JCache and HashMap stores the elements in the local Heap memory and don't have persistence behavior by default. By implementing custom CacheLoader
and CacheWriter
we can achieve persistence. Other than that, When to use it?
Upvotes: 14
Views: 10490
Reputation: 11
Mostly, caching implementations keep those cached objects off heap (outside the reach of GC). GC keeps track of each and every object allocated in java. Imagine you have millions of objects in memory. If those objects are not off heap, believe me, GC will make your application performance horrible.
Upvotes: 1
Reputation: 11132
Caches usually have more management logic than a map, which are nothing else but a more or less simple datastructure.
Some concepts, JCaches may implement
Some of these some are more general concepts of JCache, some are specific implementation details of cache providers
Upvotes: 14
Reputation: 21975
Here are the five main differences between both objects.
Unlike java.util.Map, Cache :
- do not allow null keys or values. Attempts to use null will result in a java.lang.NullPointerException
- provide the ability to read values from a javax.cache.integration.CacheLoader (read-through-caching) when a value being requested is not in a cache
- provide the ability to write values to a javax.cache.integration.CacheWriter (write-through-caching) when a value being created/updated/removed from a cache
- provide the ability to observe cache entry changes
- may capture and measure operational statistics
Upvotes: 1