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

@Cacheable, @CachePut, @CacheEvict

ควบคุมพฤติกรรมการแคชอย่างแม่นยำ

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

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

The Three Cache Annotations

Spring offers three method-level cache annotations:

  • @Cacheable — read-through, store the result
  • @CachePut — always run, update the cache
  • @CacheEvict — remove entries

@Cacheable

@Cacheable checks the cache first; on a miss it runs the method and stores the result.

@Cacheable("products")
public Product findById(Long id) {
    return repository.findById(id).orElseThrow();
}

Conditional Caching

Use condition to cache only when a predicate holds, and unless to skip caching certain results.

@Cacheable(value = "products",
    unless = "#result == null")
public Product findById(Long id) { ... }

@CachePut

@CachePut always executes the method and stores the returned value — ideal for update methods that should refresh the cache.

@CachePut(value = "products", key = "#product.id")
public Product update(Product product) {
    return repository.save(product);
}

Cacheable vs CachePut

@Cacheable may skip the method on a hit; @CachePut never skips. Never put both on the same method — their behaviors conflict.

@CacheEvict

@CacheEvict removes an entry, typically after a delete or update that invalidates cached data.

@CacheEvict(value = "products", key = "#id")
public void delete(Long id) {
    repository.deleteById(id);
}

Evicting Everything

Use allEntries = true to clear an entire cache at once.

@CacheEvict(value = "products", allEntries = true)
public void reloadAll() {
    // bulk refresh
}

Evict Before or After

By default eviction happens after the method succeeds. Set beforeInvocation = true to evict even if the method throws.

@CacheEvict(value = "products",
    allEntries = true, beforeInvocation = true)
public void risky() { ... }

Grouping with @Caching

@Caching combines multiple cache operations on one method.

@Caching(
    put = @CachePut(value = "products", key = "#p.id"),
    evict = @CacheEvict(value = "productList",
        allEntries = true))
public Product save(Product p) {
    return repository.save(p);
}

Class-Level Config

@CacheConfig sets a default cache name for all methods in a class, reducing repetition.

@Service
@CacheConfig(cacheNames = "products")
public class ProductService {
    @Cacheable(key = "#id")
    public Product findById(Long id) { ... }
}

Self-Invocation Caveat

Cache annotations work via proxies, so a method calling another cached method in the same class bypasses the cache. Call through a separate bean.

Quick Check

Test your understanding of the cache annotations.

Recap

You learned the three cache annotations:

  • @Cacheable — store and reuse results
  • @CachePut — always run and refresh
  • @CacheEvict — remove entries (allEntries, beforeInvocation)
  • Watch out for the self-invocation proxy caveat

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

บทเรียน “@Cacheable, @CachePut, @CacheEvict” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “@Cacheable, @CachePut, @CacheEvict”

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

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

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

บทเรียน “@Cacheable, @CachePut, @CacheEvict” ใช้เวลานานแค่ไหน

บทเรียน 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