一、案例准备

1.创建数据表(employee表)

2.创建Employee实体类封装数据库中的数据

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Employee { private Integer id;
private String lastName;
private String email;
private Integer gender; //1.男 2.女
private Integer dId;
}

3.编写EmployeeMapper接口(DAO测通)

@Mapper
public interface EmployeeMapper { @Select("select * from employee where id=#{id}")
Employee getEmpById(Integer id);
}

4.编写EmployeeService接口及其EmployeeServiceImpl实现类

@Service
public class EmployeeServiceImpl implements EmployeeService { @Autowired
private EmployeeMapper employeeMapper; @Override
@Cacheable(cacheNames = "emp",key = "#id",condition = "#id>0")
public Employee getEmp(Integer id) {
System.out.println("正在查询id为"+id+"号的员工");
Employee emp = employeeMapper.getEmpById(id);
return emp;
}
}

5.编写EmployeeController类

@RestController
public class EmpController { @Autowired
private EmployeeService employeeService; @GetMapping("/emp/{id}")
public Employee getEmployee(@PathVariable("id") Integer id){
Employee emp = employeeService.getEmp(id);
return emp;
}
}

6.启动访问http://localhost:8080/emp/1

成功!


-----------------------------------------------分割线-------------------------------------------------------------


二、工作原理分析

1.查看springboot启动时,导入了哪些缓存组件

通过以往springboot相关的自动配置类可知与缓存相关的自动配置类为CacheAutoConfiguration

@Import:向容器中导入一些组件(通常导入的选择器以ImportSelector结尾)
ctrl+右键查看CacheConfigurationImportSelector源码

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

打上断点,debug模式下运行

放行,查看return imports的结果


导入的组件如下

 	* "org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration"
* "org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration"

2.是哪个缓存配置类生效呢?根据当前的场景进行分析

有两种方法可以得出哪个缓存配置类生效

第一种:源码分析(该方法只做简单说明)
随便打开一个缓存配置类,例如第一个GenericCacheConfiguration,查看源码如下

@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(Cache.class)
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
class GenericCacheConfiguration { @Bean
SimpleCacheManager cacheManager(CacheManagerCustomizers customizers, Collection<Cache> caches) {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(caches);
return customizers.customize(cacheManager);
} }

根据类上的
@ConditionalOnBean(Cache.class)
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
注解进行判断该类是否生效
这些都是@Conditional注解的衍生注解,该注解是Spring4新推出的注解,判断是否满足某种条件,如果满足则给容器注册bean

第二种:查看自动配置报告
在application.properties配置文件中添加

debug=true

运行发现这几个组件中只有SimpleCacheConfiguration生效了

3.分析核心类的作用

进去查看SimpleCacheConfiguration的源码

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
class SimpleCacheConfiguration { @Bean
ConcurrentMapCacheManager cacheManager(CacheProperties cacheProperties,
CacheManagerCustomizers cacheManagerCustomizers) {
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
List<String> cacheNames = cacheProperties.getCacheNames();
if (!cacheNames.isEmpty()) {
cacheManager.setCacheNames(cacheNames);
}
return cacheManagerCustomizers.customize(cacheManager);
} }

可以看出给容器中注册了一个ConcurrentMapCacheManager缓存管理器
查看源码分析创建Cache的具体细节

这一段代码是创建Cache的核心
#首先通过该类中的cacheMap属性获取缓存,参数为缓存名字(key-value)
#然后进行判断,如果cache为null,则上锁;再次获取如果为null,则根据本类中的createConcurrentMapCache方法创建Cache,然后将其放到缓存中

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

可以看出返回时创建了ConcurrentMapCache对象,进去查看源码


lookup方法的作用是从缓存中获取数据
put方法的作用是是保存数据到缓存中

自此我们可以猜一下,ConcurrentMapCache该类的作用就是对缓存中的数据进行操作,如果缓存中没有数据,则从数据库查询,一并放到缓存中

总的来说ConcurrentMapCacheManager类的作用就是,先判断是否有某缓存,如果没有就创建该缓存,ConcurrentMapCache类从数据库中进行查询,一并将数据存储到ConcurrentMap集合中


4.运行流程

根据上面打上的四个断点,debug模式下启动


发现程序刚开始并没有走EmployeeServiceImpl的断点,而是走到了这个getCache方法,寻找name=emp的缓存

因为没有名为emp的缓存,所以会创建名为emp的缓存,继续放行

使用key去缓存中查找内容(key默认是方法的参数,浏览器访问的是1号员工信息,id=1即key=1),size=0,缓存中的数据为空

继续放行

直接调用业务方法去数据库中查询数据了,这是为什么呢,因为上个步骤在缓存中没有查询到数据,所以需要向数据库中要数据;继续放行

上一步从数据库中查询出数据之后,将数据传回前端页面展示并使用put方法将其放入到缓存中






此时缓存中已经有数据了,当我再次debug运行 就不会再从数据库中查询数据了


该博客仅为了记录自己的学习过程,理清技术点思路

