Spring Book – Chapter 10 – Caching

Configuring Coherence for SpringAwareCacheFactory

You can place an operational override file namely tangosol-coherence-override.xml, which specifies the main coherence file coherence-cache-config.xml and the Spring application configuration file namely applicationContext.xml. Listing 10-25 shows the tangosol-coherence-override.xml file which is used for configuring the cache to use SpringAwareCacheFactory.

Listing 10-25. The tangosol-coherence-override.xml file

[xml]…

com.tangosol.coherence.spring.SpringAwareCacheFactory

java.lang.String

coherence-cache-config.xml

java.lang.String

applicationContext.xml

…[/xml]

Configuring Coherence for SpringAwareCacheFactory Programmatically

Listing 10-26 shows programmatically configuring the coherence cache to use SpringAwareCacheFactory.

Listing 10-26. Configuring SpringAwareCacheFactory programmatically

[java]BeanFactory bf  = …//Get the bean factory from Spring configuration file

SpringAwareCacheFactory scf = new SpringAwareCacheFactory();

scf.setBeanFactory(bf);

CacheFactory.setConfigurableCacheFactory(scf);[/java]

Configure the SpringAwareCacheFactory in the Application Context

Listing 10-27 configuring SpringAwareCacheFactory as a Spring bean in applicationContext.xml.

Listing 10-27. Configuring SpringAwareCacheFactory as a Spring bean

[xml]

[/xml]

Plugging in the CacheFactory in Spring application Context

Listing 10-28 shows configuring CacheFactory in the Spring configuration file.

Listing 10-28. CacheFactory configuration in Spring configuration file

[xml]

[/xml]

Configuring CacheStore in the Spring Application Context

You have the provision to configure any CacheStore of your choice. Coherence comes with a built-in store namely HibernateCacheStore. Listing 10-29 shows configuring the CacheStore in the Spring configuration file using HibernateCacheStore which ships with the Coherence distribution. Here, cacheStore bean has prototype as its scope, which forces Spring to produce a new bean instance each time it needs one and lets Coherence manage its life cycle referred in the Coherence configuration shown in Listing 10-30.

Listing 10-29. CacheStore configuration in Spring configuration file

[xml]

[/xml]

Coherence Configuration File

Listing 10-30 shows the Coherence configuration file referring to various items configured earlier.

Listing 10-30. Coherence configuration file

[xml]

*

distributed-domain

distributed-domain

spring-bean:cacheStore

setEntityName

{cache-name}

5s

true

[/xml]

That’s all. We have successfully configured Oracle Coherence cache with a Spring application. Now you can use the @Cacheable annotation on methods to do the necessary caching as required in your application.

GemFire

GemFire is an in-memory distributed caching framework which pools memory across multiple processes to manage application objects and its behavior. Using dynamic replication and data partitioning techniques, it offers continuous availability, high performance and linear scalability for data intensive applications without compromising on data consistency.

By adding a GemFire configuration and a couple of Spring beans, your application’s data will be backed by a scalable, fault-tolerant, highly available distributed cache system without changing any Java code.

The various steps involved in integration of GemFire with Spring application can be summarized as follows:

  1. Configure GemFire’s configuration file namely gemfire.properties – it defines GemFire’s global properties.

Listing 10-31. Sample gemfire.properties file

[xml]\# gemfire.properties

mcast-port=40001

cache-xml-file=cache.xml

name=Gemfire_Spring_Integration[/xml]

  1. Configure GemFire’s distributed data structure configuration file cache.xml – defines the distributed data structures that will be used by this GemFire-enabled application.

Listing 10-32. Sample GemFire’s cache.xml file

[xml]<!–?xml version="1.0" encoding="UTF-8"?–>

[/xml]

  1. Configure GemFire’s distributed system bean in Spring configuration file – When this bean is instantiated, this peer will connect to the GemFire distributed system.

Listing 10-33. Distributed system bean configuration

[xml]

[/xml]

  1. Configure GemFire’s cache bean in Spring configuration file – When this bean is instantiated, GemFire will parse cache.xml file and initialize the distributed data structures defined there. At this point, the GemFire initialization process is complete.

Listing 10-34. Cache bean configuration

[xml]

[/xml]

  1. Configure GemFire’s region bean in Spring configuration file – Now that GemFire is initialized, we can retrieve the Region that backs the application’s cache.

Listing 10-35. Region bean configuration

[xml]

[/xml]

That’s all; you have now successfully integrated GemFire onto your Spring application to manage the cache.

Summary

Caching is an important optimization technique for vast majority of applications. Each enterprise application tier is rife with opportunities for caching. There is variety of options to match application needs. Spring cache abstraction brings to developers the possibility of method caching in applications with a minimal code changes and simple configuration to control the cache behavior. Since Spring provides an abstraction, which makes it extensible to support more cache managers in future or to provide your own ones.

After going through this chapter, you should have got a clear picture of the caching abstraction provided by the Spring Framework. We started off this chapter with some terminologies and then dived deep into the cache abstraction support provided by Spring. We covered the Cache libraries/frameworks which Spring supports and then we dived deep into each one. We then went on to discuss the various cache managers in detail. Finally we went onto discuss integration of Spring with other caching libraries/frameworks apart from the built-in ones so as to make you understand that this is also possible to achieve.

Page Visitors: 10471

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 the Senior Technical Engineer 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 Principal Architect to facilitate incorporating Spring as Corporate Standard in the organization.

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 *