这上一篇文章中我们熟悉了SpringBoot Cache的基本使用,接下来我们看下它的执行流程

  1. CacheAutoConfiguration 自动装配类

    根据图中标注,看到它引用了CachingConfigurationSelector这个类

 静态内部类,实现了 selectImports()这个方法 ,这个方法用于添加配置类

 通过debugger观察 imports[]数组,在控制台中看到 SimpleCacheConfiguration类的配置生效
 	static class CacheConfigurationImportSelector implements ImportSelector {

 	@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
CacheType[] types = CacheType.values();
String[] imports = new String[types.length];
for (int i = 0; i < types.length; i++) {
imports[i] = CacheConfigurations.getConfigurationClass(types[i]);
}
return imports;
} }
  1. SimpleCacheConfiguration 配置类

    创建ConcurrentMapCacheManager对象 ,给容器注册了CacheManage=>ConcurrentMapCacheManager

    @Bean
    public ConcurrentMapCacheManager cacheManager() {
    ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
    List<String> cacheNames = this.cacheProperties.getCacheNames();
    if (!cacheNames.isEmpty()) {
    cacheManager.setCacheNames(cacheNames);
    }
    return this.customizerInvoker.customize(cacheManager);
    }
  2. ConcurrentMapCacheManager 类

    [图片上传失败...(image-2a6db9-1530901912375)]

找到getCache()这个方法,根据方法名就可以知道这是获取缓存的方法

	@Override
@Nullable
public Cache getCache(String name) {
Cache cache = this.cacheMap.get(name);
if (cache == null && this.dynamic) {//判断是否为null
synchronized (this.cacheMap) {//加锁
cache = this.cacheMap.get(name);
if (cache == null) {
cache = createConcurrentMapCache(name);//保存至createConcurrentMapCache
this.cacheMap.put(name, cache);
}
}
}
return cache;
}

createConcurrentMapCache方法

protected Cache createConcurrentMapCache(String name) {
SerializationDelegate actualSerialization = (isStoreByValue() ? this.serialization : null);
return new ConcurrentMapCache(name, new ConcurrentHashMap<>(256),
isAllowNullValues(), actualSerialization); }

回到ConcurrentHashMap类,找到lookup()这个方法 ,这个缓存Key是怎么得到的呢,对这个方法打断点,看它的调用栈

@Override
@Nullable
protected Object lookup(Object key) {
return this.store.get(key);
}



进入generatorKey()这个方法



找到这个接口,是不是有了熟悉的感觉,这就是自定义主键生成策略需要实现的接口



致此,整合流程也就走完了,这里增加一点内容关于@Caching

这个注解相当于把 @CachePut、 @CacheEvict、@Cacheable这三个注解组合在一起,增加了灵活性,使用与之前类似,就不展开了



如理解有误,请指正

SpringBoot Cache 深入的更多相关文章

  1. springBoot cache操作2

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zxd1435513775/article/details/85091793一.基本项目搭建测试项目是 ...

  2. SpringBoot——Cache使用原理及Redis整合

    前言及核心概念介绍 前言 本篇主要介绍SpringBoot2.x 中 Cahe 的原理及几个主要注解,以及整合 Redis 作为缓存的步骤 核心概念 先来看看核心接口的作用及关系图: CachingP ...

  3. SpringBoot Cache 入门

    首先搭载开发环境,不会的可以参考笔者之前的文章SpringBoot入门 添加依赖 <dependency> <groupId>org.springframework.boot& ...

  4. 使用Springboot Cache做简单缓存

    使用Springboot Cache做简单缓存 1.简单介绍 ​ 当我们需要展示数据的时候,后台会根据需要从服务器中获取数据,但是频繁的请求数据库会对服务造成压力,于是我们引入了缓存这个概念. ​ 当 ...

  5. 完整SpringBoot Cache整合redis缓存(二)

    缓存注解概念 名称 解释 Cache 缓存接口,定义缓存操作.实现有:RedisCache.EhCacheCache.ConcurrentMapCache等 CacheManager 缓存管理器,管理 ...

  6. springboot整合spring @Cache和Redis

    转载请注明出处:https://www.cnblogs.com/wenjunwei/p/10779450.html spring基于注解的缓存 对于缓存声明,spring的缓存提供了一组java注解: ...

  7. SpringBoot 整合Ehcache3

    SpringBootLean 是对springboot学习与研究项目,是依据实际项目的形式对进行配置与处理,欢迎star与fork. [oschina 地址] http://git.oschina.n ...

  8. SpringBoot document notes

    图片拷贝不过来,直接从github上下载 . 链接: https://github.com/DFX339/SpringBootDocumentNotes.git Create a example po ...

  9. SpringBoot实战(十三)之缓存

    什么是缓存? 引用下百度百科的解释: 缓存就是数据交换的缓冲区(又称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,找到了则直接执行,找不到的话则从内存中查找.由于缓存的运行速度 ...

随机推荐

  1. linux基础之用户及用户组管理

    本节内容 用户管理 1. 为什么需要用户? 1.linux是一个多用户系统 2.权限管理(权限最小化) 2. 用户相关文件 /etc/passwd -->用户基本信息 /etc/shadow - ...

  2. Scala 字符串插值器

    Scala 提供了三种创新的字符串插值方法:s,f和raw,使用他们我们可以方便快捷的组合字符串. s 字符串插值器 在任何字符串前加上s,就可以直接在串中使用变量了,在生成字符串的时候会隐式调用其t ...

  3. 控制器网关/dns设置

    如果控制器ping内网可以,但是ping不同外网,十有八九是因为网关的问题,可以使用route命令设置网关,如设置为192.168.31.1(不是192.168.31.0),route add def ...

  4. SUSE12 操作系统安装

    今天开发同事需要一个客户的SUSE环境,原来没有安装过这个操作系统,网络配置方面有些问题见下一篇 镜像:SLE-12-SP3-Server-DVD-x86_64-GM-DVD1.iso 安装过程: 选 ...

  5. Swift系列十 - inout的本质

    inout是可以用来在函数内部修改外部属性内存的. 一.inout回顾 示例代码: func test(_ num: inout Int) { num = 20 } var a = 10 test(& ...

  6. synchronized使用及java中的原子性问题

    1.Synchronized关键字使用 class X { // 修饰非静态方法 synchronized void foo() { // 临界区 } // 修饰静态方法 synchronized s ...

  7. 原子层沉积(ALD)和化学气相沉积(CVD)微电子制造铜金属化的研究进展

    原子层沉积(ALD)和化学气相沉积(CVD)微电子制造铜金属化的研究进展 Atomic Layer Deposition (ALD) and Chemical Vapor Deposition (CV ...

  8. CPU,GPU,GPGPU

    CPU,GPU,GPGPU 1.基本概念 1.1  GPU 图形处理器(bai英语:Graphics Processing Unit,缩写:GPU),又称显示核心.视觉du处理器.zhi显示芯片,是一 ...

  9. CAP 超详细名词解释

    目录 引言 概述 分布式 一致性 ACID中的一致性 可用性 分区容错性 可用性与分区容错性,傻傻分不清 问题1:分区容错性说分区故障正常工作,什么叫正常工作?这个正常工作是指满足可用性吗? 问题2: ...

  10. IDEA骚技巧

    1. var 声明 2. null 判空 3. notnull 判非空 4. nn 判非空 5. for 遍历 6. fori 带索引的遍历 7. not 取反 8. if 条件判断 9. cast ...