Видео готовится

Что нужно сделать, чтобы подключить Spring Boot приложение к Redis?

Добавляется spring-boot-starter-data-redis, в application.yml указываются host/port. После этого Boot сам создаёт Lettuce connection factory и StringRedisTemplate, их остаётся только inject-ить и пользоваться.

📖 Ответ

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
  data:
    redis:
      host: localhost
      port: 6379
      password: ${REDIS_PASSWORD:}
      timeout: 2s

В Boot 3 префикс spring.data.redis; в Boot 2 был spring.redis — если копируете из старых статей, настройка просто не сработает.

@Service
class OtpService {
    private final StringRedisTemplate redis;
    OtpService(StringRedisTemplate redis) { this.redis = redis; }

    void save(String phone, String code) {
        redis.opsForValue().set("otp:" + phone, code, Duration.ofMinutes(2));
    }
}

Для локальных проверок хватит docker run -p 6379:6379 redis:7, а в тестах удобен Testcontainers.