• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Configure Application using Caching Declaration

After configuring your Spring application and enabling caching, the next step is to identify the methods that need to be cached and their respective policies.

You can bind the caching behaviors to the methods in two ways mentioned below.

  • Caching Annotations
  • Declarative XML based Caching

Configure using Caching Annotations

For caching declaration, the abstraction provides Java annotations which allows methods to trigger cache population or cache eviction.

Sample Example

Let’s take a simple example of web service with two classes that explains how caching is performed using @Cacheable annotation. Here, every call to the endpoint /books/isbn would go to the method getBookNameByIsbn() which in turn would execute the method findBookInSlowSource() only if it does not find value in the cache.

// BookController.java
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/{isbn}")
    public String getBookNameByIsbn(@PathVariable("isbn") String isbn) {
        return bookService.getBookNameByIsbn(isbn);
    }
}
// BookService.java
@Service
public class BookService {
    @Cacheable("booksCache")
    public String getBookNameByIsbn(String isbn) {
        return findBookInSlowSource(isbn);
    }

    private String findBookInSlowSource(String isbn) {
        // some long processing
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Sample Book Name";
    }
}

To get a detailed understanding and to learn more about other Caching Annotations or creation of Custom Key Generator, refer to the Spring Documentation.

Configure using Declarative XML based Caching

If annotations are not an option (no access to the sources or no external code), you can use XML for declarative caching. So instead of annotating the methods for caching, you can specify the target method and the caching directives externally.

In the configuration below, the bookService is made cacheable. The caching semantics to apply are encapsulated in the cache:advice definition which instructs method getBookNameByIsbn to be used for putting data into the cache while method loadBooks for evicting data. Both definitions are working against the booksCache.

<!-- the service we want to make cacheable -->
<bean id="bookService" class="x.y.service.BookService"/>

<!-- cache definitions -->
<cache:advice id="booksCacheAdvice" cache-manager="cacheManager">
    <cache:caching cache="booksCache">
        <cache:cacheable method="getBookNameByIsbn" key="#isbn"/>
        <cache:cache-evict method="loadBooks" all-entries="true"/>
    </cache:caching>
</cache:advice>

Also, for using custom key generator for cache key generation through XML based caching, beans for key generator need to be defined as below with CustomerKeyGenerator as class.

<bean id="customerKeyGenerator" class="cachekeygenerator.CustomerKeyGenerator">

Here, the customerKeyGenerator is used to generate cache key whenever a customer is added to the customersCache. Also, you need to specify keygenerator bean in your cache:advice tag.

<cache:advice id="customerCacheAdvice" key-generator="customerKeyGenerator">
    <cache:caching cache="customersCache">
        <cache:cacheable method="findCustomerByID"/>
    </cache:caching>
</cache:advice>

After modifying all the configurations, your application is now ready to use NCache as Spring Caching Provider.

See Also

Configure Application for Generic Spring Caching Provider
Configure Application for JCache Caching Provider
NCache as Spring Data Cache

Back to top Copyright © 2017 Alachisoft