|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cache.caffeine; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.atomic.AtomicLong; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import reactor.core.publisher.Flux; |
| 25 | +import reactor.core.publisher.Mono; |
| 26 | + |
| 27 | +import org.springframework.cache.CacheManager; |
| 28 | +import org.springframework.cache.annotation.CacheConfig; |
| 29 | +import org.springframework.cache.annotation.Cacheable; |
| 30 | +import org.springframework.cache.annotation.EnableCaching; |
| 31 | +import org.springframework.context.ApplicationContext; |
| 32 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests for annotation-based caching methods that use reactive operators. |
| 40 | + * |
| 41 | + * @author Juergen Hoeller |
| 42 | + * @since 6.1 |
| 43 | + */ |
| 44 | +public class CaffeineReactiveCachingTests { |
| 45 | + |
| 46 | + @Test |
| 47 | + void withCaffeineAsyncCache() { |
| 48 | + ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, ReactiveCacheableService.class); |
| 49 | + ReactiveCacheableService service = ctx.getBean(ReactiveCacheableService.class); |
| 50 | + |
| 51 | + Object key = new Object(); |
| 52 | + |
| 53 | + Long r1 = service.cacheFuture(key).join(); |
| 54 | + Long r2 = service.cacheFuture(key).join(); |
| 55 | + Long r3 = service.cacheFuture(key).join(); |
| 56 | + |
| 57 | + assertThat(r1).isNotNull(); |
| 58 | + assertThat(r1).isSameAs(r2).isSameAs(r3); |
| 59 | + |
| 60 | + key = new Object(); |
| 61 | + |
| 62 | + r1 = service.cacheMono(key).block(); |
| 63 | + r2 = service.cacheMono(key).block(); |
| 64 | + r3 = service.cacheMono(key).block(); |
| 65 | + |
| 66 | + assertThat(r1).isNotNull(); |
| 67 | + assertThat(r1).isSameAs(r2).isSameAs(r3); |
| 68 | + |
| 69 | + key = new Object(); |
| 70 | + |
| 71 | + r1 = service.cacheFlux(key).blockFirst(); |
| 72 | + r2 = service.cacheFlux(key).blockFirst(); |
| 73 | + r3 = service.cacheFlux(key).blockFirst(); |
| 74 | + |
| 75 | + assertThat(r1).isNotNull(); |
| 76 | + assertThat(r1).isSameAs(r2).isSameAs(r3); |
| 77 | + |
| 78 | + key = new Object(); |
| 79 | + |
| 80 | + List<Long> l1 = service.cacheFlux(key).collectList().block(); |
| 81 | + List<Long> l2 = service.cacheFlux(key).collectList().block(); |
| 82 | + List<Long> l3 = service.cacheFlux(key).collectList().block(); |
| 83 | + |
| 84 | + assertThat(l1).isNotNull(); |
| 85 | + assertThat(l1).isEqualTo(l2).isEqualTo(l3); |
| 86 | + |
| 87 | + key = new Object(); |
| 88 | + |
| 89 | + r1 = service.cacheMono(key).block(); |
| 90 | + r2 = service.cacheMono(key).block(); |
| 91 | + r3 = service.cacheMono(key).block(); |
| 92 | + |
| 93 | + assertThat(r1).isNotNull(); |
| 94 | + assertThat(r1).isSameAs(r2).isSameAs(r3); |
| 95 | + |
| 96 | + // Same key as for Mono, reusing its cached value |
| 97 | + |
| 98 | + r1 = service.cacheFlux(key).blockFirst(); |
| 99 | + r2 = service.cacheFlux(key).blockFirst(); |
| 100 | + r3 = service.cacheFlux(key).blockFirst(); |
| 101 | + |
| 102 | + assertThat(r1).isNotNull(); |
| 103 | + assertThat(r1).isSameAs(r2).isSameAs(r3); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + @CacheConfig(cacheNames = "first") |
| 108 | + static class ReactiveCacheableService { |
| 109 | + |
| 110 | + private final AtomicLong counter = new AtomicLong(); |
| 111 | + |
| 112 | + @Cacheable |
| 113 | + CompletableFuture<Long> cacheFuture(Object arg) { |
| 114 | + return CompletableFuture.completedFuture(this.counter.getAndIncrement()); |
| 115 | + } |
| 116 | + |
| 117 | + @Cacheable |
| 118 | + Mono<Long> cacheMono(Object arg) { |
| 119 | + return Mono.just(this.counter.getAndIncrement()); |
| 120 | + } |
| 121 | + |
| 122 | + @Cacheable |
| 123 | + Flux<Long> cacheFlux(Object arg) { |
| 124 | + return Flux.just(this.counter.getAndIncrement(), 0L); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + @Configuration(proxyBeanMethods = false) |
| 130 | + @EnableCaching |
| 131 | + static class Config { |
| 132 | + |
| 133 | + @Bean |
| 134 | + CacheManager cacheManager() { |
| 135 | + CaffeineCacheManager ccm = new CaffeineCacheManager("first"); |
| 136 | + ccm.setAsyncCacheMode(true); |
| 137 | + return ccm; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments