Spring Boot 4 Microservices & REST APIs · درس

مدة TTL وإبطال التخزين المؤقت

أدر انتهاء الصلاحية والإخلاء

الدرس 4 من 413 خطوة

مدة TTL وإبطال التخزين المؤقت درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why TTL Matters

Cached data can go stale. A TTL (time-to-live) automatically expires entries after a set duration, bounding how stale data can get.

Setting a Default TTL

Apply a TTL to all caches via the default configuration.

RedisCacheConfiguration config =
    RedisCacheConfiguration.defaultCacheConfig()
        .entryTtl(Duration.ofMinutes(10));

Per-Cache TTL

Different data ages at different rates — give each cache its own TTL.

Map<String, RedisCacheConfiguration> configs = Map.of(
    "products",
        RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofHours(1)),
    "prices",
        RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMinutes(1)));

Caffeine TTL via Properties

For a local Caffeine cache, set expiry in properties.

spring.cache.caffeine.spec=expireAfterWrite=300s,maximumSize=1000

Explicit Invalidation

TTL handles time-based expiry; @CacheEvict handles event-based invalidation when data changes.

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

Write-Through Updates

Pair an update with @CachePut so the cache reflects the new value immediately instead of waiting for expiry.

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

Clearing a Whole Cache

After a bulk change, evict all entries to force fresh reads.

@CacheEvict(value = "products", allEntries = true)
public void importBatch(List<Product> items) {
    repository.saveAll(items);
}

Eviction Policies

When a cache hits its size limit, an eviction policy decides what to remove. Common policies: LRU (least recently used) and LFU (least frequently used).

Bounding Cache Size

Limit memory by capping the number of entries with Caffeine's maximumSize.

spring.cache.caffeine.spec=maximumSize=10000

Cache Stampede

When a popular key expires, many requests miss at once and all hit the database — a stampede. Mitigate with short staggered TTLs or refresh-ahead strategies.

Monitoring Hit Rate

Track the cache hit rate (hits ÷ total lookups). A low rate means the cache is not helping — revisit TTL, keys, or whether caching fits the access pattern.

Quick Check

Test your understanding of TTL and invalidation.

Recap

You learned cache expiry and invalidation:

  • TTL via entryTtl bounds staleness
  • Per-cache TTLs for different data
  • @CacheEvict / @CachePut for event-based invalidation
  • Eviction policies (LRU/LFU), size limits, and stampede awareness
البدء مجانًا

تعلم Java مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
24
الدروس
93

الأسئلة الشائعة

هل درس «مدة TTL وإبطال التخزين المؤقت» مجاني؟

نعم — نص درس «مدة TTL وإبطال التخزين المؤقت» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «مدة TTL وإبطال التخزين المؤقت»؟

أدر انتهاء الصلاحية والإخلاء تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «مدة TTL وإبطال التخزين المؤقت»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تجريد التخزين المؤقت في Spring
  2. @Cacheable و@CachePut و@CacheEvict
  3. Redis كمزوّد للتخزين المؤقت
  4. مدة TTL وإبطال التخزين المؤقت
← العودة إلى Spring Boot 4 Microservices & REST APIs