0Pricing
Spring Boot 4 Microservices & REST APIs · Lektion

Cache-TTL und Invalidierung

Verwalten Sie Ablauf und Entfernung von Cache-Einträgen.

Cache-TTL und Invalidierung ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Microservices & REST APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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

Häufig gestellte Fragen

Ist die Lektion „Cache-TTL und Invalidierung“ kostenlos?

Ja — der vollständige Text von „Cache-TTL und Invalidierung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Microservices & REST APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Cache-TTL und Invalidierung“?

Verwalten Sie Ablauf und Entfernung von Cache-Einträgen. Du übst Spring Boot 4 Microservices & REST APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Spring Boot 4 Microservices & REST APIs zu starten?

Keine Vorkenntnisse erforderlich. Spring Boot 4 Microservices & REST APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Cache-TTL und Invalidierung“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Spring Boot 4 Microservices & REST APIs-Lektion Code schreiben und ausführen?

Ja. Jede Spring Boot 4 Microservices & REST APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Die Spring-Cache-Abstraktion
  2. @Cacheable, @CachePut, @CacheEvict
  3. Redis als Cache-Provider
  4. Cache-TTL und Invalidierung
← Zurück zu Spring Boot 4 Microservices & REST APIs