Skip to main content

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




Popular posts from this blog

Change Port on a Spring Boot Application when using intelliJ

Change Port on a Spring Boot Application when using intelliJ  Two ways to change the port that the embedded Tomcat is using in a Spring Boot Application. The first way is to modify the application properties in the resources folder: Update application props in Spring Boot Application There are a couple issues with this approach.  You might only want this port change in your local environment and if you check it in by accident you might break things. Alternatively, you can use a command line parameter. Go to Run -> Edit Configurations -> VM options Then enter in the following argument: -Dserver.port=8090 Then click apply and OK and attempt to run the application again. Update port using VM option

How to set up a SQL Server 2008 Local Database

Setting up and Configuring your Microsoft SQL Server 2008 Local Database Download SQL Server Download SQL Server Express.  Express is the Free edition of MS SQL Server http://www.microsoft.com/en-us/download/details.aspx?id=1695 Install your SQL Server Instance Click the Start button -> Click Microsoft SQL Server 2008 -> Click Configuration Tools -> Select SQL Server Installation Center Select Installation -> Select New SQL Server stand-alone installation Select SQL Server stand-alone installation Get your Local SQL Server Service Up and Running Open up the SQL Server Configuration Manager Click the Start button -> Click Microsoft SQL Server 2008 -> Click Configuration Tools -> Select SQL Server Configuration Manager Setup SQL Server Config Manager Start up the SQL Services You want to ensure the following services are in the "Running" State and then you want to set the following services to automatic St...

Building Microservices with JHipster

Check out this article on Medium and LinkedIn Pulse, for a brief overview of microservices as a primer for working with JHipster to build web applications with a Microservices backend. Building microservices with Jhipster JHipster is sponsored by Ippon Technologies(https://www.ippon.tech/). Need cost-effective fractional engineering with US expertise? Fintech and Blockchain Specialized Fractional Engineering