博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot Cache 深入
阅读量:7126 次
发布时间:2019-06-28

本文共 2192 字,大约阅读时间需要 7 分钟。

这上一篇文章中我们熟悉了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

    @Beanpublic ConcurrentMapCacheManager cacheManager() {	ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();	List
    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这三个注解组合在一起,增加了灵活性,使用与之前类似,就不展开了复制代码

如理解有误,请指正

转载地址:http://naael.baihongyu.com/

你可能感兴趣的文章
基于POLARDB数据库的压测实践
查看>>
通过工具SecureCRTPortable将项目部署到服务器上
查看>>
利用QRCode实现待logo的二维码的创建
查看>>
【云周刊】第190期:阿里云超算揭秘:虚拟机的心脏,物理机的肌肉
查看>>
崩溃bug日志总结3
查看>>
推荐一个有趣的Chrome扩展程序-查看任意网站的开发技术栈
查看>>
shell技巧5 - 全自动打包ipa
查看>>
uC/OS-II源码分析(六)
查看>>
阿里、美团、网易、华为等二十厂秋招Java面经大合集
查看>>
为什么说,“景区”AI 改造势在必行
查看>>
第十八章:MVVM(二)
查看>>
进程调度(二)
查看>>
python元组,集合类型,及字典补充
查看>>
9、python函数进阶
查看>>
Markdown一看就会
查看>>
dotweb——go语言的一个微型web框架(一)
查看>>
又是一个名叫草泥马的项目:thefuck
查看>>
《七周七并发模型》作者Paul Butcher访谈问题有奖征集
查看>>
linux基本功能的一些命令(用户,系统信息,包管理等)
查看>>
使用 dnscrypt-proxy 防止 dns 污染
查看>>