@@ -652,9 +652,8 @@ private void collectPutRequests(Collection<CacheOperationContext> contexts,
652
652
@ Nullable Object result , Collection <CachePutRequest > putRequests ) {
653
653
654
654
for (CacheOperationContext context : contexts ) {
655
- if (isConditionPassing (context , result ) && context .canPutToCache (result )) {
656
- Object key = generateKey (context , result );
657
- putRequests .add (new CachePutRequest (context , key ));
655
+ if (isConditionPassing (context , result )) {
656
+ putRequests .add (new CachePutRequest (context ));
658
657
}
659
658
}
660
659
}
@@ -943,22 +942,16 @@ private class CachePutRequest {
943
942
944
943
private final CacheOperationContext context ;
945
944
946
- private final Object key ;
947
-
948
- public CachePutRequest (CacheOperationContext context , Object key ) {
945
+ public CachePutRequest (CacheOperationContext context ) {
949
946
this .context = context ;
950
- this .key = key ;
951
947
}
952
948
953
949
@ Nullable
954
950
public Object apply (@ Nullable Object result ) {
955
951
if (result instanceof CompletableFuture <?> future ) {
956
952
return future .whenComplete ((value , ex ) -> {
957
- if (ex != null ) {
958
- performEvict (ex );
959
- }
960
- else {
961
- performPut (value );
953
+ if (ex == null ) {
954
+ performCachePut (value );
962
955
}
963
956
});
964
957
}
@@ -968,27 +961,20 @@ public Object apply(@Nullable Object result) {
968
961
return returnValue ;
969
962
}
970
963
}
971
- performPut (result );
964
+ performCachePut (result );
972
965
return null ;
973
966
}
974
967
975
- void performPut (@ Nullable Object value ) {
976
- if (logger .isTraceEnabled ()) {
977
- logger .trace ("Creating cache entry for key '" + this .key + "' in cache(s) " +
978
- this .context .getCacheNames ());
979
- }
980
- for (Cache cache : this .context .getCaches ()) {
981
- doPut (cache , this .key , value );
982
- }
983
- }
984
-
985
- void performEvict (Throwable cause ) {
986
- if (logger .isTraceEnabled ()) {
987
- logger .trace ("Removing cache entry for key '" + this .key + "' from cache(s) " +
988
- this .context .getCacheNames () + " due to exception: " + cause );
989
- }
990
- for (Cache cache : this .context .getCaches ()) {
991
- doEvict (cache , this .key , false );
968
+ public void performCachePut (@ Nullable Object value ) {
969
+ if (this .context .canPutToCache (value )) {
970
+ Object key = generateKey (this .context , value );
971
+ if (logger .isTraceEnabled ()) {
972
+ logger .trace ("Creating cache entry for key '" + key + "' in cache(s) " +
973
+ this .context .getCacheNames ());
974
+ }
975
+ for (Cache cache : this .context .getCaches ()) {
976
+ doPut (cache , key , value );
977
+ }
992
978
}
993
979
}
994
980
}
@@ -1017,11 +1003,10 @@ public void onNext(Object o) {
1017
1003
}
1018
1004
@ Override
1019
1005
public void onError (Throwable t ) {
1020
- this .request .performEvict (t );
1021
1006
}
1022
1007
@ Override
1023
1008
public void onComplete () {
1024
- this .request .performPut (this .cacheValue );
1009
+ this .request .performCachePut (this .cacheValue );
1025
1010
}
1026
1011
}
1027
1012
@@ -1107,7 +1092,7 @@ public Object processPutRequest(CachePutRequest request, @Nullable Object result
1107
1092
}
1108
1093
else {
1109
1094
return adapter .fromPublisher (Mono .from (adapter .toPublisher (result ))
1110
- .doOnSuccess (request ::performPut ). doOnError ( request :: performEvict ));
1095
+ .doOnSuccess (request ::performCachePut ));
1111
1096
}
1112
1097
}
1113
1098
return NOT_HANDLED ;
0 commit comments