Spring Boot services · Chapter 32 of 41

Caching

By the end of this chapter you can keep a method's answers in a cache, keep that cache right when the data changes, and explain why a call from inside the same class never reaches it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

At the 6 am rush every counter, phone and menu board asks Tapri Cloud for the menu, hundreds of times a minute, and the menu changes twice a week. Ravi forwards the database bill: "Why are we paying to read the same four prices all day?" This chapter's MenuRepository counts its reads; after three calls to GET /menu:

$ curl localhost:8081/menu/reads
3

With what you know so far, you would keep a map in MenuService yourself, look in it before every read, and clear it in every method that changes a price. Forget one and the board shows yesterday's price all week.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    MenuService.java: switch caching on and cache the whole menu

    Switch Spring's caching on for the app (on TapriApplication), and cache MenuService.menu() in a cache called "menu": three calls to menu() each return the four items and read the repository once in all.

    The app must then have a CacheManager, and it must have a cache named menu.

  2. Task 2

    MenuService.java: cache single items under their lower-case name

    Cache MenuService.item(name) in a cache called "items", keyed by the name in lower case: item("Masala chai"), item("masala chai") and item("MASALA CHAI") all answer price 40 with one repository read, and the entry is stored under the key "masala chai".

    A different item, item("Bun maska"), is a different entry and costs a second read. Needs task 1.

  3. Task 3

    MenuService.java: throw the cached menu away when a price changes

    Write MenuService.changePrice(name, price) so it changes the price through the repository's updatePrice and returns the updated item: changePrice("Cutting chai", 30) returns Cutting chai at 30.

    A price change must clear the cached menu: the next menu() reads the repository again and contains Cutting chai at 30, and the call after that is answered from the cache again. Needs task 1.

  4. Task 4

    MenuService.java: put the item with its new price into the cache

    Make changePrice also store the item it returns in the "items" cache, under the same key item() uses (the name in lower case): with item("Masala chai") cached, changePrice("Masala chai", 45) then item("masala chai") answers 45 with no new repository read.

    Keep the menu eviction from task 3. Needs tasks 2 and 3.

  5. Task 5 · interview question

    Interview: make the menu board read the menu through the cache

    Write MenuBoard.cheapest() so it returns the cheapest item on the menu (Parle-G at 10 on the opening menu), taken from MenuService's cached menu, never from the repository: two calls to cheapest() and one to menu() read the repository once in all. After changePrice("Cutting chai", 5), cheapest() is Cutting chai. Needs tasks 1 and 3.

    Be ready to explain why a call to a cached method from inside the same class skips the cache.

What you will learn

  • @Cacheable: the first call runs the method and stores the answer; later calls with the same key get it back.
  • Keys: which arguments make up an entry's key.
  • @CacheEvict and @CachePut: throw entries away, or replace them, when the data changes.
  • The proxy: why a call from inside the same class skips the cache (the interview question).

More in Production

Build "Caching" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.