Tuesday, January 2, 2018

Add Guava Cache to Spring Boot to Cache Method Calls

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 dont 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






go to link download
download
alternative link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.