0
0
Spring Bootframework~30 mins

@CachePut for updating cache in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Using @CachePut to Update Cache in Spring Boot
📖 Scenario: You are building a simple Spring Boot service that manages product prices. You want to cache product prices to improve performance. When a product price is updated, you want to update the cache immediately to keep it fresh.
🎯 Goal: Build a Spring Boot service with a method that updates a product price and uses @CachePut to update the cache with the new price.
📋 What You'll Learn
Create a Map<String, Double> called productPrices with initial product prices
Create a String variable called cacheName with value "prices"
Create a method updatePrice that takes String product and Double price and updates productPrices
Annotate updatePrice with @CachePut to update the cache named prices
💡 Why This Matters
🌍 Real World
Caching updated data in services is common to improve performance and keep data fresh in web applications.
💼 Career
Understanding @CachePut is important for backend developers working with Spring Boot to manage cache updates efficiently.
Progress0 / 4 steps
1
Create initial product prices map
Create a Map<String, Double> called productPrices with these exact entries: "apple" with 1.0, "banana" with 0.5, and "orange" with 0.8.
Spring Boot
Need a hint?

Use new HashMap<>() and add entries with put.

2
Add cache name variable
Add a String variable called cacheName and set it to the exact value "prices".
Spring Boot
Need a hint?

Declare a String variable and assign the value "prices".

3
Create updatePrice method with cache update
Create a public method updatePrice that takes String product and Double price. Inside, update productPrices by putting the new price for the product. Annotate this method with @CachePut(cacheNames = "prices", key = "#product") to update the cache entry for the product.
Spring Boot
Need a hint?

Use @CachePut with cacheNames = "prices" and key = "#product". Return the updated price.

4
Add class-level annotation and complete service
Add the @Service annotation to the PriceService class to make it a Spring service component.
Spring Boot
Need a hint?

Import and add @Service annotation above the class declaration.