Demo: a local Caffeine cache with metrics
On This Lesson 3 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
add a Caffeine cache to a Shop endpoint and read its stats. Prepares MID-037-LAB1.
Draft outline details
Step by step
- Add the Caffeine dependency and build a
Cache<Long, ProductDto>withmaximumSize(1000),expireAfterWrite(5, MINUTES), andrecordStats(). - Wrap
findProduct(id):cache.get(id, this::loadFromDb). - Expose
cache.stats()through Micrometer (hit count, miss count, load time). - Generate load with a small script; show the hit ratio rising after warm-up.
- Planned mistake: cache the per-customer discounted price by product id only; a second customer sees the first customer's price. Fix: cache the shared base price and apply the discount afterwards, or include the customer tier in the key.
- Verify Caffeine version and Micrometer binding names before recording.
Check your understanding
Why is recordStats() needed?
Show answer
Without it the cache reports no hit or miss counts.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.