• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 20224960533

15 Dec 2025 08:11AM UTC coverage: 84.388% (-0.02%) from 84.404%
20224960533

push

github

TAKETODAY
:white_check_mark: 在测试中排除 jacoco 初始化方法以避免干扰

61869 of 78367 branches covered (78.95%)

Branch coverage included in aggregate %.

145916 of 167860 relevant lines covered (86.93%)

3.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
today-context/src/main/java/infra/cache/support/CompositeCacheManager.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17
package infra.cache.support;
18

19
import org.jspecify.annotations.Nullable;
20

21
import java.util.ArrayList;
22
import java.util.Arrays;
23
import java.util.Collection;
24
import java.util.Collections;
25
import java.util.LinkedHashSet;
26
import java.util.List;
27
import java.util.Set;
28

29
import infra.beans.factory.InitializingBean;
30
import infra.cache.Cache;
31
import infra.cache.CacheManager;
32
import infra.cache.concurrent.ConcurrentMapCacheManager;
33

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

59
  private final List<CacheManager> cacheManagers = new ArrayList<>();
×
60

61
  private boolean fallbackToNoOpCache = false;
×
62

63
  /**
64
   * Construct an empty CompositeCacheManager, with delegate CacheManagers to
65
   * be added via the {@link #setCacheManagers "cacheManagers"} property.
66
   */
67
  public CompositeCacheManager() {
×
68
  }
×
69

70
  /**
71
   * Construct a CompositeCacheManager from the given delegate CacheManagers.
72
   *
73
   * @param cacheManagers the CacheManagers to delegate to
74
   */
75
  public CompositeCacheManager(CacheManager... cacheManagers) {
×
76
    setCacheManagers(Arrays.asList(cacheManagers));
×
77
  }
×
78

79
  /**
80
   * Specify the CacheManagers to delegate to.
81
   */
82
  public void setCacheManagers(Collection<CacheManager> cacheManagers) {
83
    this.cacheManagers.addAll(cacheManagers);
×
84
  }
×
85

86
  /**
87
   * Indicate whether a {@link NoOpCacheManager} should be added at the end of the delegate list.
88
   * In this case, any {@code getCache} requests not handled by the configured CacheManagers will
89
   * be automatically handled by the {@link NoOpCacheManager} (and hence never return {@code null}).
90
   */
91
  public void setFallbackToNoOpCache(boolean fallbackToNoOpCache) {
92
    this.fallbackToNoOpCache = fallbackToNoOpCache;
×
93
  }
×
94

95
  @Override
96
  public void afterPropertiesSet() {
97
    if (this.fallbackToNoOpCache) {
×
98
      this.cacheManagers.add(new NoOpCacheManager());
×
99
    }
100
  }
×
101

102
  @Override
103
  @Nullable
104
  public Cache getCache(String name) {
105
    for (CacheManager cacheManager : this.cacheManagers) {
×
106
      Cache cache = cacheManager.getCache(name);
×
107
      if (cache != null) {
×
108
        return cache;
×
109
      }
110
    }
×
111
    return null;
×
112
  }
113

114
  @Override
115
  public Collection<String> getCacheNames() {
116
    Set<String> names = new LinkedHashSet<>();
×
117
    for (CacheManager manager : this.cacheManagers) {
×
118
      names.addAll(manager.getCacheNames());
×
119
    }
×
120
    return Collections.unmodifiableSet(names);
×
121
  }
122

123
  @Override
124
  public void resetCaches() {
125
    for (CacheManager manager : this.cacheManagers) {
×
126
      manager.resetCaches();
×
127
    }
×
128
  }
×
129

130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc