@Cacheable и Redis.Первый запрос с конкретным ключом идет в реальный API.
Все повторные — из Redis за миллисекунды.
<!-- Добавляем зависимости -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-b
# Базовая настройка в application.yml
spring:
redis:
host: localhost
port: 6379
cache:
type: redis
server:
port: 8080
// Конфигурация Redis (TTL 10 минут)
@Bean
public RedisCacheConfiguration cacheConfig() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10))
.disableCachingNullValues();
}
// Кэшируем метод сервиса
@Cacheable("users")
public User getUserById(Long id) {
return userService.fetchUserFromRemoteApi(id);
}
Вот ссылка на демо-проект на Spring Boot (для работы нужен запущенный Redis на порту 6379)
👉 Java Portal
