Caching Data in Spring Using Redis

Caching is a way for applications to store data so that future requests to the same data are returned faster and do not require repeating computationally expensive operations. The Spring Framework provides a simple way to cache the results of method calls with little to no configuration.

The examples in this post use Spring 4.1.3 and Spring Data Redis 1.4.1, the latest versions at the time of this writing. Full source code for this post can be found here.

Installing Dependencies

You'll need to install the spring-data-redis and jedis plugins. Add the following to your pom.xml file in your Spring project if you're using Maven:

Enabling Cache Support in Your Spring Application

To enable support for caching in your application, you'll want to create a new CacheManager bean. There are many different implementations of the CacheManager interface, but for this post we'll be using RedisCacheManager to allow integration with Redis.

Java-based Configuration

If you're using Java-based configuration, you'll want to annotate one of your configuration classes with the @EnableCaching annotation and expose a CacheManager bean. You'll also need a connection factory and a RedisTemplate bean for Spring to communicate with your Redis database. An example CacheConfig class is shown below:

Caching the Results of a Method

After setting up your caching configuration, you can then begin to start caching method results with the @Cacheable annotation. Putting this annotation on a method will cause it's results to be cached in your Redis database for the length specified in your expiration, or until it is manually expired.

Updating and Evicting Cached Data

In addition to the @Cacheable annotation, Spring provides the @CachePut and @CacheEvict annotations for manually managing cached data. The @CachePut annotation allows you to update the cache without interfering with the execution of the method. For example, if you updated a user, you would want to place the result into the cache to be looked up later:

Implementing a Custom Key Generator

For basic purposes, the default key generation system for cached data works. The parameters to your method become the key in your cache store. For example, in the following method, the value for the parameter username would become the key in your store:

@Cacheable("users")
public User findByUsername(String username)

But this becomes problematic when you want cache the result of another method that also takes in the same value, for instance:

You could easily just specify a different key for each @Cacheable annotation, but that becomes tedious and hard to maintain. The solution is to implement a custom key generator that will generate the key for each method to be unique by default. Adding a keyGenerator bean to your CacheConfig class (shown above) will give you this functionality. Note that for your custom key generator to work by default, this class must implement the CachingConfigurer interface. Extending CachingConfigurerSupport will provide this for you.

Now when your method results are cached, they will be cached with your custom key generator implementation.

Conclusion

This is a simple example of how to cache application data in your Redis instance. Although this post focuses on caching with Redis, caching with other providers is very straightforward and the caching concepts remain the same. For more information and complete documentation, see Spring's caching reference. Feel free to leave a comment if you have any questions or run into any problems.