0
0
SpringbootHow-ToBeginner · 4 min read

How to Use Redis Cache in Spring Boot: Simple Guide

To use Redis cache in Spring Boot, add the spring-boot-starter-data-redis dependency, configure Redis connection properties, and enable caching with @EnableCaching. Then, annotate methods with @Cacheable to cache their results automatically.
📐

Syntax

To use Redis cache in Spring Boot, you need to:

  • Add the Redis starter dependency.
  • Configure Redis connection in application.properties.
  • Enable caching with @EnableCaching in a configuration class.
  • Use @Cacheable on methods to cache their return values.
java
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
}

# application.properties
spring.redis.host=localhost
spring.redis.port=6379

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

// Service method example
@Cacheable("items")
public List<String> getItems() {
    // method logic
}
💻

Example

This example shows a Spring Boot application that caches a method's result using Redis. The @Cacheable annotation caches the output of getData() so repeated calls return cached data without executing the method again.

java
package com.example.redis;

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

import java.util.List;
import java.util.Arrays;

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

@Service
class DataService {
    @Cacheable("dataCache")
    public List<String> getData() {
        System.out.println("Fetching data from method");
        return Arrays.asList("apple", "banana", "cherry");
    }
}
Output
Fetching data from method // On first call, prints this and caches result // On subsequent calls, no print, returns cached list
⚠️

Common Pitfalls

Common mistakes when using Redis cache in Spring Boot include:

  • Not enabling caching with @EnableCaching, so cache annotations have no effect.
  • Missing Redis server running or wrong connection settings causing connection failures.
  • Using non-serializable objects as cache values, which Redis cannot store.
  • Not configuring a proper Redis serializer, leading to unreadable cached data.
java
/* Wrong: Missing @EnableCaching annotation */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

/* Right: Add @EnableCaching to enable caching */
@SpringBootApplication
@EnableCaching
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
📊

Quick Reference

Summary tips for using Redis cache in Spring Boot:

  • Add spring-boot-starter-data-redis dependency.
  • Configure Redis host and port in application.properties.
  • Enable caching with @EnableCaching.
  • Use @Cacheable, @CachePut, and @CacheEvict annotations to manage cache.
  • Ensure Redis server is running and accessible.

Key Takeaways

Add spring-boot-starter-data-redis and configure Redis connection to use Redis cache in Spring Boot.
Enable caching with @EnableCaching annotation in your main or configuration class.
Use @Cacheable on methods to cache their results automatically in Redis.
Ensure Redis server is running and connection properties are correct to avoid errors.
Avoid caching non-serializable objects or configure serializers properly for Redis.