Allow @CachePut 'key' SpEL to refer to #result

Allow the @CachePut 'key' SpEL to refer to the result of the method call
via a '#result' variable.

This change is particularly useful when working with JPA entities that
have generated @Id values since the ID will often not be available
until the entity has been saved.

Issue: SPR-10664
This commit is contained in:
Phillip Webb 2013-06-23 08:51:51 -07:00
parent f75d4e13a2
commit eea230f2a8
11 changed files with 188 additions and 15 deletions

View File

@ -163,4 +163,11 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
public Object multiUpdate(Object arg1) { public Object multiUpdate(Object arg1) {
return arg1; return arg1;
} }
@Override
@CachePut(value="primary", key="#result.id")
public TestEntity putRefersToResult(TestEntity arg1) {
arg1.setId(Long.MIN_VALUE);
return arg1;
}
} }

View File

@ -70,4 +70,6 @@ public interface CacheableService<T> {
T multiConditionalCacheAndEvict(Object arg1); T multiConditionalCacheAndEvict(Object arg1);
T multiUpdate(Object arg1); T multiUpdate(Object arg1);
TestEntity putRefersToResult(TestEntity arg1);
} }

View File

@ -171,4 +171,11 @@ public class DefaultCacheableService implements CacheableService<Long> {
public Long multiUpdate(Object arg1) { public Long multiUpdate(Object arg1) {
return Long.valueOf(arg1.toString()); return Long.valueOf(arg1.toString());
} }
@Override
@CachePut(value="primary", key="#result.id")
public TestEntity putRefersToResult(TestEntity arg1) {
arg1.setId(Long.MIN_VALUE);
return arg1;
}
} }

View File

@ -0,0 +1,56 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.config;
import org.springframework.util.ObjectUtils;
/**
* Simple test entity for use with caching tests.
*
* @author Michael Plšd
*/
public class TestEntity {
private Long id;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.id);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null) {
return false;
}
if (obj instanceof TestEntity) {
return ObjectUtils.nullSafeEquals(this.id, ((TestEntity) obj).id);
}
return false;
}
}

View File

