Custom TTL for Spring data Redis Cache

Carvia Tech | May 15, 2020 | 2 min read | 23 views

Spring Boot 2.2 provides options to customize Redis Cache using RedisCacheManagerBuilderCustomizer enabling us to set custom value for TTL (time-to-live/expiry) for each of the configured cache.

At the time of writing this article, Spring Boot 2.2.7.RELEASE does not allow configuring TTL value of each individual cache through application.yml

Redis Setup

You can easily setup Redis on Ubuntu machine using the below command

Ubuntu 20.04 LTS
$ sudo apt update
$ sudo apt install redis-server

Check redis service status using the below systemctl command:

$ sudo systemctl status redis-server.service

If you are using Mac, then homebrew can be used to quickly install Redis server.

Mac using homebrew
$ brew install redis

Gradle setup

we will be using Spring Boot 2.2.7.RELEASE for this tutorial with dependencies on Spring Data Redis. You can find about Spring Data Redis project here Spring Boot Features


We need to configure Spring data redis cache in application.yml for propert hostname, port number and cache names.

application.yml localhost
spring.redis.port: 6379
spring.cache.cache-names: cache1, cache2
spring.cache.redis.time-to-live: 600000

Configuration for Redis

Now we will create a custom configuration to create a Bean of type RedisCacheManagerBuilderCustomizer to allow RedisCacheManager configuration. RedisCacheConfiguration allows a method named entryTtl(Duration) that allows us to set custom value for TTL for a given Cache.
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

public class RedisConfig {

    RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
        return (builder) -> {
            Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>();
            configurationMap.put("cache1", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(20)));  (1)
            configurationMap.put("cache2", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(2)));     (2)

1 cache1 will have expiry of 20 seconds from time of creation.
2 cache2 will have expiry of 2 hours from time of creation (time-to-live).

Kotlin equivalent of the same configuration is this:

class RedisConfig {

    fun redisCacheManagerBuilderCustomizer() = RedisCacheManagerBuilderCustomizer { builder ->
        val configurationMap = HashMap<String, RedisCacheConfiguration>()
        configurationMap["cache1"] = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(20))
        configurationMap["cache2"] = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(2))

That’s all.

Top articles in this category:
  1. Custom banner in spring boot
  2. Setting a Random Port in Spring Boot Application at startup
  3. Prevent Lost Updates in Database Transaction using Spring Hibernate
  4. Spring Boot 2.0 Reactive Web Performance Metrics
  5. How to prevent duplicate form submission in Spring MVC
  6. Spring Data ElasticSearch using HTTPS and Basic Auth
  7. Basic Auth Security in Spring Boot 2

Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info