Redis jako dostawca cache
Konfiguruj cache oparty na Redisie
Redis jako dostawca cache to bezpłatna lekcja Spring Boot 4 Microservices & REST APIs na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Boot 4 Microservices & REST APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Why Redis for Caching
Redis is an in-memory data store that works as a distributed cache shared across many app instances.
- Survives app restarts
- Shared by all nodes
- Supports TTL and eviction
Adding the Dependency
Add Spring Data Redis to switch the cache backend to Redis.
// build.gradle
implementation "org.springframework.boot:spring-boot-starter-data-redis"Connection Settings
Point Spring at your Redis server in properties.
# application.properties
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.cache.type=redisAuto-Configured RedisCacheManager
With Redis on the classpath and spring.cache.type=redis, Spring Boot auto-configures a RedisCacheManager. Your existing @Cacheable methods now use Redis with no code change.
Customizing the CacheManager
Define a RedisCacheManager bean for fine control over defaults.
@Bean
public RedisCacheManager cacheManager(
RedisConnectionFactory factory) {
RedisCacheConfiguration config =
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10));
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
}Serialization
Cached values are serialized before storage. JDK serialization is the default; JSON is more portable and readable.
RedisCacheConfiguration config =
RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(
SerializationPair.fromSerializer(
new GenericJackson2JsonRedisSerializer()));Serializable Entities
For JDK serialization, cached objects must implement Serializable. JSON serialization avoids this requirement.
public class Product implements Serializable {
private Long id;
private String name;
}Key Prefixes
Redis cache keys are prefixed with the cache name by default (e.g. products::1), keeping caches namespaced in the shared store.
Per-Cache Configuration
Give individual caches their own TTL via a map of configurations.
Map<String, RedisCacheConfiguration> configs =
Map.of("products",
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(5)));
return RedisCacheManager.builder(factory)
.withInitialCacheConfigurations(configs)
.build();RedisTemplate for Direct Access
Beyond the cache abstraction, RedisTemplate lets you read and write Redis directly.
@Autowired
RedisTemplate<String, String> redisTemplate;
redisTemplate.opsForValue()
.set("greeting", "hello",
Duration.ofSeconds(30));Resilience to Redis Outages
If Redis is unreachable, cache operations fail. Consider a fallback strategy or error handler so a cache outage does not take down the app.
Quick Check
Test your understanding of Redis caching.
Recap
You learned to use Redis as a cache:
- Add the data-redis starter, set
spring.cache.type=redis - Auto-configured
RedisCacheManager - Customize TTL and JSON serialization via a bean
- Keys are namespaced by cache name
Często zadawane pytania
Czy lekcja „Redis jako dostawca cache” jest bezpłatna?
Tak — pełny tekst „Redis jako dostawca cache” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Boot 4 Microservices & REST APIs, przejdź na CoddyKit PRO. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.
Co nauczysz się w „Redis jako dostawca cache”?
Konfiguruj cache oparty na Redisie Ćwiczysz Spring Boot 4 Microservices & REST APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Spring Boot 4 Microservices & REST APIs?
Nie wymagamy żadnego doświadczenia. Spring Boot 4 Microservices & REST APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Redis jako dostawca cache”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Spring Boot 4 Microservices & REST APIs?
Tak. Każda lekcja Spring Boot 4 Microservices & REST APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Abstrakcja cache Spring
- @Cacheable, @CachePut, @CacheEvict
- Redis jako dostawca cache
- TTL i unieważnianie cache