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

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:

More info on Spring Cache:

You can view the complete source code here:

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:


  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, if you require any additional information. Please visit our website

  2. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here. Spring Boot Online Training


Post a Comment

Popular posts from this blog

How to set up a SQL Server 2008 Local Database

Change Port on a Spring Boot Application when using intelliJ