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);
} }

spring中的组合模式的更多相关文章

  1. 002-创建型-04-建造者模式(Builder)、JDK1.7源码中的建造者模式、Spring中的建造者模式

    一.概述 建造者模式的定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 工厂类模式提供的是创建单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来创建复合对象 ...

  2. 设计模式(十一):从文Finder中认识"组合模式"(Composite Pattern)

    上一篇博客中我们从从电影院中认识了"迭代器模式"(Iterator Pattern),今天我们就从文件系统中来认识一下“组合模式”(Composite Pattern).说到组合模 ...

  3. 在商城系统中使用设计模式----策略模式之在spring中使用策略模式

    1.前言: 这是策略模式在spring中的使用,对策略模式不了解对同学可以移步在商城中简单对使用策略模式. 2.问题: 在策略模式中,我们创建表示各种策略的对象和一个行为,随着策略对象改变而改变的 c ...

  4. js中的组合模式

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. spring中的原型模式

    大家好,我原本是神剑山庄的铸剑师,名叫小赵,本来干的好好的,后来一时兴起,睡了三少爷的小姨子,与其一直提心吊胆,干脆来个逃之夭夭. 但是,我也要吃饭的呀,工作也得找,神剑山庄去不得,还有断剑山庄.藏剑 ...

  6. Spring中的工厂模式和单例模式

    Spring预备知识(适合中小型项目) 作用:集成和管理其他框架 工厂模式: A  a  = new A( ); 将类所要创建的对象写入工厂,统一进行管理 package com.spring; pu ...

  7. Spring中的代理模式

    代理模式 所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 代理模式是一种 ...

  8. spring中的工厂模式

    spring的bean的创建原理就是框架利用反射创建出实例对象 工厂模式:工厂帮我们创建对象:有一个专门帮我们创建对象的类,我们把这个类叫做工厂类. 例如:Plane plane = PlaneFac ...

  9. 【Spring源码解析】—— 策略模式在Spring中的应用

    一.         什么是策略模式 策略模式的定义/含义:策略本身就是为了实现某一个目标而采取的一种工作方式,因此只要能够达成目标,则采取哪一种策略都可以:因此多种实际的策略之间是相互平行的. 注意 ...

随机推荐

  1. .NET Core授权失败如何自定义响应信息?

    前言 在.NET 5之前,当授权失败即403时无法很友好的自定义错误信息,以致于比如利用Vue获取到的是空响应,不能很好的处理实际业务,同时涉及到权限粒度控制到控制器.Action,也不能很好的获取对 ...

  2. JAVA 中的权限访问修饰符(public,protected,default,private )

    JAVA中有四个权限访问修饰符:public,protected,default,private 注意:这里讲的是对类中属性和方法的访问权限,并不是类的访问权限 1.default:包访问权限 如果什 ...

  3. Springboot:@RequestMapping注解及属性详解

    @RequestMapping 注解: @RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理 ...

  4. MySQL存储引擎——InnoDB和MyISAM的区别

    MySQL5.5后,默认存储引擎是InnoDB,5.5之前默认是MyISAM. InnoDB(事务性数据库引擎)和MyISAM的区别补充: InnoDB是聚集索引,数据结构是B+树,叶子节点存K-V, ...

  5. varnish配置语言(2)

    目录 1. Backend servers 2. 多个后端 3. Varnish 中的后端服务器和虚拟主机 4. 调度器 5. 健康检查 6. Hashing 7. 优雅模式 Grace mode 和 ...

  6. SHELL 变量引用

    shell变量的引用非常重要,运用技巧灵活多变 变量的引用主要包含四类:双引号引用.单引号引用.反引号引用.反斜线引用 " " 双引号 屏蔽除美元符号$.反引号( ` )和反斜线( ...

  7. C语言:printf标志符

    %d 十进制整数 %i %lf双精度浮点数 %o八进制整数  0%o或0%O %x十六进制整数  0x%x 或  0X%X %f单精度浮点数 %E  %e科学计数法 %s  字符串 %c  字符(单个 ...

  8. C语言 c++区别

    C语言是C89标准,C++是C++99标准的.C89就是在1989年制定的标准,如今最新的是C11和C++11标准.根据不同的标准,它们的功能也会有所不同,但是越新的版本支持的编译器越少

  9. 高校表白App-团队冲刺第二天

    今天要做什么 今天要把昨天的activity进行完善,并且加上计时跳转的功能,将其设置为主页面,设置两种跳转功能. 遇到的问题 今天没遇到什么大的问题,只是在进行编写的时候,又出现了R文件无法找到的情 ...

  10. [刘阳Java]_酷炫视频播放器制作_界面篇

    今天开始分享一篇酷炫播放器制作,包括界面+JS.整个案例非常类似腾讯视频,优酷视频,爱奇艺视频.我们先看一下效果图,然后这篇文章主要界面篇 是不是效果比较酷炫,那么我接着来给大家说一下这个界面设计思路 ...