From 70608bb2e729a793aa24f74d9d137653b01cff8a Mon Sep 17 00:00:00 2001 From: davidmartinezbarua Date: Fri, 26 Nov 2021 10:56:17 -0300 Subject: [PATCH] Revert "Spring Webflux and @Cacheable Annotation" --- spring-5-webflux/pom.xml | 20 ---- .../com/baeldung/spring/caching/Item.java | 50 ---------- .../spring/caching/ItemRepository.java | 8 -- .../baeldung/spring/caching/ItemService.java | 42 -------- .../SpringWebfluxCachingApplication.java | 16 ---- .../resources/application-cache.properties | 2 - .../MonoFluxResultCachingLiveTest.java | 95 ------------------- 7 files changed, 233 deletions(-) delete mode 100644 spring-5-webflux/src/main/java/com/baeldung/spring/caching/Item.java delete mode 100644 spring-5-webflux/src/main/java/com/baeldung/spring/caching/ItemRepository.java delete mode 100644 spring-5-webflux/src/main/java/com/baeldung/spring/caching/ItemService.java delete mode 100644 spring-5-webflux/src/main/java/com/baeldung/spring/caching/SpringWebfluxCachingApplication.java delete mode 100644 spring-5-webflux/src/main/resources/application-cache.properties delete mode 100644 spring-5-webflux/src/test/java/com/baeldung/spring/caching/MonoFluxResultCachingLiveTest.java diff --git a/spring-5-webflux/pom.xml b/spring-5-webflux/pom.xml index d6afb686fc..69de83c227 100644 --- a/spring-5-webflux/pom.xml +++ b/spring-5-webflux/pom.xml @@ -66,31 +66,11 @@ - - io.projectreactor.addons - reactor-extra - 3.4.5 - - - com.github.ben-manes.caffeine - caffeine - 2.9.2 - - - org.springframework.boot - spring-boot-starter-data-mongodb-reactive - io.projectreactor reactor-test test - - org.testcontainers - mongodb - 1.16.2 - test - com.squareup.okhttp3 mockwebserver diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/caching/Item.java b/spring-5-webflux/src/main/java/com/baeldung/spring/caching/Item.java deleted file mode 100644 index 7b79ff7503..0000000000 --- a/spring-5-webflux/src/main/java/com/baeldung/spring/caching/Item.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.spring.caching; - -import org.springframework.data.annotation.Id; -import org.springframework.data.mongodb.core.mapping.Document; - -@Document -public class Item { - - @Id - private String _id; - private String name; - private double price; - - public Item(String name, double price) { - this.name = name; - this.price = price; - } - - public Item() { - } - - public String get_id() { - return _id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public double getPrice() { - return price; - } - - public void setPrice(double price) { - this.price = price; - } - - @Override - public String toString() { - return "Item{" + - "id='" + _id + '\'' + - ", name='" + name + '\'' + - ", price=" + price + - '}'; - } -} diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/caching/ItemRepository.java b/spring-5-webflux/src/main/java/com/baeldung/spring/caching/ItemRepository.java deleted file mode 100644 index 27c97de36a..0000000000 --- a/spring-5-webflux/src/main/java/com/baeldung/spring/caching/ItemRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.baeldung.spring.caching; - -import org.springframework.data.mongodb.repository.ReactiveMongoRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface ItemRepository extends ReactiveMongoRepository { -} diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/caching/ItemService.java b/spring-5-webflux/src/main/java/com/baeldung/spring/caching/ItemService.java deleted file mode 100644 index b24b54521e..0000000000 --- a/spring-5-webflux/src/main/java/com/baeldung/spring/caching/ItemService.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.spring.caching; - -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; - - public ItemService(ItemRepository repository) { - this.repository = repository; - this.cache = Caffeine.newBuilder() - .build(this::getItem_withAddons); - } - - @Cacheable("items") - public Mono getItem(String id) { - return repository.findById(id); - } - - public Mono save(Item item) { - return repository.save(item); - } - - @Cacheable("items") - public Mono getItem_withCache(String id) { - return repository.findById(id).cache(); - } - - @Cacheable("items") - public Mono getItem_withAddons(String id) { - return CacheMono.lookup(cache.asMap(), id) - .onCacheMissResume(() -> repository.findById(id).cast(Object.class)).cast(Item.class); - } - -} diff --git a/spring-5-webflux/src/main/java/com/baeldung/spring/caching/SpringWebfluxCachingApplication.java b/spring-5-webflux/src/main/java/com/baeldung/spring/caching/SpringWebfluxCachingApplication.java deleted file mode 100644 index 5266e33775..0000000000 --- a/spring-5-webflux/src/main/java/com/baeldung/spring/caching/SpringWebfluxCachingApplication.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.spring.caching; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; - -@SpringBootApplication -@EnableMongoRepositories -@EnableCaching -public class SpringWebfluxCachingApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringWebfluxCachingApplication.class, args); - } -} diff --git a/spring-5-webflux/src/main/resources/application-cache.properties b/spring-5-webflux/src/main/resources/application-cache.properties deleted file mode 100644 index 23414da2dd..0000000000 --- a/spring-5-webflux/src/main/resources/application-cache.properties +++ /dev/null @@ -1,2 +0,0 @@ -logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG -logging.level.org.springframework.cache=TRACE \ No newline at end of file diff --git a/spring-5-webflux/src/test/java/com/baeldung/spring/caching/MonoFluxResultCachingLiveTest.java b/spring-5-webflux/src/test/java/com/baeldung/spring/caching/MonoFluxResultCachingLiveTest.java deleted file mode 100644 index 322b3c5aa5..0000000000 --- a/spring-5-webflux/src/test/java/com/baeldung/spring/caching/MonoFluxResultCachingLiveTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.baeldung.spring.caching; - - -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; -import org.testcontainers.containers.MongoDBContainer; -import org.testcontainers.utility.DockerImageName; -import reactor.core.publisher.Mono; - -import static org.assertj.core.api.Assertions.assertThat; - -@SpringBootTest -@ActiveProfiles("cache") -public class MonoFluxResultCachingLiveTest { - - - @Autowired - ItemService itemService; - - final static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")); - - @DynamicPropertySource - static void mongoDbProperties(DynamicPropertyRegistry registry) { - mongoDBContainer.start(); - registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl); - } - -@Test -public void givenItem_whenGetItemIsCalled_thenMonoIsCached() { - Mono glass = itemService.save(new Item("glass", 1.00)); - - String id = glass.block().get_id(); - - Mono mono = itemService.getItem(id); - Item item = mono.block(); - - assertThat(item).isNotNull(); - assertThat(item.getName()).isEqualTo("glass"); - assertThat(item.getPrice()).isEqualTo(1.00); - - Mono mono2 = itemService.getItem(id); - Item item2 = mono2.block(); - - assertThat(item2).isNotNull(); - assertThat(item2.getName()).isEqualTo("glass"); - assertThat(item2.getPrice()).isEqualTo(1.00); -} - - @Test - public void givenItem_whenGetItemWithCacheIsCalled_thenMonoResultIsCached() { - Mono glass = itemService.save(new Item("glass", 1.00)); - - String id = glass.block().get_id(); - - Mono mono = itemService.getItem_withCache(id); - Item item = mono.block(); - - assertThat(item).isNotNull(); - assertThat(item.getName()).isEqualTo("glass"); - assertThat(item.getPrice()).isEqualTo(1.00); - - Mono mono2 = itemService.getItem_withCache(id); - Item item2 = mono2.block(); - - assertThat(item2).isNotNull(); - assertThat(item2.getName()).isEqualTo("glass"); - assertThat(item2.getPrice()).isEqualTo(1.00); - } - - @Test - public void givenItem_whenGetItemWithAddonsIsCalled_thenMonoResultIsCached() { - Mono glass = itemService.save(new Item("glass", 1.00)); - - String id = glass.block().get_id(); - - Mono mono = itemService.getItem_withAddons(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); - Item item2 = mono2.block(); - - assertThat(item2).isNotNull(); - assertThat(item2.getName()).isEqualTo("glass"); - assertThat(item2.getPrice()).isEqualTo(1.00); - } - -}