@@ -616,7 +616,7 @@ private void performCacheEvicts(List<CacheOperationContext> contexts, @Nullable
616
616
for (CacheOperationContext context : contexts ) {
617
617
CacheEvictOperation operation = (CacheEvictOperation ) context .metadata .operation ;
618
618
if (isConditionPassing (context , result )) {
619
- Object key = null ;
619
+ Object key = context . getGeneratedKey () ;
620
620
for (Cache cache : context .getCaches ()) {
621
621
if (operation .isCacheWide ()) {
622
622
logInvalidating (context , operation , null );
@@ -673,7 +673,7 @@ private Object generateKey(CacheOperationContext context, @Nullable Object resul
673
673
throw new IllegalArgumentException ("""
674
674
Null key returned for cache operation [%s]. If you are using named parameters, \
675
675
ensure that the compiler uses the '-parameters' flag."""
676
- .formatted (context .metadata .operation ));
676
+ .formatted (context .metadata .operation ));
677
677
}
678
678
if (logger .isTraceEnabled ()) {
679
679
logger .trace ("Computed cache key '" + key + "' for operation " + context .metadata .operation );
@@ -798,6 +798,9 @@ protected class CacheOperationContext implements CacheOperationInvocationContext
798
798
@ Nullable
799
799
private Boolean conditionPassing ;
800
800
801
+ @ Nullable
802
+ private Object key ;
803
+
801
804
public CacheOperationContext (CacheOperationMetadata metadata , Object [] args , Object target ) {
802
805
this .metadata = metadata ;
803
806
this .args = extractArgs (metadata .method , args );
@@ -873,9 +876,17 @@ else if (this.metadata.operation instanceof CachePutOperation cachePutOperation)
873
876
protected Object generateKey (@ Nullable Object result ) {
874
877
if (StringUtils .hasText (this .metadata .operation .getKey ())) {
875
878
EvaluationContext evaluationContext = createEvaluationContext (result );
876
- return evaluator .key (this .metadata .operation .getKey (), this .metadata .methodKey , evaluationContext );
879
+ this .key = evaluator .key (this .metadata .operation .getKey (), this .metadata .methodKey , evaluationContext );
880
+ }
881
+ else {
882
+ this .key = this .metadata .keyGenerator .generate (this .target , this .metadata .method , this .args );
877
883
}
878
- return this .metadata .keyGenerator .generate (this .target , this .metadata .method , this .args );
884
+ return this .key ;
885
+ }
886
+
887
+ @ Nullable
888
+ protected Object getGeneratedKey () {
889
+ return this .key ;
879
890
}
880
891
881
892
private EvaluationContext createEvaluationContext (@ Nullable Object result ) {
@@ -969,7 +980,10 @@ public Object apply(@Nullable Object result) {
969
980
970
981
public void performCachePut (@ Nullable Object value ) {
971
982
if (this .context .canPutToCache (value )) {
972
- Object key = generateKey (this .context , value );
983
+ Object key = this .context .getGeneratedKey ();
984
+ if (key == null ) {
985
+ key = generateKey (this .context , value );
986
+ }
973
987
if (logger .isTraceEnabled ()) {
974
988
logger .trace ("Creating cache entry for key '" + key + "' in cache(s) " +
975
989
this .context .getCacheNames ());
0 commit comments