0Pricing
Spring Boot 4 Microservices & REST APIs · บทเรียน

Redis ในฐานะผู้ให้บริการแคช

กำหนดค่าการแคชที่ใช้ Redis

Redis ในฐานะผู้ให้บริการแคช เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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

คำถามที่พบบ่อย

บทเรียน “Redis ในฐานะผู้ให้บริการแคช” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Redis ในฐานะผู้ให้บริการแคช” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Redis ในฐานะผู้ให้บริการแคช”

กำหนดค่าการแคชที่ใช้ Redis คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “Redis ในฐานะผู้ให้บริการแคช” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม

ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แนวคิดนามธรรมของแคช Spring
  2. @Cacheable, @CachePut, @CacheEvict
  3. Redis ในฐานะผู้ให้บริการแคช
  4. TTL ของแคชและการทำให้ไม่ถูกต้อง
← กลับไปที่ Spring Boot 4 Microservices & REST APIs