Skip to content
Ameen Abozaid
Planned lessonMID-037-L05

Demo: a local Caffeine cache with metrics

  • Lesson 5 of 5
  • Mid-level
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

  1. Add the Caffeine dependency and build a Cache<Long, ProductDto> with maximumSize(1000), expireAfterWrite(5, MINUTES), and recordStats().
  2. Wrap findProduct(id): cache.get(id, this::loadFromDb).
  3. Expose cache.stats() through Micrometer (hit count, miss count, load time).
  4. Generate load with a small script; show the hit ratio rising after warm-up.
  5. 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.
  6. 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Type to search levels, phases, modules and lessons.