org.springframework.cache.support.CompositeCacheManager

/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.cache.support; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set; import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.lang.Nullable; /**
* Composite {@link CacheManager} implementation that iterates over
* a given collection of delegate {@link CacheManager} instances.
*
* <p>Allows {@link NoOpCacheManager} to be automatically added to the end of
* the list for handling cache declarations without a backing store. Otherwise,
* any custom {@link CacheManager} may play that role of the last delegate as
* well, lazily creating cache regions for any requested name.
*
* <p>Note: Regular CacheManagers that this composite manager delegates to need
* to return {@code null} from {@link #getCache(String)} if they are unaware of
* the specified cache name, allowing for iteration to the next delegate in line.
* However, most {@link CacheManager} implementations fall back to lazy creation
* of named caches once requested; check out the specific configuration details
* for a 'static' mode with fixed cache names, if available.
*
* @author Costin Leau
* @author Juergen Hoeller
* @since 3.1
* @see #setFallbackToNoOpCache
* @see org.springframework.cache.concurrent.ConcurrentMapCacheManager#setCacheNames
*/
public class CompositeCacheManager implements CacheManager, InitializingBean { private final List<CacheManager> cacheManagers = new ArrayList<>(); private boolean fallbackToNoOpCache = false; /**
* Construct an empty CompositeCacheManager, with delegate CacheManagers to
* be added via the {@link #setCacheManagers "cacheManagers"} property.
*/
public CompositeCacheManager() {
} /**
* Construct a CompositeCacheManager from the given delegate CacheManagers.
* @param cacheManagers the CacheManagers to delegate to
*/
public CompositeCacheManager(CacheManager... cacheManagers) {
setCacheManagers(Arrays.asList(cacheManagers));
} /**
* Specify the CacheManagers to delegate to.
*/
public void setCacheManagers(Collection<CacheManager> cacheManagers) {
this.cacheManagers.addAll(cacheManagers);
} /**
* Indicate whether a {@link NoOpCacheManager} should be added at the end of the delegate list.
* In this case, any {@code getCache} requests not handled by the configured CacheManagers will
* be automatically handled by the {@link NoOpCacheManager} (and hence never return {@code null}).
*/
public void setFallbackToNoOpCache(boolean fallbackToNoOpCache) {
this.fallbackToNoOpCache = fallbackToNoOpCache;
} @Override
public void afterPropertiesSet() {
if (this.fallbackToNoOpCache) {
this.cacheManagers.add(new NoOpCacheManager());
}
} @Override
@Nullable
public Cache getCache(String name) {
for (CacheManager cacheManager : this.cacheManagers) {
Cache cache = cacheManager.getCache(name);
if (cache != null) {
return cache;
}
}
return null;
} @Override
public Collection<String> getCacheNames() {
Set<String> names = new LinkedHashSet<>();
for (CacheManager manager : this.cacheManagers) {
names.addAll(manager.getCacheNames());
}
return Collections.unmodifiableSet(names);
} }

最新文章

  1. python 进程和线程
  2. 2013ACM/ICPC亚洲区南京站现场赛-HDU4809(树形DP)
  3. ffmpeg音频编码
  4. 我所理解的cocos2dx自适配屏幕大小方案
  5. Monkey之环境搭建完全版
  6. 在eclipse中安装插件
  7. PyDev-Python的Eclipse插件安装
  8. 特性扩展:WebActivatorEx
  9. 自制单片机之四……LCD1602的驱动
  10. VS中使用.NET Reactor进行代码混淆
  11. 谈谈ASP.NET Core中的ResponseCaching
  12. js 小写金额转大写
  13. Java-Socket实现文件的断点续传
  14. 新版Ubuntu安装日文输入法
  15. 你要的fpga&amp;数字前端笔面试题都在这儿了
  16. VLC播放H264文件问题
  17. django源码解析之 BooleanField (三)
  18. RavenDb学习(五)结果转换
  19. 【线程】Thread中的join介绍
  20. Abp中SwaggerUI的接口说明文档配置

热门文章

  1. 4.3Unicode和ASCII码
  2. BUU mrctf shit
  3. Jmeter之cokie管理器和http默认值
  4. java001-泛型
  5. WSL2:在Windows系统中开发Linux程序的又一神器
  6. ScienceDirect内容爬虫
  7. [刘阳Java]_Spring对Dao的支持_第10讲
  8. 【16位RAW图像处理三】直方图均衡化及局部直方图均衡用于16位图像的细节增强。
  9. 网络损伤仪WANsim的队列深度功能
  10. C++第四十五篇 -- MFC关闭调用的窗口