Skip to content

Commit d7591c6

Browse files
committed
Clarified CompositeCacheManager's applicability, added convenience constructor with given delegates, and fixed getCacheNames implementation to never return duplicates
Issue: SPR-11427 (cherry picked from commit d550ffb)
1 parent 42dec02 commit d7591c6

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

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

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 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,73 @@
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>();
80+
this.cacheManagers.clear(); // just here to preserve compatibility with previous behavior
5081
this.cacheManagers.addAll(cacheManagers);
5182
}
5283

5384
/**
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
85+
* Indicate whether a {@link NoOpCacheManager} should be added at the end of the delegate list.
86+
* In this case, any {@code getCache} requests not handled by the configured CacheManagers will
5687
* be automatically handled by the {@link NoOpCacheManager} (and hence never return {@code null}).
5788
*/
5889
public void setFallbackToNoOpCache(boolean fallbackToNoOpCache) {
@@ -77,11 +108,11 @@ public Cache getCache(String name) {
77108
}
78109

79110
public Collection<String> getCacheNames() {
80-
List<String> names = new ArrayList<String>();
111+
Set<String> names = new LinkedHashSet<String>();
81112
for (CacheManager manager : this.cacheManagers) {
82113
names.addAll(manager.getCacheNames());
83114
}
84-
return Collections.unmodifiableList(names);
115+
return Collections.unmodifiableSet(names);
85116
}
86117

87118
}

0 commit comments

Comments
 (0)