Add Guava Cache to Spring Boot to Cache Method Calls


Add Google Guava Cache to Spring Boot to Cache the Results of Method Calls using Java Config and Spring Annotations



If you have a time-intensive method that is slowing down your application and the results of that method don't change very often, it is a good candidate for caching.  This guide will show you how to implement caching in your Spring Boot application using Google Guava cache.  

Guava cache is a simple, lightweight in-memory cache that has more configuration options then the default In-Memory Spring Cache.  Guava cache is not for clustered systems, for that you will want to use Hazelcast http://hazelcast.com/

So if you just need a simple cache but you want more config options such as cache expiration time, then this approach may fit your use case.  Alternatively, you could use EhCache.

Full source code references can be found at the bottom of the article.

More info on Guava Cache:
https://code.google.com/p/guava-libraries/wiki/CachesExplained

More info on Spring Cache:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html

You can view the complete source code here: https://gist.github.com/anataliocs/1074e7004b2c99320b7a


Add dependencies to your Maven pom.xml file



Add the two following dependencies to your maven pom file and clean and package your dependencies.  You will need to add Guava and a Spring library that contains helper methods and classes for configuring the cache.

Add dependencies to your pom.xml for google guava cache and helper spring methods
Add dependencies to your pom.xml for guava cache

Create CacheConfig file



You need to create a CacheConfig file to configure the cache using Java config.  Create this config file as a separate class implementing CachingConfigurer and place it in your config folder with your other Java config classes.

Add Cache Manager and setup Guava Cache
Add Cache Manager and setup Guava Cache



Add Cacheable annotation to method to be cached



You will need to add the @Cacheable annotation and pass in the name of the cache which will be used for that method.

If your method has a passed in parameter, results for each different parameter will be cached.  So, each time a new parameter is passed in, this will execute the method and place the results in the cache.  Future calls using that parameter will pull results from the cache and skip execution of the method.

Add Cacheable annotation to the method to be cached
Add Cacheable annotation to the method to be cached





Add Endpoint to Clear Cache on Demand



You will also probably want to add an endpoint to easily refresh the cache.  You can do this by adding an endpoint to a controller with the @CacheEvict annotation.

This method will completely clear the cache.  All future requests will execute the method until the cache is populated again.

You may want to protect this endpoint using basic security implemented through Spring Security


Add an endpoint to clear the cache
Add an endpoint to clear the cache

Other considerations and Source Code



You may want to initially warm the cache and call the method with all possible parameters such that even initial users access the cached version.


You can view the complete source code here: https://gist.github.com/anataliocs/1074e7004b2c99320b7a




Comments

  1. Nice Article! Codevian Technologies is a professional PHP development company. We provide our services and best results to our customers. We bring great websites and web application of every size to our clients. We transform your dream projects into reality. Codevian Technologies is the right place to hire php developers. Please feel free to call us on +91 9225108952 or contact by email at sales@codevian.com, if you require any additional information. Please visit our website www.codevian.com.com

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thanks so much for such an encouraging post. I’ve read the blog and thought it was really insightful.
    Data Science Training in Hyderabad
    Data Science Course in Hyderabad

    ReplyDelete
  4. This article is very useful and gives us a lot of information and tips. The post is explained clearly so that we are able to understand it clearly.
    AWS Training in Hyderabad
    AWS Course in Hyderabad

    ReplyDelete
  5. Really this article is truly one of the best in article history and am a collector of old "items" and sometimes read new items if I find them interesting which is one that I found quite fascinating and should be part of my collection. Very good work!
    Data Scientist Course in Gurgaon

    ReplyDelete

Post a Comment

Popular posts from this blog

Change Port on a Spring Boot Application when using intelliJ

New Personal Website

How to set up a SQL Server 2008 Local Database