Spring Book – Chapter 10 – Caching

CompositeCacheManager

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

[xml]

[/xml]

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

[xml]

[/xml]

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: 11432

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

4 thoughts on “Spring Book – Chapter 10 – Caching

    1. I don’t know whether i should take your comment as positive or negative.. :). If it is positive, thank you. If it is negative, please let me know what i can improve on to make it positive.. 🙂

      Regards
      Tomcy John

  1. Hi,

    Is there a PDF version of this article, or at least a Web version somewhere else? This Website is really bad (full of ads) and the code samples are incomplete.

    Thanks,
    Lukasz

    1. Hi Lukasz,

      I plan to have a ad free version of the entire book. For that, though, i plan to distribute with a small charge. By end of October I plan to release using “Gumroad”. If you are still interested, with a small fee you can download and enjoy an ad free version of the book.

      Regards
      Tomcy John

Leave a Reply

Your email address will not be published. Required fields are marked *