首页 > 其他分享 >springboot缓存之缓存工作原理

springboot缓存之缓存工作原理

时间:2022-11-30 19:37:49浏览次数:42  
标签:缓存 springboot cache boot autoconfigure springframework 原理 ConcurrentMapCacheMana


核心:使用CacheManager[ConcurrentMapCacheManager]按照名字得到Cache[ConcurrentMapCache]组件

        key使用keyGenerator生成的,默认是SimpleKeyGenerator

        @Cacheable标注的方法执行之前先来检查缓存中有没有这个数据,默认按照参数的值作为key去查询缓存,如果没有就运行犯法并将结果放入缓存,以后再来调用就可以直接使用缓存中的数据.  

  • 自动配置类 
@Import({CacheAutoConfiguration.CacheConfigurationImportSelector.class, CacheAutoConfiguration.CacheManagerEntityManagerFactoryDependsOnPostProcessor.class})
public class CacheAutoConfiguration {

进入CacheConfigurationImportSelector类,给缓存导入配置组件

static class CacheConfigurationImportSelector implements ImportSelector {
CacheConfigurationImportSelector() {
}

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

springboot缓存之缓存工作原理_spring

springboot缓存之缓存工作原理_spring_02

  • 缓存的配置类

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.SimpleCacheConfiguration

org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration

