From 5f1cc6ddba2a390a64376617433b231440d7a791 Mon Sep 17 00:00:00 2001 From: timis1 <12120641+timis1@users.noreply.github.com> Date: Thu, 2 Feb 2023 20:08:39 +0200 Subject: [PATCH] JAVA-16681 GitHub Issue: CacheUtils shown in this tutorial are now Deprecated and marked for removal (#13397) Co-authored-by: timis1 --- .../baeldung/webflux/caching/ItemService.java | 18 ++++++++---------- .../caching/MonoFluxResultCachingLiveTest.java | 6 +++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/spring-5-webflux-2/src/main/java/com/baeldung/webflux/caching/ItemService.java b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/caching/ItemService.java index 7fc3732ba5..9bf934b504 100644 --- a/spring-5-webflux-2/src/main/java/com/baeldung/webflux/caching/ItemService.java +++ b/spring-5-webflux-2/src/main/java/com/baeldung/webflux/caching/ItemService.java @@ -1,22 +1,22 @@ package com.baeldung.webflux.caching; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.stereotype.Service; -import reactor.cache.CacheMono; + import reactor.core.publisher.Mono; @Service public class ItemService { private final ItemRepository repository; - private final LoadingCache cache; + private final LoadingCache> cache; public ItemService(ItemRepository repository) { this.repository = repository; - this.cache = Caffeine.newBuilder() - .build(this::getItem_withAddons); + this.cache = Caffeine.newBuilder().build(this::getItem_withCaffeine); } @Cacheable("items") @@ -34,9 +34,7 @@ public class ItemService { } @Cacheable("items") - public Mono getItem_withAddons(String id) { - return CacheMono.lookup(cache.asMap(), id) - .onCacheMissResume(() -> repository.findById(id).cast(Object.class)).cast(Item.class); + public Mono getItem_withCaffeine(String id) { + return cache.asMap().computeIfAbsent(id, k -> repository.findById(id).cast(Item.class)); } - } diff --git a/spring-5-webflux-2/src/test/java/com/baeldung/webflux/caching/MonoFluxResultCachingLiveTest.java b/spring-5-webflux-2/src/test/java/com/baeldung/webflux/caching/MonoFluxResultCachingLiveTest.java index 028a6d33a3..2075c1e77e 100644 --- a/spring-5-webflux-2/src/test/java/com/baeldung/webflux/caching/MonoFluxResultCachingLiveTest.java +++ b/spring-5-webflux-2/src/test/java/com/baeldung/webflux/caching/MonoFluxResultCachingLiveTest.java @@ -73,19 +73,19 @@ public void givenItem_whenGetItemIsCalled_thenMonoIsCached() { } @Test - public void givenItem_whenGetItemWithAddonsIsCalled_thenMonoResultIsCached() { + public void givenItem_whenGetItemWithCaffeineIsCalled_thenMonoResultIsCached() { Mono glass = itemService.save(new Item("glass", 1.00)); String id = glass.block().get_id(); - Mono mono = itemService.getItem_withAddons(id); + Mono mono = itemService.getItem_withCaffeine(id); Item item = mono.block(); assertThat(item).isNotNull(); assertThat(item.getName()).isEqualTo("glass"); assertThat(item.getPrice()).isEqualTo(1.00); - Mono mono2 = itemService.getItem_withAddons(id); + Mono mono2 = itemService.getItem_withCaffeine(id); Item item2 = mono2.block(); assertThat(item2).isNotNull();