Spring Cache with In-Memory Data Grid

Deprecated: Function create_function() is deprecated in /var/www/html/blogs.alachisoft.com/tayzgrid/wp-content/plugins/wp-spamshield/wp-spamshield.php on line 1164

Spring framework is a very popular lightweight dependency-injection container for Java. It simplifies Java development and the overall complexity of your enterprise applications. And, these Java applications frequently need to handle a lot of transactions for which they require scalability and robustness. And, although they’re able to scale their application tier but their database or data storage becomes a bottleneck.

To overcome these challenges, people are turning to In-Memory Data Grids (IMDG) like TayzGrid. An In-Memory Data Grid is a linearly scalable key value store that lets you cache frequently used data in a distributed manner and helps reduce those expensive database trips. As a result, the pressure on your database is significantly offloaded and is no longer a bottleneck.

Fortunately, Spring provides a project named Spring Cache that not only lets you integrate these IMDGs but also helps you simplify the way you use it. Instead of making caching API calls throughout your program, you simply annotate the methods whose output you want to cache and Spring takes care of the rest.

How Spring Cache Works

To understand how to use Spring Cache and an IMDG, lets take a look at the following function named getbyID which is responsible to fetch some information from the database (or do some computationally expensive calls)

@Cacheable("Customer") //Results of this method will be cached
  public Customer getById(String customerId) {
    //Database calls or Expensive Computations
    return customer;

When you use the @Cacheable annotation on the method getById, Spring Framework starts intercepting all calls to this method. It then checks the cache to see if the method has been previously executed with the provided method parameters and its results are already in the cache. If they are in the cache then it returns those results instead of executing the method again. But, if the method has not been executed, then it executes this method, obtains the results and caches them for future references and then returns the control back to the caller along with the results.

To enable this annotation-driven cache management you need to place @EnableCaching in your controller or Main class. It registers all the necessary Spring Components, responsible for caching, that power the framework’s aspect oriented architecture i.e. intercepting method calls where @Cacheable is provided.

Plugging in an In-Memory Data Grid

The next and the last step is to plug in an IMDG. TayzGrid is one such In-Memory Data Grid and is extremely fast and scalable. It provides Spring Cache integration that you can find in the installation folder. To plug it in, all you need to do is to provide the TayzGrid CacheManager to the Spring framework and then add in all the references from the TayzGrid lib folder. For an example, see the following code

public class Main{

  public void run(String... args) throws Exception { … }

  public CacheManager cacheManager() {
    TayzGridConfigurationManager tgconfig = new TayzGridConfigurationManager();
    TayzGridCacheManager cacheManager = new TayzGridCacheManager();
    return cacheManager;


Now all the @Cacheable annotated method calls will be intercepted and  directed to TayzGrid from here on, storing the method results in a distributed cache.

In the above example I referenced an XML, this tayzgrid-spring.xml stores and configures how TayGrid caches are accessed and you can configure them giving you more control. Create an XML file named tayzgrid-spring.xml in your project and enter the following configuration.

<application-config enable-cache-exception="true"
    <cache name="Customer"
    <cache name="DefaultCache"
           expiration-period="15" />

You can control in which cache you want to store data by changing the name in the cache tag. If you see the @Cacheable annotation I previously used, it was with a string “Customer”. This lets you select where you want your methods results to be cached. Each Cache could have its own cluster giving you complete power on your application architecture. Just remember to start those caches from the TayGridCacheManager before you test your code. You can also configure the behavior of each of your caches separately. For more detail check out the TayzGrid Programmers Guide

That’s it! With a few changes you can now cache your methods and avoid expensive database trips all with the help of Spring Cache. Plugging in TayzGrid helps you to have the ability to scale your architecture at peak loads. You’ll have your data distributed across a cluster providing high availability and high scalability to your applications. You can also integrate TayzGrid without any code change.

TayzGrid is Open Source (Apache 2.0 license) and is totally free to use. Learn more about TayzGrid or download a full working copy.

About Basit Anwer
Basit is a Solution Architect at Alachisoft - provider of a leading In-Memory Data Grid for Java called TayzGrid and for .NET called NCache. Alachisoft has been a market leader for 10 years in the .NET market with NCache.
This entry was posted in In-Memory Data Grid, Java Distributed Cache and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *