Spring Book – Chapter 10 – Caching

Custom Annotation

Similar to transaction management, you can write your own customer annotation to minimize duplication of cache annotation declaration. Similar to other stereotype annotations, both @Cacheable and @CacheEvict can be used as meta-annotations (annotations that can annotate other annotations). Listing 10-15 shows using @Cacheable with other annotations in a method and then using stereotype annotation is created and used to avoid duplication.

Listing 10-15. Custom annotation creation and its use

Use of @Cacheable without using stereotype annotation

[java]@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.METHOD})

@Cacheable(value=“customers”, key="#customerCode")

public Customer getCustomer(String customerCode){

//…..

}[/java]

Writing a stereotype annotation

[java]@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.METHOD})

@Cacheable(value=“customers”, key="#customerCode")

public @interface CustomerCacheable {

}[/java]

Using streotype annotation avoiding duplication

[java]@CustomerCacheable

public Book Customer getCustomer(String customerCode){

//….

}[/java]

Declarative XML-based Caching

You have a choice of completely eliminating Java source code change for caching by making use of Spring AOP and XML Spring configuration file. Listing 10-16 shows the XML configuration in which Spring AOP is used for cache abstraction.

Listing 10-16. XML configuration using Spring AOP for declaring cache abstraction

[xml]<!– the service we want to make cacheable. A third party class. –>

<!– cache definitions –>

<!– apply the cache behaviour to all classes in the third party jar starting with package x.y –>

[/xml]

Declaration of cache abstraction using XML configuration can be used when source code is not available and you would still want to have the target method cached. This is achieved by using Spring AOP. Listing 10-15 shows one such example.

Cache Manager

Managing of cache is achieved in Spring Framework using the org.springframework.cache.CacheManager interface. It serves as a manager to multiple caches in your application. Through this interface it is possible to dynamically add and remove caches at runtime. To use any cache implementation not supported by Spring, you can implement this interface and plug it into your Spring application with ease. Listing 10-17 shows the source for CacheManager interface.

Listing 10-17. CacheManager interface in Spring Framework

[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]

The following sections describe some cache managers which Spring Framework has provided along with the frameworks. These cache managers are described in some detail so that if your application suffices using these cache managers, you can do so very easily with little effort of configuring.

Page Visitors: 13493

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

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

    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 *