SpringBoot Cache 深入
这上一篇文章中我们熟悉了SpringBoot Cache的基本使用,接下来我们看下它的执行流程
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;
}
}
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);
}
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 深入的更多相关文章
- springBoot cache操作2
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zxd1435513775/article/details/85091793一.基本项目搭建测试项目是 ...
- SpringBoot——Cache使用原理及Redis整合
前言及核心概念介绍 前言 本篇主要介绍SpringBoot2.x 中 Cahe 的原理及几个主要注解,以及整合 Redis 作为缓存的步骤 核心概念 先来看看核心接口的作用及关系图: CachingP ...
- SpringBoot Cache 入门
首先搭载开发环境,不会的可以参考笔者之前的文章SpringBoot入门 添加依赖 <dependency> <groupId>org.springframework.boot& ...
- 使用Springboot Cache做简单缓存
使用Springboot Cache做简单缓存 1.简单介绍 当我们需要展示数据的时候,后台会根据需要从服务器中获取数据,但是频繁的请求数据库会对服务造成压力,于是我们引入了缓存这个概念. 当 ...
- 完整SpringBoot Cache整合redis缓存(二)
缓存注解概念 名称 解释 Cache 缓存接口,定义缓存操作.实现有:RedisCache.EhCacheCache.ConcurrentMapCache等 CacheManager 缓存管理器,管理 ...
- springboot整合spring @Cache和Redis
转载请注明出处:https://www.cnblogs.com/wenjunwei/p/10779450.html spring基于注解的缓存 对于缓存声明,spring的缓存提供了一组java注解: ...
- SpringBoot 整合Ehcache3
SpringBootLean 是对springboot学习与研究项目,是依据实际项目的形式对进行配置与处理,欢迎star与fork. [oschina 地址] http://git.oschina.n ...
- SpringBoot document notes
图片拷贝不过来,直接从github上下载 . 链接: https://github.com/DFX339/SpringBootDocumentNotes.git Create a example po ...
- SpringBoot实战(十三)之缓存
什么是缓存? 引用下百度百科的解释: 缓存就是数据交换的缓冲区(又称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,找到了则直接执行,找不到的话则从内存中查找.由于缓存的运行速度 ...
随机推荐
- split命令_Linux split命令:切割(拆分)文件
<Linux就该这么学>是一本基于最新Linux系统编写的入门必读书籍,内容面向零基础读者,由浅入深渐进式教学,销量保持国内第一,年销售量预期超过10万本.点此免费在线阅读. 15 分钟之 ...
- 华为交换机Console口属性配置
华为交换机Console口属性配置 一.设置通过账号和密码(AAA验证)登陆Console口 进入 Console 用户界面视图 <Huawei>system-view [Huawei]u ...
- Linux 部署 iSCSI 客户端配置(Linux)
Linux 部署 iSCSI 客户端配置(Linux) 客户端环境 Client :RHEL8 IP : 192.168.121.11 一.测试与服务端的连通性 [root@Client-linux ...
- 微信引流的方式 PC控制手机的方式
http://www.yunjing100.cn/ 云鲸一百 小萝卜 http://www.xiaoluobei.com/
- Linux系统编程【5】——stty的学习
从文件的角度看设备 之前几篇文章介绍的编程是基于文件的.数据可以保存在文件中,也可以从文件中取出来做处理,再存回去.不仅如此,Linux操作系统还专门为这个东西建立了一套规则,就是前期介绍的" ...
- openssl自签发证书
DOMAIN=www.example.com openssl genrsa -out ${DOMAIN}.key # 生成私有key openssl req -x509 -new -nodes -ke ...
- [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)
39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...
- volatile 关键字笔记
你应该知道的 volatile 关键字 当一个变量被 volatile 修饰时,任何线程对它的写操作都会立即刷新到主内存中,并且会强制让缓存了该变量的线程中的数据清空,必须从主内存重新读取最新数据. ...
- 深度学*点云语义分割:CVPR2019论文阅读
深度学*点云语义分割:CVPR2019论文阅读 Point Cloud Oversegmentation with Graph-Structured Deep Metric Learning 摘要 本 ...
- 多视觉任务的全能: HRNet
多视觉任务的全能: HRNet HRNet是微软亚洲研究院的王井东老师领导的团队完成的,打通图像分类.图像分割.目标检测.人脸对齐.姿态识别.风格迁移.Image Inpainting.超分.opti ...