キャッシュのTTLと無効化
有効期限と削除を管理します
「キャッシュのTTLと無効化」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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=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
よくある質問
「キャッシュのTTLと無効化」レッスンは無料ですか?
はい。「キャッシュのTTLと無効化」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
「キャッシュのTTLと無効化」で何を学びますか?
有効期限と削除を管理します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「キャッシュのTTLと無効化」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。