@ -148,15 +148,15 @@ public abstract class CacheAspectSupport implements InitializingBean {
// Process any early evictions // Process any early evictions
processCacheEvicts(contexts.get(CacheEvictOperation.class), true, ExpressionEvaluator.NO_RESULT); processCacheEvicts(contexts.get(CacheEvictOperation.class), true, ExpressionEvaluator.NO_RESULT);
// Collect puts, either explicit @CachePuts or from a @Cachable miss // Collect puts from any @Cachable miss
List<CachePutRequest> cachePutRequests = new ArrayList<CachePutRequest>(); List<CachePutRequest> cachePutRequests = new ArrayList<CachePutRequest>();
collectPutRequests(contexts.get(CachePutOperation.class), cachePutRequests, false); collectPutRequests(contexts.get(CacheableOperation.class),
collectPutRequests(contexts.get(CacheableOperation.class), cachePutRequests, true); ExpressionEvaluator.NO_RESULT, cachePutRequests, true);
ValueWrapper result = null; ValueWrapper result = null;
// We only attempt to get a cached result if there are no put requests // We only attempt to get a cached result if there are no put requests
if(cachePutRequests.isEmpty()) { if(cachePutRequests.isEmpty() && contexts.get(CachePutOperation.class).isEmpty()) {
result = findCachedResult(contexts.get(CacheableOperation.class)); result = findCachedResult(contexts.get(CacheableOperation.class));
} }
@ -165,6 +165,10 @@ public abstract class CacheAspectSupport implements InitializingBean {
result = new SimpleValueWrapper(invoker.invoke()); result = new SimpleValueWrapper(invoker.invoke());
} }
// Collect any explicit @CachePuts
collectPutRequests(contexts.get(CachePutOperation.class), result.get(),
cachePutRequests, false);
// Process any collected put requests, either from @CachePut or a @Cacheable miss // Process any collected put requests, either from @CachePut or a @Cacheable miss
for (CachePutRequest cachePutRequest : cachePutRequests) { for (CachePutRequest cachePutRequest : cachePutRequests) {
cachePutRequest.apply(result.get()); cachePutRequest.apply(result.get());
@ -182,13 +186,13 @@ public abstract class CacheAspectSupport implements InitializingBean {
CacheEvictOperation operation = (CacheEvictOperation) context.operation; CacheEvictOperation operation = (CacheEvictOperation) context.operation;
if (beforeInvocation == operation.isBeforeInvocation() && if (beforeInvocation == operation.isBeforeInvocation() &&
isConditionPassing(context, result)) { isConditionPassing(context, result)) {
performCacheEvict(context, operation); performCacheEvict(context, operation, result);
} }
} }
} }
private void performCacheEvict(CacheOperationContext context, private void performCacheEvict(CacheOperationContext context,
CacheEvictOperation operation) { CacheEvictOperation operation, Object result) {
Object key = null; Object key = null;
for (Cache cache : context.getCaches()) { for (Cache cache : context.getCaches()) {
if (operation.isCacheWide()) { if (operation.isCacheWide()) {
@ -196,7 +200,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
cache.clear(); cache.clear();
} else { } else {
if(key == null) { if(key == null) {
key = context.generateKey(); key = context.generateKey(result);
} }
logInvalidating(context, operation, key); logInvalidating(context, operation, key);
cache.evict(key); cache.evict(key);
@ -213,10 +217,11 @@ public abstract class CacheAspectSupport implements InitializingBean {
} }
} }
private void collectPutRequests(Collection<CacheOperationContext> contexts, Collection<CachePutRequest> putRequests, boolean whenNotInCache) { private void collectPutRequests(Collection<CacheOperationContext> contexts,
Object result, Collection<CachePutRequest> putRequests, boolean whenNotInCache) {
for (CacheOperationContext context : contexts) { for (CacheOperationContext context : contexts) {
if (isConditionPassing(context, ExpressionEvaluator.NO_RESULT)) { if (isConditionPassing(context, result)) {
Object key = generateKey(context); Object key = generateKey(context, result);
if (!whenNotInCache || findInCaches(context, key) == null) { if (!whenNotInCache || findInCaches(context, key) == null) {
putRequests.add(new CachePutRequest(context, key)); putRequests.add(new CachePutRequest(context, key));
} }
@ -229,7 +234,8 @@ public abstract class CacheAspectSupport implements InitializingBean {
for (CacheOperationContext context : contexts) { for (CacheOperationContext context : contexts) {
if (isConditionPassing(context, ExpressionEvaluator.NO_RESULT)) { if (isConditionPassing(context, ExpressionEvaluator.NO_RESULT)) {
if(result == null) { if(result == null) {
result = findInCaches(context, generateKey(context)); result = findInCaches(context,
generateKey(context, ExpressionEvaluator.NO_RESULT));
} }
} }
} }
@ -254,8 +260,8 @@ public abstract class CacheAspectSupport implements InitializingBean {
return passing; return passing;
} }
private Object generateKey(CacheOperationContext context) { private Object generateKey(CacheOperationContext context, Object result) {
Object key = context.generateKey(); Object key = context.generateKey(result);
Assert.notNull(key, "Null key returned for cache operation (maybe you " Assert.notNull(key, "Null key returned for cache operation (maybe you "
+ "are using named params on classes without debug info?) " + "are using named params on classes without debug info?) "
+ context.operation); + context.operation);
@ -393,9 +399,9 @@ public abstract class CacheAspectSupport implements InitializingBean {
* Computes the key for the given caching operation. * Computes the key for the given caching operation.
* @return generated key (null if none can be generated) * @return generated key (null if none can be generated)
*/ */
protected Object generateKey() { protected Object generateKey(Object result) {
if (StringUtils.hasText(this.operation.getKey())) { if (StringUtils.hasText(this.operation.getKey())) {
EvaluationContext evaluationContext = createEvaluationContext(ExpressionEvaluator.NO_RESULT); EvaluationContext evaluationContext = createEvaluationContext(result);
return CacheAspectSupport.this.evaluator.key(this.operation.getKey(), this.method, evaluationContext); return CacheAspectSupport.this.evaluator.key(this.operation.getKey(), this.method, evaluationContext);
} }
return CacheAspectSupport.this.keyGenerator.generate(this.target, this.method, this.args); return CacheAspectSupport.this.keyGenerator.generate(this.target, this.method, this.args);

View File

@ -367,6 +367,16 @@ public abstract class AbstractAnnotationTests {
assertSame(r2, secondary.get(o).get()); assertSame(r2, secondary.get(o).get());
} }
public void testPutRefersToResult(CacheableService<?> service) throws Exception {
Long id = Long.MIN_VALUE;
TestEntity entity = new TestEntity();
Cache primary = cm.getCache("primary");
assertNull(primary.get(id));
assertNull(entity.getId());
service.putRefersToResult(entity);
assertSame(entity, primary.get(id).get());
}
public void testMultiCacheAndEvict(CacheableService<?> service) { public void testMultiCacheAndEvict(CacheableService<?> service) {
String methodName = "multiCacheAndEvict"; String methodName = "multiCacheAndEvict";
@ -621,6 +631,16 @@ public abstract class AbstractAnnotationTests {
testMultiPut(ccs); testMultiPut(ccs);
} }
@Test
public void testPutRefersToResult() throws Exception {
testPutRefersToResult(cs);
}
@Test
public void testClassPutRefersToResult() throws Exception {
testPutRefersToResult(ccs);
}
@Test @Test
public void testMultiCacheAndEvict() { public void testMultiCacheAndEvict() {
testMultiCacheAndEvict(cs); testMultiCacheAndEvict(cs);

View File

@ -31,6 +31,7 @@ import org.springframework.cache.annotation.Caching;
public class AnnotatedClassCacheableService implements CacheableService<Object> { public class AnnotatedClassCacheableService implements CacheableService<Object> {
private final AtomicLong counter = new AtomicLong(); private final AtomicLong counter = new AtomicLong();
public static final AtomicLong nullInvocations = new AtomicLong(); public static final AtomicLong nullInvocations = new AtomicLong();
@Override @Override
@ -164,4 +165,11 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
public Object multiUpdate(Object arg1) { public Object multiUpdate(Object arg1) {
return arg1; return arg1;
} }
@Override
@CachePut(value="primary", key="#result.id")
public TestEntity putRefersToResult(TestEntity arg1) {
arg1.setId(Long.MIN_VALUE);
return arg1;
}
} }

View File

@ -70,4 +70,6 @@ public interface CacheableService<T> {
T multiConditionalCacheAndEvict(Object arg1); T multiConditionalCacheAndEvict(Object arg1);
T multiUpdate(Object arg1); T multiUpdate(Object arg1);
TestEntity putRefersToResult(TestEntity arg1);
} }

View File

@ -170,4 +170,11 @@ public class DefaultCacheableService implements CacheableService<Long> {
public Long multiUpdate(Object arg1) { public Long multiUpdate(Object arg1) {
return Long.valueOf(arg1.toString()); return Long.valueOf(arg1.toString());
} }
@Override
@CachePut(value="primary", key="#result.id")
public TestEntity putRefersToResult(TestEntity arg1) {
arg1.setId(Long.MIN_VALUE);
return arg1;
}
} }

View File

@ -0,0 +1,56 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.config;
import org.springframework.util.ObjectUtils;
/**
* Simple test entity for use with caching tests.
*
* @author Michael Plšd
*/
public class TestEntity {
private Long id;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.id);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null) {
return false;
}
if (obj instanceof TestEntity) {
return ObjectUtils.nullSafeEquals(this.id, ((TestEntity) obj).id);
}
return false;
}
}

View File

@ -45,6 +45,7 @@
<cache:cache-evict method="multiConditionalCacheAndEvict" cache="secondary"/> <cache:cache-evict method="multiConditionalCacheAndEvict" cache="secondary"/>
<cache:cache-put method="multiUpdate" cache="primary"/> <cache:cache-put method="multiUpdate" cache="primary"/>
<cache:cache-put method="multiUpdate" cache="secondary"/> <cache:cache-put method="multiUpdate" cache="secondary"/>
<cache:cache-put method="putRefersToResult" cache="primary" key="#result.id"/>
</cache:caching> </cache:caching>
</cache:advice> </cache:advice>
@ -82,6 +83,7 @@
<cache:cache-evict method="multiConditionalCacheAndEvict" cache="secondary"/> <cache:cache-evict method="multiConditionalCacheAndEvict" cache="secondary"/>
<cache:cache-put method="multiUpdate" cache="primary"/> <cache:cache-put method="multiUpdate" cache="primary"/>
<cache:cache-put method="multiUpdate" cache="secondary"/> <cache:cache-put method="multiUpdate" cache="secondary"/>
<cache:cache-put method="putRefersToResult" cache="primary" key="#result.id"/>
</cache:caching> </cache:caching>
</cache:advice> </cache:advice>