Cache Abstraction in Spring
Caching is an important feature for any application which needs high performance. Many open-source frameworks are available which provide us with the necessary caching frameworks like Terracotta’s EhCache, Oracle’s Coherance, SpringSource’s Gemfire etc.
The ease with which to integrate your application with such frameworks varies and depends on various factors. Spring Framework provides abstraction over this complexity by providing common set of API’s which can be used to interact with the underlying cache providing frameworks with ease.
Since Spring provides only an abstract layer, it needs concrete implementation to actually do the work of caching in your application. The entry point for implementing cache is by implementing caching central interface called CacheManager. Listing 10-1 shows this CacheManager interface.
Listing 10-1. Central interface for cache abstraction in Spring – CacheManager
[java]package org.springframework.cache;
import java.util.Collection;
public interface CacheManager {
/**
* Return the cache associated with the given name.
* @param name cache identifier (must not be {@code null})
* @return associated cache, or {@code null} if none is found
*/
Cache getCache(String name);
/**
* Return a collection of the caches known by this cache manager.
* @return names of caches known by the cache manager.
*/
Collection getCacheNames();
}[/java]
By default Spring provides two concrete implementation of this CacheManager interface. They are:
- EhCacheCacheManager – default implementation for Terracotta’s EhCache framework.
- ConcurrentMapCacheManager – default implementation for Java’s ConcurrentHashMap as cache store.
Cache managers will be discussed in detail later on in this same chapter with appropriate code samples for easy understanding.
Cache abstraction in Spring can be configured declaratively in two ways similar to trsanction management. They are:
- Declarative Annotation-based
- Declarative XML-based
By doing declarative caching for your Spring application, you have the following advantages as listed below:
- Minimal impact on the code
- Ease of plugging in various caching solutions
Using annotations effectively for declaratively configuring caching in your application has following advantages:
- Easier to use
- Cache semantics close to affected Java source code making the code well documented
Declarative Annotation-based caching
The general steps to be followed for adding declarative cache management to your application can be summarized as follows:
- Step 1: Declare the cache manager bean and implementation instance (factory bean), which will be used in your application, in the Spring configuration file as shown in Listing 10-2 below.
Listing 10-2. CacheManager and factory bean instance configuration in Spring configuration file
[xml]
<!– Ehcache library setup –>
[/xml]
- Step 2: In the XML configuration enable the scanning of cache related annotation. It is done as shown in the Listing 10-3 in the XML configuration.
Listing 10-3. Allow cache annotation scanning in your application
[xml][/xml]
The various attributes in this tag is explained below:
- cache-manager: id or name of the bean implementing the CacheManager interface. It can be omitted if the cache manager bean in your XML file is named “cacheManager”, as Spring will look for any bean with this name by default.
- mode: Allowed values are “proxy” or “aspectj”. Putting the value as “proxy” will use Spring AOP framework to proxy any class having a caching annotation and putting the value as “aspectj” will rely on AspectJ aspect for cache management.
- order: It is an optional attribute. If you have more than one aspect declared on the same method, it can be useful to specify the order in which they execute
- Step 3: Use cache related annotations in your Java source code as required by your application. Listing 10-4 shows a sample code snippet in a class how the annotations will be used.
Listing 10-4. Cache annotation usage in Java source code
[java]public class LoyaltyService {
…
@Cacheable(value="customers", key="#customerCode")
public Customer getCustomer(String customerCode){
//Do something…
}
}[/java]
We will see integration details of various caching frameworks with the Spring Framework later in other sections of this same chapter in detail.
Page Visitors: 13097

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
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.
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
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
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