|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2012 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>(); |
50 | 80 | this.cacheManagers.addAll(cacheManagers);
|
51 | 81 | }
|
52 | 82 |
|
53 | 83 | /**
|
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 |
56 | 86 | * be automatically handled by the {@link NoOpCacheManager} (and hence never return {@code null}).
|
57 | 87 | */
|
58 | 88 | public void setFallbackToNoOpCache(boolean fallbackToNoOpCache) {
|
@@ -80,11 +110,11 @@ public Cache getCache(String name) {
|
80 | 110 |
|
81 | 111 | @Override
|
82 | 112 | public Collection<String> getCacheNames() {
|
83 |
| - List<String> names = new ArrayList<String>(); |
| 113 | + Set<String> names = new LinkedHashSet<String>(); |
84 | 114 | for (CacheManager manager : this.cacheManagers) {
|
85 | 115 | names.addAll(manager.getCacheNames());
|
86 | 116 | }
|
87 |
| - return Collections.unmodifiableList(names); |
| 117 | + return Collections.unmodifiableSet(names); |
88 | 118 | }
|
89 | 119 |
|
90 | 120 | }
|
0 commit comments