0Pricing
Spring Boot 4 Microservices & REST APIs · Pelajaran

Redis sebagai Penyedia Cache

Konfigurasikan cache yang didukung Redis.

Redis sebagai Penyedia Cache adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Boot 4 Microservices & REST APIs, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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=redis

Auto-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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Redis sebagai Penyedia Cache” gratis?

Ya — teks lengkap “Redis sebagai Penyedia Cache” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Boot 4 Microservices & REST APIs, upgrade ke CoddyKit PRO. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Redis sebagai Penyedia Cache”?

Konfigurasikan cache yang didukung Redis. Kamu berlatih Spring Boot 4 Microservices & REST APIs dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Spring Boot 4 Microservices & REST APIs?

Tidak diperlukan pengalaman sebelumnya. Spring Boot 4 Microservices & REST APIs di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.

Berapa lama pelajaran “Redis sebagai Penyedia Cache” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Boot 4 Microservices & REST APIs ini?

Ya. Setiap pelajaran Spring Boot 4 Microservices & REST APIs menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Abstraksi Cache Spring
  2. @Cacheable, @CachePut, @CacheEvict
  3. Redis sebagai Penyedia Cache
  4. TTL dan Invalidasi Cache
← Kembali ke Spring Boot 4 Microservices & REST APIs