@Cacheable, @CachePut, @CacheEvict
Kendalikan perilaku cache secara tepat.
@Cacheable, @CachePut, @CacheEvict adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Boot 4 Microservices & REST APIs, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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
Pertanyaan yang Sering Diajukan
Apakah pelajaran “@Cacheable, @CachePut, @CacheEvict” gratis?
Ya — teks lengkap “@Cacheable, @CachePut, @CacheEvict” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Boot 4 Microservices & REST APIs, upgrade ke CoddyKit PRO. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “@Cacheable, @CachePut, @CacheEvict”?
Kendalikan perilaku cache secara tepat. Kamu berlatih Spring Boot 4 Microservices & REST APIs dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Spring Boot 4 Microservices & REST APIs?
Tidak diperlukan pengalaman sebelumnya. Spring Boot 4 Microservices & REST APIs di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.
Berapa lama pelajaran “@Cacheable, @CachePut, @CacheEvict” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Boot 4 Microservices & REST APIs ini?
Ya. Setiap pelajaran Spring Boot 4 Microservices & REST APIs menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Abstraksi Cache Spring
- @Cacheable, @CachePut, @CacheEvict
- Redis sebagai Penyedia Cache
- TTL dan Invalidasi Cache