Spring provides abstraction for adding caching into application and supporting Redis as one of its providers. This tutorial will give you some highlights when implement in Spring Boot

What you'll need

  • JDK 8+ or OpenJDK 8+
  • Maven 3+
  • Redis 5+ or Docker CE 18+

Dependencies

Beside other Spring Boot starter dependencies, include the Data Redis starter to get the auto-configuration and access to Redis

<dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>  

Auto-configure Redis and Caching

Spring Boot provides the following auto-configuration for Redis and caching

  • RedisCacheManager is auto-configured when Redis is available and configured. You can use spring.redis.* properties to tune the default settings, for instance
spring.redis.host=hk-redis  
spring.redis.timeout=2000
  • Cache default settings can be configured by using spring.cache.redis.* properties
spring.cache.redis.key-prefix=hellokoding::  
spring.cache.redis.time-to-live=100000

spring.redis.timeout and spring.cache.redis.time-to-live value are in milliseconds

How caching works

  • Data in a cache stored in key-value pairs

  • Caching in Spring uses Java Serialization by default to serialize key and value objects. You can implement Serializable to your objects or change the default configuration to use other RedisSerializer implementations such as GenericJackson2JsonRedisSerializer

import java.io.Serializable;

public class Product implements Serializable {  
}

Apply caching into a method

Add the @Cacheable annotation to the method you want to cache its result

import org.springframework.cache.annotation.Cacheable;

...

@Cacheable(key = "#id")
public Optional<Product> findById(Long id) {  
    return productRepository.findById(id);
}

The key attribute is used to define the cache key in SpEL expression

If no value is found in the cache, the target method will be invoked and returned value stored in the associated cache

Enable the caching capability

The @EnableCaching annotation enables Spring caching capability, can be used together with @Configuration when you need to customize the default configuration

import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Application {  
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Internally, @EnableCaching triggers a post processor to inspect, intercept and handle behaviour of caching annotations such as @Cachable on public methods

Conclusion

In this tutorial, we walk though some highlights note when implements caching in Spring Boot applications. You can find the implementation source code at https://github.com/hellokoding/hellokoding-courses/tree/master/springboot-examples/springboot-caching-redis

Share to social

Van N.

Van N. is a software engineer, creator of HelloKoding. He loves coding, blogging, and traveling. You may find him on GitHub and LinkedIn