14

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?

Kamal Chandraprakash
  • 1,872
  • 18
  • 28
  • Thanks for the good question. That should be answered in the Cache class itself. Hopefully the next release will: https://github.com/jsr107/jsr107spec/issues/365 – cruftex Jun 08 '16 at 07:34
  • Does anyone know why default implementation (disk persistence) for `CacheLoader` and `CacheWriter` is not provided? The custom implementations using MapDB / RocksDB leads to third-party dependencies and it might be error-prone. – Kamal Chandraprakash Jun 09 '16 at 03:53

3 Answers3

14

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

  • Expiration: Entries may expire and get removed from the cache after a certain period of time or since last use
  • Eviction: elements get removed from the cache if space is limited. There can be different eviction strategies .e. LRU, FIFO, ...
  • Distribution: i.e. in a cluster, while Maps are local to a JVM
  • Persistence: Elements in the cache can be persistent and present after restart, contents of a Map are just lost
  • More Memory: Cache implementations may use more memory than the JVM Heap provides, using a technique called BigMemory where objects are serialized into a separately allocated bytebuffer. This JVM-external memory is managed by the OS (paging) and not the JVM
  • option to store keys and values either by value or by reference (in maps you to handle this yourself)
  • option to apply security

Some of these some are more general concepts of JCache, some are specific implementation details of cache providers

Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67
1

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

Source : GrepCode.com

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • 1
    Allow null keys or values on Maps depends on the implementation class. i.e. HashMap allow null keys and values and Hashtable doesn't allow null keys or values. – David SN Jun 08 '16 at 07:38
1

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.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42