1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .cache .support ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Arrays ;
20
21
import java .util .Collection ;
21
22
import java .util .Collections ;
23
+ import java .util .LinkedHashSet ;
22
24
import java .util .List ;
25
+ import java .util .Set ;
23
26
24
27
import org .springframework .beans .factory .InitializingBean ;
25
28
import org .springframework .cache .Cache ;
26
29
import org .springframework .cache .CacheManager ;
27
- import org .springframework .util .Assert ;
28
30
29
31
/**
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.
32
34
*
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.
35
46
*
36
47
* @author Costin Leau
37
48
* @author Juergen Hoeller
38
49
* @since 3.1
50
+ * @see #setFallbackToNoOpCache
51
+ * @see org.springframework.cache.concurrent.ConcurrentMapCacheManager#setCacheNames
39
52
*/
40
- public class CompositeCacheManager implements InitializingBean , CacheManager {
53
+ public class CompositeCacheManager implements CacheManager , InitializingBean {
41
54
42
- private List <CacheManager > cacheManagers ;
55
+ private final List <CacheManager > cacheManagers = new ArrayList < CacheManager >() ;
43
56
44
57
private boolean fallbackToNoOpCache = false ;
45
58
46
59
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
+ */
47
79
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
50
81
this .cacheManagers .addAll (cacheManagers );
51
82
}
52
83
53
84
/**
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
56
87
* be automatically handled by the {@link NoOpCacheManager} (and hence never return {@code null}).
57
88
*/
58
89
public void setFallbackToNoOpCache (boolean fallbackToNoOpCache ) {
@@ -77,11 +108,11 @@ public Cache getCache(String name) {
77
108
}
78
109
79
110
public Collection <String > getCacheNames () {
80
- List <String > names = new ArrayList <String >();
111
+ Set <String > names = new LinkedHashSet <String >();
81
112
for (CacheManager manager : this .cacheManagers ) {
82
113
names .addAll (manager .getCacheNames ());
83
114
}
84
- return Collections .unmodifiableList (names );
115
+ return Collections .unmodifiableSet (names );
85
116
}
86
117
87
118
}
0 commit comments