深度理解springboot集成cache缓存之源码解析的更多相关文章

  1. Springboot集成RabbitMQ之MessageConvert源码解析

    问题 最近在使用RabbitMq时遇到了一个问题,明明是转换成json发送到mq中的数据,消费者接收到的却是一串数字也就是byte数组,但是使用mq可视化页面查看数据却是正常的,之前在使用过程中从未遇 ...

  2. 详解SpringBoot集成jsp(附源码)+遇到的坑

    本文介绍了SpringBoot集成jsp(附源码)+遇到的坑 ,分享给大家 1.大体步骤 (1)创建Maven web project: (2)在pom.xml文件添加依赖: (3)配置applica ...

  3. SpringBoot集成jsp(附源码)+遇到的坑

    1.大体步骤 (1)       创建Maven web project: (2)       在pom.xml文件添加依赖: (3)       配置application.properties支持 ...

  4. SpringBoot(1.5.6.RELEASE)源码解析

    转自 https://www.cnblogs.com/dylan-java/p/7450914.html 启动SpringBoot,需要在入口函数所在的类上添加@SpringBootApplicati ...

  5. springboot ---> spring ioc 注册流程 源码解析 this.prepareContext 部分

    现在都是在springboot 中 集成 spirng,那我们就从springboot 开始. 一:springboot 启动main 函数 public static void main(Strin ...

  6. springboot自动扫描添加的BeanDefinition源码解析

    1. springboot启动过程中,首先会收集需要加载的bean的定义,作为BeanDefinition对象,添加到BeanFactory中去. 由于BeanFactory中只有getBean之类获 ...

  7. Springboot集成cache的key生成策略

    代码接上文:深度理解springboot集成redis缓存之源码解析 ## 1.使用SpEL表达式 @Cacheable(cacheNames = "emp",key = &quo ...

  8. [源码解析] PyTorch 流水线并行实现 (6)--并行计算

    [源码解析] PyTorch 流水线并行实现 (6)--并行计算 目录 [源码解析] PyTorch 流水线并行实现 (6)--并行计算 0x00 摘要 0x01 总体架构 1.1 使用 1.2 前向 ...

  9. [源码解析] PyTorch 分布式(1)------历史和概述

    [源码解析] PyTorch 分布式(1)------历史和概述 目录 [源码解析] PyTorch 分布式(1)------历史和概述 0x00 摘要 0x01 PyTorch分布式的历史 1.1 ...

随机推荐

  1. 题解 P1659 【[国家集训队]拉拉队排练】

    一眼可得PAM 如果没学过PAM的可以看这里:PAM学习小结 我们令PAM上多记录一个信息\(sum\),表示该节点表示串在原串上出现了多少次. 当我们处理完了\(sum\),对于长度\(len\)为 ...

  2. 配置DHCP Relay的功能原理是什么?

    DHCP中继代理,就是在DHCP服务器和客户端之间转发DHCP数据包.当DHCP客户端与服务器不在同一个子网上,就必须有DHCP中继代理来转发DHCP请求和应答消息.DHCP中继代理的数据转发,与通常 ...

  3. MySQL 8.0安装以及初始化错误解决方法

    MySQL 8.0 安装配置及错误排查 官网下载 CentOS7环境下的具体安装步骤 初始化MySQL发生错误的解决方法 忘记数据库root密码 官网下载 mysql官网下载链接:https://de ...

  4. VUE3 之 状态动画 - 这个系列的教程通俗易懂,适合新手

    1. 概述 老话说的好:不用羡慕别人,每个人都有属于自己的人生道路,重要的是在前进道路上遇见阻碍时,如何去积极的面对并解决. 言归正传,今天我们来聊聊 VUE 的状态动画. 2. 状态动画 2.1 数 ...

  5. 如何使用Google Analytics Universal Analytics增强型电子商务

    Google Analytics: Universal Analytics增强型电子商务,可以让运营人员轻松地跟踪用户在其购物历程中与产品的互动,包括产品展示.产品点击.查看产品详情.将产品添加到购物 ...

  6. 《前端运维》一、Linux基础--09常用软件安装

    一.软件包管理 RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的"添加/删除程序".软件包有几种类型,我们一起来看下: 源 ...

  7. exit函数和return语句

    exit函数是c语言的库函数,有一个整型的参数,代表进程终止,这个函数需<stdlib.h>头文件 在函数中写return只是代表函数终止了,不管在程序的任何位置调用exit那么进程就立即 ...

  8. CentOS 7.5 安装配置tigervnc-server

    系统版本: [root@s10 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) 1.安装 Gnome 包 [root@ ...

  9. python 模块之 selenium 自动化使用教程

    一.安装 pip install Selenium 二.初始化浏览器 Chrome 是初始化谷歌浏览器 Firefox 是初始化火狐浏览器 Edge 是初始化IE浏览器 PhantomJS 是一个无界 ...

  10. Acwing 社交距离 分类讨论+贪心

    一种新型疾病,COWVID-19,开始在全世界的奶牛之间传播. Farmer John 正在采取尽可能多的预防措施来防止他的牛群被感染. Farmer John 的牛棚是一个狭长的建筑物,有一排共 N ...