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
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();
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
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
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
Coherence Configuration File
Listing 10-30 shows the Coherence configuration file referring to various items configured earlier.
Listing 10-30. Coherence configuration file
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 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:
- Configure GemFire’s configuration file namely gemfire.properties – it defines GemFire’s global properties.
Listing 10-31. Sample gemfire.properties file
- 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"?–>
- 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
- 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
- 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
That’s all; you have now successfully integrated GemFire onto your Spring application to manage the cache.
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: 12284