Skip to content

Commit 20c34cb

Browse files
committed
Provide predetermined capacity for cache operation collections
Issue: SPR-17079
1 parent 8e5f243 commit 20c34cb

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* invocation context.
3333
*
3434
* @author Stephane Nicoll
35+
* @author Juergen Hoeller
3536
* @since 4.1
3637
*/
3738
public abstract class AbstractCacheResolver implements CacheResolver, InitializingBean {
@@ -83,18 +84,16 @@ public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext
8384
if (cacheNames == null) {
8485
return Collections.emptyList();
8586
}
86-
else {
87-
Collection<Cache> result = new ArrayList<>();
88-
for (String cacheName : cacheNames) {
89-
Cache cache = getCacheManager().getCache(cacheName);
90-
if (cache == null) {
91-
throw new IllegalArgumentException("Cannot find cache named '" +
92-
cacheName + "' for " + context.getOperation());
93-
}
94-
result.add(cache);
87+
Collection<Cache> result = new ArrayList<>(cacheNames.size());
88+
for (String cacheName : cacheNames) {
89+
Cache cache = getCacheManager().getCache(cacheName);
90+
if (cache == null) {
91+
throw new IllegalArgumentException("Cannot find cache named '" +
92+
cacheName + "' for " + context.getOperation());
9593
}
96-
return result;
94+
result.add(cache);
9795
}
96+
return result;
9897
}
9998

10099
/**

spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,16 @@ private Object generateKey(CacheOperationContext context, @Nullable Object resul
586586

587587
private class CacheOperationContexts {
588588

589-
private final MultiValueMap<Class<? extends CacheOperation>, CacheOperationContext> contexts =
590-
new LinkedMultiValueMap<>();
589+
private final MultiValueMap<Class<? extends CacheOperation>, CacheOperationContext> contexts;
591590

592591
private final boolean sync;
593592

594593
public CacheOperationContexts(Collection<? extends CacheOperation> operations, Method method,
595594
Object[] args, Object target, Class<?> targetClass) {
596595

597-
for (CacheOperation operation : operations) {
598-
this.contexts.add(operation.getClass(), getOperationContext(operation, method, args, target, targetClass));
596+
this.contexts = new LinkedMultiValueMap<>(operations.size());
597+
for (CacheOperation op : operations) {
598+
this.contexts.add(op.getClass(), getOperationContext(op, method, args, target, targetClass));
599599
}
600600
this.sync = determineSyncFlag(method);
601601
}
@@ -623,18 +623,22 @@ private boolean determineSyncFlag(Method method) {
623623
}
624624
if (syncEnabled) {
625625
if (this.contexts.size() > 1) {
626-
throw new IllegalStateException("@Cacheable(sync=true) cannot be combined with other cache operations on '" + method + "'");
626+
throw new IllegalStateException(
627+
"@Cacheable(sync=true) cannot be combined with other cache operations on '" + method + "'");
627628
}
628629
if (cacheOperationContexts.size() > 1) {
629-
throw new IllegalStateException("Only one @Cacheable(sync=true) entry is allowed on '" + method + "'");
630+
throw new IllegalStateException(
631+
"Only one @Cacheable(sync=true) entry is allowed on '" + method + "'");
630632
}
631633
CacheOperationContext cacheOperationContext = cacheOperationContexts.iterator().next();
632634
CacheableOperation operation = (CacheableOperation) cacheOperationContext.getOperation();
633635
if (cacheOperationContext.getCaches().size() > 1) {
634-
throw new IllegalStateException("@Cacheable(sync=true) only allows a single cache on '" + operation + "'");
636+
throw new IllegalStateException(
637+
"@Cacheable(sync=true) only allows a single cache on '" + operation + "'");
635638
}
636639
if (StringUtils.hasText(operation.getUnless())) {
637-
throw new IllegalStateException("@Cacheable(sync=true) does not support unless attribute on '" + operation + "'");
640+
throw new IllegalStateException(
641+
"@Cacheable(sync=true) does not support unless attribute on '" + operation + "'");
638642
}
639643
return true;
640644
}

0 commit comments

Comments
 (0)