Browse Source

Provide predetermined capacity for cache operation collections

Issue: SPR-17079

(cherry picked from commit 20c34cb)
pull/1916/head
Juergen Hoeller 6 years ago
parent
commit
3878db2e8c
  1. 29
      spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java
  2. 20
      spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -32,6 +32,7 @@ import org.springframework.util.Assert; @@ -32,6 +32,7 @@ import org.springframework.util.Assert;
* invocation context.
*
* @author Stephane Nicoll
* @author Juergen Hoeller
* @since 4.1
*/
public abstract class AbstractCacheResolver implements CacheResolver, InitializingBean {
@ -40,9 +41,17 @@ public abstract class AbstractCacheResolver implements CacheResolver, Initializi @@ -40,9 +41,17 @@ public abstract class AbstractCacheResolver implements CacheResolver, Initializi
private CacheManager cacheManager;
/**
* Construct a new {@code AbstractCacheResolver}.
* @see #setCacheManager
*/
protected AbstractCacheResolver() {
}
/**
* Construct a new {@code AbstractCacheResolver} for the given {@link CacheManager}.
* @param cacheManager the CacheManager to use
*/
protected AbstractCacheResolver(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
@ -75,18 +84,16 @@ public abstract class AbstractCacheResolver implements CacheResolver, Initializi @@ -75,18 +84,16 @@ public abstract class AbstractCacheResolver implements CacheResolver, Initializi
if (cacheNames == null) {
return Collections.emptyList();
}
else {
Collection<Cache> result = new ArrayList<>();
for (String cacheName : cacheNames) {
Cache cache = getCacheManager().getCache(cacheName);
if (cache == null) {
throw new IllegalArgumentException("Cannot find cache named '" +
cacheName + "' for " + context.getOperation());
}
result.add(cache);
Collection<Cache> result = new ArrayList<>(cacheNames.size());
for (String cacheName : cacheNames) {
Cache cache = getCacheManager().getCache(cacheName);
if (cache == null) {
throw new IllegalArgumentException("Cannot find cache named '" +
cacheName + "' for " + context.getOperation());
}
return result;
result.add(cache);
}
return result;
}
/**

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

@ -557,16 +557,16 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker @@ -557,16 +557,16 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
private class CacheOperationContexts {
private final MultiValueMap<Class<? extends CacheOperation>, CacheOperationContext> contexts =
new LinkedMultiValueMap<>();
private final MultiValueMap<Class<? extends CacheOperation>, CacheOperationContext> contexts;
private final boolean sync;
public CacheOperationContexts(Collection<? extends CacheOperation> operations, Method method,
Object[] args, Object target, Class<?> targetClass) {
for (CacheOperation operation : operations) {
this.contexts.add(operation.getClass(), getOperationContext(operation, method, args, target, targetClass));
this.contexts = new LinkedMultiValueMap<>(operations.size());
for (CacheOperation op : operations) {
this.contexts.add(op.getClass(), getOperationContext(op, method, args, target, targetClass));
}
this.sync = determineSyncFlag(method);
}
@ -594,18 +594,22 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker @@ -594,18 +594,22 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
}
if (syncEnabled) {
if (this.contexts.size() > 1) {
throw new IllegalStateException("@Cacheable(sync=true) cannot be combined with other cache operations on '" + method + "'");
throw new IllegalStateException(
"@Cacheable(sync=true) cannot be combined with other cache operations on '" + method + "'");
}
if (cacheOperationContexts.size() > 1) {
throw new IllegalStateException("Only one @Cacheable(sync=true) entry is allowed on '" + method + "'");
throw new IllegalStateException(
"Only one @Cacheable(sync=true) entry is allowed on '" + method + "'");
}
CacheOperationContext cacheOperationContext = cacheOperationContexts.iterator().next();
CacheableOperation operation = (CacheableOperation) cacheOperationContext.getOperation();
if (cacheOperationContext.getCaches().size() > 1) {
throw new IllegalStateException("@Cacheable(sync=true) only allows a single cache on '" + operation + "'");
throw new IllegalStateException(
"@Cacheable(sync=true) only allows a single cache on '" + operation + "'");
}
if (StringUtils.hasText(operation.getUnless())) {
throw new IllegalStateException("@Cacheable(sync=true) does not support unless attribute on '" + operation + "'");
throw new IllegalStateException(
"@Cacheable(sync=true) does not support unless attribute on '" + operation + "'");
}
return true;
}

Loading…
Cancel
Save