Skip to content

Commit

Permalink
Introduce MonoFromFutureAsyncLoadingCacheGetAll Refaster rule (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
werli authored Feb 18, 2025
1 parent 2419dad commit c2a26ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,22 @@ Mono<V> after(AsyncLoadingCache<K, V> cache, K key) {
}
}

/**
* Don't propagate {@link Mono} cancellations to upstream cache value computations, as completion
* of such computations may benefit concurrent or subsequent cache usages.
*/
static final class MonoFromFutureAsyncLoadingCacheGetAll<K1, K2 extends K1, V> {
@BeforeTemplate
Mono<Map<K1, V>> before(AsyncLoadingCache<K1, V> cache, Iterable<K2> keys) {
return Mono.fromFuture(() -> cache.getAll(keys));
}

@AfterTemplate
Mono<Map<K1, V>> after(AsyncLoadingCache<K1, V> cache, Iterable<K2> keys) {
return Mono.fromFuture(() -> cache.getAll(keys), /* suppressCancel= */ true);
}
}

/**
* Prefer {@link Flux#fromStream(Supplier)} over {@link Flux#fromStream(Stream)}, as the former
* yields a {@link Flux} that is more likely to behave as expected when subscribed to more than
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -745,6 +746,11 @@ Mono<String> testMonoFromFutureAsyncLoadingCacheGet() {
return Mono.fromFuture(() -> ((AsyncLoadingCache<Integer, String>) null).get(0));
}

Mono<Map<Integer, String>> testMonoFromFutureAsyncLoadingCacheGetAll() {
return Mono.fromFuture(
() -> ((AsyncLoadingCache<Integer, String>) null).getAll(ImmutableSet.of()));
}

Flux<Integer> testFluxFromStreamSupplier() {
return Flux.fromStream(Stream.of(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -719,6 +720,11 @@ Mono<String> testMonoFromFutureAsyncLoadingCacheGet() {
return Mono.fromFuture(() -> ((AsyncLoadingCache<Integer, String>) null).get(0), true);
}

Mono<Map<Integer, String>> testMonoFromFutureAsyncLoadingCacheGetAll() {
return Mono.fromFuture(
() -> ((AsyncLoadingCache<Integer, String>) null).getAll(ImmutableSet.of()), true);
}

Flux<Integer> testFluxFromStreamSupplier() {
return Flux.fromStream(() -> Stream.of(1));
}
Expand Down

0 comments on commit c2a26ed

Please sign in to comment.