A composite cache manager is used to manage multiple cache managers containing multiple caches. Composite cache manager implementation iterates over a given collection of CacheManager instances. Allows NoOpCacheManager to be automatically added to the list for handling the cache declarations without a backing store. To mix local and distributed caches, use the CompositeCacheManager, demonstrated in Listing 10-23.
Listing 10-23. Spring configuration of CompositeCacheManager
Spring’s Support – Cache Storage Libraries
Spring Framework supports two cache storage libraries namely EhCache and ConcurrentMap. Spring has given appropriate cache managers and required factory implementations for these. In this section we will discuss in detail the integration between your application and these cache storage libraries in detail along with its features as well as pros and cons of using these cache libraries with your Spring application.
JDK ConcurrentMap based Cache
The JDK-based Cache implementation resides under org.springframework.cache.concurrent package in the Spring Framework. It allows to use ConcurrentHashMap as a backing Cache store. In Listing 10-24 below, SimpleCacheManager class is used to create the CacheManager. Two caches are created, one being default and other one named “customers”.
Listing 10-24. Spring configuration of CompositeCacheManager
When to choose ConcurrentMap based cache in your Spring application can be influenced by looking at the various advantages and disadvantages of this as summarized below.
Advantages of Using ConcurrentMap
Choosing ConcurrentMap offers the following advantages:
- Simpler and easy to implement
- Works well for small data which are more or less static and or which rarely change
Disadvantages of Using ConcurrentMap
Choosing ConcurrentMap offers the following disadvantages:
- No built in way to share data across multiple JVM’s. Lacks distributable capability
- Difficult to figure out the usage of heap memory being used up by the cache
- No capability of storing data outside Java heap memory
- No built in eviction algorithms present
- No built in statistics gathering capabilities
Page Visitors: 12454