Spring Book – Chapter 10 – Caching

Ehcache based Cache

Ehcache is an excellent library which provides the community with high quality, good documentation, simple to use and wide adopted cache framework. It is one of the most popular open-source and most widely used Java-based cache frameworks available today. It scales from in-process cache to various distributed cache. It is actively developed and maintained by Terracotta.

Spring uses a proxy which intercepts call to the method of bean; consults the cache to check if method was called with same parameters before, if so will return cached result. EhCache is the actual provider of caching solution and Spring Framework handles method interception and stores the results in the cache.

Ehcache Features

There are various features in EhCache which makes it an ideal cache framework choice in your application. Some of the well known features are listed below:

  • Well known in the Java community
  • Simpler and easy to implement
  • Extensibiity
  • Can be offloaded to memory
  • Can be scaled to any number of nodes
  • Supports various cache eviction policies
  • JMX enabled
  • Have been already integrated with many frameworks like Spring, Hibernate, JRuby etc.
  • Rich feature list. Some of which are (I have just highlighted the important points but haven’t gone on to explain these):
    • Multiple transaction model
    • Search capabilities
    • Persistence storage options
    • Monitoring tools availability
    • Distributed cache options etc.
    • Open source project managed by Terracotta along with licensed full support product

Similar to ConcurrentMap based cache; I would like to bring in both advantages as well as disadvantages of EhCache, so as to give you an idea of which one to wisely choose in your application according to your requirement.

Advantages of Using EhCache

Choosing EhCache offers the following advantages:

  • Simpler and easy to implement
  • Ideal for all kinds of data-sets
  • Has built in eviction algorithms
  • Capable of distributed data caching
  • Statistics gathering capability is built-in
  • Configurable built-in heap memory usage patterns. This give full control of ho much heap memory is used by the cache.
  • Data can be stored outside of the Java heap memory and it can be controlled in a configurable way.

Disadvantages of Using EhCache

To be honest, I don’t see any limitation that I can put at this point in time.

Spring’s Support – Other Cache Libraries

Spring Framework’s philosophy of easy pluggability comes in lime light in this as well. Even though it has it has given its choice of integration as discussed earlier. It can be used to integrate with other caching frameworks available in the industry.

There are many caching frameworks available, but in this section we will see specifically two caching framework integration namely Oracle Coherence and SpringSource GemFire.

Oracle Coherence

When configuring Coherence, it allows providing your own CacheStore implementations. CacheStore implementations intern uses some sort of resource, such as a data source. Spring provides the best way to configure all kinds of resources and manage the lifecycle of these resources in your application.

Coherence com.tangosol.net.CacheFactory class, static factory methods allows you to access all Coherence caches and services. These methods are delegate to a com.tangosol.net.ConfigurableCacheFactory interface, which can be plugged in by using theCacheFactory.setConfigurableCacheFactory method or the operational override file of coherence namely tangosol-coherence-override.xml.

The com.tangosol.coherence.spring.SpringAwareCacheFactory interface is a custom ConfigurableCacheFactory which helps in accessing caches in the Coherence configuration file and also gives the ability to reference beans configured using Spring.

The various steps involved in integration of Oracle Coherence with Spring application are summarized in the following sections.

Page Visitors: 13484

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. Every weekend i used to go to see this web site, for the reason that i want enjoyment, since
    this this web page conations actually pleasant funny data too.

    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.. 🙂

      Tomcy John

  2. 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.


    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.

      Tomcy John

Leave a Reply

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