From 624d6dd167dc55a1758864d60c74b85cfeef7c54 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 6 Jun 2024 20:43:04 +0200 Subject: [PATCH] Expose actual result value for @CacheEvict condition Closes gh-32960 --- .../cache/interceptor/CacheAspectSupport.java | 4 ++-- .../cache/CacheReproTests.java | 20 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index 75f2293e7e7..6384591a0e3 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -630,7 +630,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker if (result instanceof CompletableFuture future) { return future.whenComplete((value, ex) -> { if (ex == null) { - performCacheEvicts(applicable, result); + performCacheEvicts(applicable, value); } }); } @@ -1112,7 +1112,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker ReactiveAdapter adapter = (result != null ? this.registry.getAdapter(result.getClass()) : null); if (adapter != null) { return adapter.fromPublisher(Mono.from(adapter.toPublisher(result)) - .doOnSuccess(value -> performCacheEvicts(contexts, result))); + .doOnSuccess(value -> performCacheEvicts(contexts, value))); } return NOT_HANDLED; } diff --git a/spring-context/src/test/java/org/springframework/cache/CacheReproTests.java b/spring-context/src/test/java/org/springframework/cache/CacheReproTests.java index c67102f7da8..9d358b81cdc 100644 --- a/spring-context/src/test/java/org/springframework/cache/CacheReproTests.java +++ b/spring-context/src/test/java/org/springframework/cache/CacheReproTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -606,9 +606,9 @@ class CacheReproTests { return CompletableFuture.completedFuture(item); } - @CacheEvict(cacheNames = "itemCache", allEntries = true) - public CompletableFuture clear() { - return CompletableFuture.completedFuture(null); + @CacheEvict(cacheNames = "itemCache", allEntries = true, condition = "#result > 0") + public CompletableFuture clear() { + return CompletableFuture.completedFuture(1); } } @@ -655,9 +655,9 @@ class CacheReproTests { return Mono.just(item); } - @CacheEvict(cacheNames = "itemCache", allEntries = true) - public Mono clear() { - return Mono.empty(); + @CacheEvict(cacheNames = "itemCache", allEntries = true, condition = "#result > 0") + public Mono clear() { + return Mono.just(1); } } @@ -706,9 +706,9 @@ class CacheReproTests { return Flux.fromIterable(item); } - @CacheEvict(cacheNames = "itemCache", allEntries = true) - public Flux clear() { - return Flux.empty(); + @CacheEvict(cacheNames = "itemCache", allEntries = true, condition = "#result > 0") + public Flux clear() { + return Flux.just(1); } }