Skip to content

Commit 3878db2

Browse files
committed
Provide predetermined capacity for cache operation collections
Issue: SPR-17079 (cherry picked from commit 20c34cb)
1 parent 24a113f commit 3878db2

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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 {
@@ -40,9 +41,17 @@ public abstract class AbstractCacheResolver implements CacheResolver, Initializi
4041
private CacheManager cacheManager;
4142

4243

44+
/**
45+
* Construct a new {@code AbstractCacheResolver}.
46+
* @see #setCacheManager
47+
*/
4348
protected AbstractCacheResolver() {
4449
}
4550

51+
/**
52+
* Construct a new {@code AbstractCacheResolver} for the given {@link CacheManager}.
53+
* @param cacheManager the CacheManager to use
54+
*/
4655
protected AbstractCacheResolver(CacheManager cacheManager) {
4756
this.cacheManager = cacheManager;
4857
}
@@ -75,18 +84,16 @@ public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext
7584
if (cacheNames == null) {
7685
return Collections.emptyList();
7786
}
78-
else {
79-
Collection<Cache> result = new ArrayList<>();
80-
for (String cacheName : cacheNames) {
81-
Cache cache = getCacheManager().getCache(cacheName);
82-
if (cache == null) {
83-
throw new IllegalArgumentException("Cannot find cache named '" +
84-
cacheName + "' for " + context.getOperation());
85-
}
86-
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());
8793
}
88-
return result;
94+
result.add(cache);
8995
}
96+
return result;
9097
}
9198

9299
/**

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
@@ -557,16 +557,16 @@ private Object generateKey(CacheOperationContext context, @Nullable Object resul
557557

558558
private class CacheOperationContexts {
559559

560-
private final MultiValueMap<Class<? extends CacheOperation>, CacheOperationContext> contexts =
561-
new LinkedMultiValueMap<>();
560+
private final MultiValueMap<Class<? extends CacheOperation>, CacheOperationContext> contexts;
562561

563562
private final boolean sync;
564563

565564
public CacheOperationContexts(Collection<? extends CacheOperation> operations, Method method,
566565
Object[] args, Object target, Class<?> targetClass) {
567566

568-
for (CacheOperation operation : operations) {
569-
this.contexts.add(operation.getClass(), getOperationContext(operation, method, args, target, targetClass));
567+
this.contexts = new LinkedMultiValueMap<>(operations.size());
568+
for (CacheOperation op : operations) {
569+
this.contexts.add(op.getClass(), getOperationContext(op, method, args, target, targetClass));
570570
}
571571
this.sync = determineSyncFlag(method);
572572
}
@@ -594,18 +594,22 @@ private boolean determineSyncFlag(Method method) {
594594
}
595595
if (syncEnabled) {
596596
if (this.contexts.size() > 1) {
597-
throw new IllegalStateException("@Cacheable(sync=true) cannot be combined with other cache operations on '" + method + "'");
597+
throw new IllegalStateException(
598+
"@Cacheable(sync=true) cannot be combined with other cache operations on '" + method + "'");
598599
}
599600
if (cacheOperationContexts.size() > 1) {
600-
throw new IllegalStateException("Only one @Cacheable(sync=true) entry is allowed on '" + method + "'");
601+
throw new IllegalStateException(
602+
"Only one @Cacheable(sync=true) entry is allowed on '" + method + "'");
601603
}
602604
CacheOperationContext cacheOperationContext = cacheOperationContexts.iterator().next();
603605
CacheableOperation operation = (CacheableOperation) cacheOperationContext.getOperation();
604606
if (cacheOperationContext.getCaches().size() > 1) {
605-
throw new IllegalStateException("@Cacheable(sync=true) only allows a single cache on '" + operation + "'");
607+
throw new IllegalStateException(
608+
"@Cacheable(sync=true) only allows a single cache on '" + operation + "'");
606609
}
607610
if (StringUtils.hasText(operation.getUnless())) {
608-
throw new IllegalStateException("@Cacheable(sync=true) does not support unless attribute on '" + operation + "'");
611+
throw new IllegalStateException(
612+
"@Cacheable(sync=true) does not support unless attribute on '" + operation + "'");
609613
}
610614
return true;
611615
}

0 commit comments

Comments
 (0)