Skip to content

Commit d550ffb

Browse files
committed
Clarified CompositeCacheManager's applicability, added convenience constructor with given delegates, and fixed getCacheNames implementation to never return duplicates
Issue: SPR-11427
1 parent 34d397e commit d550ffb

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

spring-context/src/main/java/org/springframework/cache/support/CompositeCacheManager.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -17,42 +17,72 @@
1717
package org.springframework.cache.support;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Collection;
2122
import java.util.Collections;
23+
import java.util.LinkedHashSet;
2224
import java.util.List;
25+
import java.util.Set;
2326

2427
import org.springframework.beans.factory.InitializingBean;
2528
import org.springframework.cache.Cache;
2629
import org.springframework.cache.CacheManager;
27-
import org.springframework.util.Assert;
2830

2931
/**
30-
* Composite {@link CacheManager} implementation that iterates
31-
* over a given collection of {@link CacheManager} instances.
32+
* Composite {@link CacheManager} implementation that iterates over
33+
* a given collection of delegate {@link CacheManager} instances.
3234
*
33-
* Allows {@link NoOpCacheManager} to be automatically added to the list for handling
34-
* the cache declarations without a backing store.
35+
* <p>Allows {@link NoOpCacheManager} to be automatically added to the end of
36+
* the list for handling cache declarations without a backing store. Otherwise,
37+
* any custom {@link CacheManager} may play that role of the last delegate as
38+
* well, lazily creating cache regions for any requested name.
39+
*
40+
* <p>Note: Regular CacheManagers that this composite manager delegates to need
41+
* to return {@code null} from {@link #getCache(String)} if they are unaware of
42+
* the specified cache name, allowing for iteration to the next delegate in line.
43+
* However, most {@link CacheManager} implementations fall back to lazy creation
44+
* of named caches once requested; check out the specific configuration details
45+
* for a 'static' mode with fixed cache names, if available.
3546
*
3647
* @author Costin Leau
3748
* @author Juergen Hoeller
3849
* @since 3.1
50+
* @see #setFallbackToNoOpCache
51+
* @see org.springframework.cache.concurrent.ConcurrentMapCacheManager#setCacheNames
3952
*/
40-
public class CompositeCacheManager implements InitializingBean, CacheManager {
53+
public class CompositeCacheManager implements CacheManager, InitializingBean {
4154

42-
private List<CacheManager> cacheManagers;
55+
private final List<CacheManager> cacheManagers = new ArrayList<CacheManager>();
4356

4457
private boolean fallbackToNoOpCache = false;
4558

4659

60+
/**
61+
* Construct an empty CompositeCacheManager, with delegate CacheManagers to
62+
* be added via the {@link #setCacheManagers "cacheManagers"} property.
63+
*/
64+
public CompositeCacheManager() {
65+
}
66+
67+
/**
68+
* Construct a CompositeCacheManager from the given delegate CacheManagers.
69+
* @param cacheManagers the CacheManagers to delegate to
70+
*/
71+
public CompositeCacheManager(CacheManager... cacheManagers) {
72+
setCacheManagers(Arrays.asList(cacheManagers));
73+
}
74+
75+
76+
/**
77+
* Specify the CacheManagers to delegate to.
78+
*/
4779
public void setCacheManagers(Collection<CacheManager> cacheManagers) {
48-
Assert.notEmpty(cacheManagers, "cacheManagers Collection must not be empty");
49-
this.cacheManagers = new ArrayList<CacheManager>();
5080
this.cacheManagers.addAll(cacheManagers);
5181
}
5282

5383
/**
54-
* Indicate whether a {@link NoOpCacheManager} should be added at the end of the manager lists.
55-
* In this case, any {@code getCache} requests not handled by the configured cache managers will
84+
* Indicate whether a {@link NoOpCacheManager} should be added at the end of the delegate list.
85+
* In this case, any {@code getCache} requests not handled by the configured CacheManagers will
5686
* be automatically handled by the {@link NoOpCacheManager} (and hence never return {@code null}).
5787
*/
5888
public void setFallbackToNoOpCache(boolean fallbackToNoOpCache) {
@@ -80,11 +110,11 @@ public Cache getCache(String name) {
80110

81111
@Override
82112
public Collection<String> getCacheNames() {
83-
List<String> names = new ArrayList<String>();
113+
Set<String> names = new LinkedHashSet<String>();
84114
for (CacheManager manager : this.cacheManagers) {
85115
names.addAll(manager.getCacheNames());
86116
}
87-
return Collections.unmodifiableList(names);
117+
return Collections.unmodifiableSet(names);
88118
}
89119

90120
}

0 commit comments

Comments
 (0)