TTL e invalidazione della cache
Gestisca scadenza ed eliminazione.
TTL e invalidazione della cache è una lezione Spring Boot 4 Microservices & REST APIs gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Boot 4 Microservices & REST APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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=1000Explicit 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=10000Cache 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
entryTtlbounds staleness - Per-cache TTLs for different data
@CacheEvict/@CachePutfor event-based invalidation- Eviction policies (LRU/LFU), size limits, and stampede awareness
Impara Java con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 24
- Lezioni
- 93
Domande Frequenti
La lezione «TTL e invalidazione della cache» è gratuita?
Sì — il testo completo di «TTL e invalidazione della cache» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Boot 4 Microservices & REST APIs, passa a CoddyKit PRO. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Cosa imparerò in «TTL e invalidazione della cache»?
Gestisca scadenza ed eliminazione. Eserciti Spring Boot 4 Microservices & REST APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Spring Boot 4 Microservices & REST APIs?
Non è richiesta alcuna esperienza precedente. Spring Boot 4 Microservices & REST APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «TTL e invalidazione della cache»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Spring Boot 4 Microservices & REST APIs?
Sì. Ogni lezione Spring Boot 4 Microservices & REST APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- L'astrazione della cache di Spring
- @Cacheable, @CachePut, @CacheEvict
- Redis come provider della cache
- TTL e invalidazione della cache