  • 哪个配置类默认生效   SimpleCacheConfiguration

application.properties中新增


debug=true


Setting——keymap——Main menu——Edit——Find

springboot缓存之缓存工作原理_spring_03

springboot缓存之缓存工作原理_spring_04

注意是在default中

springboot缓存之缓存工作原理_spring_05

设置完即可在控制台中ctrl+F查找

springboot缓存之缓存工作原理_数据_06

进入此类

@Configuration(
proxyBeanMethods = false
)
@ConditionalOnMissingBean({CacheManager.class})
@Conditional({CacheCondition.class})
class SimpleCacheConfiguration {
SimpleCacheConfiguration() {
}

@Bean
ConcurrentMapCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers cacheManagerCustomizers) {
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
List<String> cacheNames = cacheProperties.getCacheNames();
if (!cacheNames.isEmpty()) {
cacheManager.setCacheNames(cacheNames);
}

return (ConcurrentMapCacheManager)cacheManagerCustomizers.customize(cacheManager);
}
}
  • 发现它给容器中注册了一个CacheManager:ConcurrentMapCacheManager
  • 进入ConcurrentMapCacheManager
public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderAware {
  • 进入CacheManager
public interface CacheManager {
@Nullable
Cache getCache(String var1);

Collection<String> getCacheNames();
}
  • 进入ConcurrentMapCacheManager,打个断点

springboot缓存之缓存工作原理_缓存_07

进入createConcurrentMapCache方法

protected Cache createConcurrentMapCache(String name) {
SerializationDelegate actualSerialization = this.isStoreByValue() ? this.serialization : null;
return new ConcurrentMapCache(name, new ConcurrentHashMap(256), this.isAllowNullValues(), actualSerialization);
}
  • 可知可以获取和创建ConcurrentMapCache类型的缓存组件;它的作用:将数据保存在ConcurrentMap中
  • 进入ConcurrentMapCache,打个断点

springboot缓存之缓存工作原理_缓存_08


private final ConcurrentMap<Object, Object> store;


  • 给service中打上断点

springboot缓存之缓存工作原理_数据_09

ConcurrentMapCache中

springboot缓存之缓存工作原理_缓存_10

springboot缓存之缓存工作原理_缓存_11

ConcurrentMapCacheManager

springboot缓存之缓存工作原理_spring_12

将断点打到方法内

刷新页面

springboot缓存之缓存工作原理_数据_13

发现在还未进入service缓存方法之前先调用getCache,得到

springboot缓存之缓存工作原理_数据_14

,即

springboot缓存之缓存工作原理_数据_15

缓存名

springboot缓存之缓存工作原理_缓存_16

springboot缓存之缓存工作原理_缓存_17

放行.进入到

springboot缓存之缓存工作原理_缓存_18

springboot缓存之缓存工作原理_缓存_19

,点进generateKey

springboot缓存之缓存工作原理_数据_20

继续点进

springboot缓存之缓存工作原理_缓存_21


private SingletonSupplier<KeyGenerator> keyGenerator = SingletonSupplier.of(SimpleKeyGenerator::new);


springboot缓存之缓存工作原理_spring_22

生成策略:

如果没有参数:key=new SimpleKey();

如果有一个参数:key=参数的值

如果有多个参数:key=new SimpleKey(params)

public static Object generateKey(Object... params) {
if (params.length == 0) {
return SimpleKey.EMPTY;
} else {
if (params.length == 1) {
Object param = params[0];
if (param != null && !param.getClass().isArray()) {
return param;
}
}

return new SimpleKey(params);
}
}

放行

springboot缓存之缓存工作原理_缓存_23

放行

springboot缓存之缓存工作原理_spring_24

刷新请求1号数据第二次的时候

直接从缓存中拿数据,不请求方法

springboot缓存之缓存工作原理_spring_25

放行

springboot缓存之缓存工作原理_spring_26

  1. @Cacheable  方法运行前,先去查询Cache(缓存组件),按照cacheNames指定的名字获取(CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自动创建
  2. 去Cache中查找缓存的内容,使用一个key,默认就是方法的参数;key是按照某种策略生成的,默认是使用keyGenerator生成的,默认使用SimpleKeyGenerator生成key
  3. 没有查到缓存就调用目标方法
  4. 将目标方法返回的结果放到缓存中

标签:缓存,springboot,cache,boot,autoconfigure,springframework,原理,ConcurrentMapCacheMana
From: https://blog.51cto.com/u_12528551/5900311

相关文章

  • springboot缓存之搭建redis环境
    Redis是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库\缓存和消息中间件.安装redis,使用dockerdockerpullregistry.docker-cn.com/library/redis运行re......
  • springboot缓存之整合redis
     一\引入redispom.xml中添加<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>......
  • springboot分布式之duddo简介+docker安装zookeeper
    dubbo:解决服务之间远程服务调用问题================================================================================================1-安装zookeeperdockerpullzoo......
  • springboot热部署之devtools开发热部署
    example: 修改接口此时target/classes中的文件依然是ctrl+F9后,target/class中的文件虽然做出修改,但是并未部署到浏览器中进入spring官网的springboot官方文档中的usings......
  • springboot检索之elasticsearch快速使用
    打开elasticsearch官网,docs,简体中文->Elasticsearch:权威指南   Elasticsearch是 面向文档 的,意味着它存储整个对象或 文档。Elasticsearch不仅存储文档,而且 ......
  • SpringBoot参数校验
    1.前言因为网络传输的不可靠性,以及前端数据控制的可篡改性,后端的参数校验是必须的,应用程序必须通过某种手段来确保输入进来的数据从语义上来讲是正确的。2.数据校验的......
  • SpringBoot整合ElasticSearch
    目录1SpringBoot整合ElasticSearch1.1pom.xml1.2创建高级客户端1.3基本用法1.3.1创建、判断存在、删除索引1.3.2对文档的CRUD1.3.3批量CRUD数据1.3.4查询所有、模......
  • springboot任务之异步任务
    1-新建工程,只选web模块2-新增service包,AsyncService类packagecom.example.springboottask.service;importorg.springframework.stereotype.Service;@Servicepublicclas......
  • springcloud之springboot自动装载
        ImportSelector接口是Spring导入外部配置的核心接口,在SpringBoot的自动配置和@EnableXXX(功能性注解)中起到了决定性的作用.当在@Configuration标注的Class......
  • springboot消息之使用RabbitTemplate给rabbitmq发送和接收消息&序列化机制
    1-引入spring-boot-starter-amqp2-application.yml配置3-测试RabbitMQ  1--AmqpAdmin:管理组件  2--RabbitTemplate:消息发送处理组件======================......