A beginner-friendly Spring Boot project that demonstrates Redis caching using Spring Cache (@Cacheable) with a TTL (time-to-live).
The first request is slow (simulated external API call), and the next requests are fast because the response is served from Redis until the TTL expires.
- Java 21
- Spring Boot
- Spring Cache (
@EnableCaching,@Cacheable) - Spring Data Redis (Redis as cache store)
- Docker Compose (Redis container)
- Gradle
- The API endpoint calls a service method annotated with
@Cacheable. - On the first request (cache miss), the method runs and stores the result in Redis.
- On the next request (cache hit), Spring returns the cached value from Redis.
- After the TTL expires, the cache entry is removed and the next request refreshes it.
From the project root:
docker compose up -d