@Cacheable, @CachePut, @CacheEvict
Steuern Sie das Cache-Verhalten präzise.
@Cacheable, @CachePut, @CacheEvict ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 2 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.
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
Häufig gestellte Fragen
Ist die Lektion „@Cacheable, @CachePut, @CacheEvict“ kostenlos?
Ja — der vollständige Text von „@Cacheable, @CachePut, @CacheEvict“ 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 „@Cacheable, @CachePut, @CacheEvict“?
Steuern Sie das Cache-Verhalten präzise. 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 2 von 4.
Wie lange dauert die Lektion „@Cacheable, @CachePut, @CacheEvict“?
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
- Die Spring-Cache-Abstraktion
- @Cacheable, @CachePut, @CacheEvict
- Redis als Cache-Provider
- Cache-TTL und Invalidierung