如何进行高效的源码阅读:以Spring Cache扩展为例带你搞清楚
摘要
日常开发中,需要用到各种各样的框架来实现API、系统的构建。作为程序员,除了会使用框架还必须要了解框架工作的原理。这样可以便于我们排查问题,和自定义的扩展。那么如何去学习框架呢。通常我们通过阅读文档、查看源码,然后又很快忘记。始终不能融汇贯通。本文主要基于Spring Cache扩展为例,介绍如何进行高效的源码阅读。
SpringCache的介绍
为什么以Spring Cache为例呢,原因有两个
- Spring框架是web开发最常用的框架,值得开发者去阅读代码,吸收思想
- 缓存是企业级应用开发必不可少的,而随着系统的迭代,我们可能会需要用到内存缓存、分布式缓存。那么Spring Cache作为胶水层,能够屏蔽掉我们底层的缓存实现。
一句话解释Spring Cache: 通过注解的方式,利用AOP的思想来解放缓存的管理。
step1 查看文档
首先通过查看官方文档,概括了解Spring Cache
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html
重点两点
- 两个接口抽象
Cache,CacheManager,具体的实现都是基于这两个抽象实现。
典型的SPI机制,和eat your dog food。当需要提供接口给外部调用,首先自己内部的实现也必须基于同样一套抽象机制
The cache abstraction does not provide an actual store and relies on abstraction materialized by the org.springframework.cache.Cache and org.springframework.cache.CacheManager interfaces.
Spring Cache提供了这些缓存的实现,如果没有一种
CacheManage,或者CacheResolver,会按照指定的顺序去实现If you have not defined a bean of type CacheManager or a CacheResolver named cacheResolver (see CachingConfigurer), Spring Boot tries to detect the following providers (in the indicated order):
1.Generic
2.JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others)
3.EhCache 2.x
4.Hazelcast
5.Infinispan
6.Couchbase
7.Redis
8.Caffeine
9.Simple
step2 run demo
对Spring Cache有了一个大概的了解后,我们首先使用起来,跑个demo。
定义一个用户查询方法
@Component
public class CacheSample {
@Cacheable(cacheNames = "users")
public Map<Long, User> getUser(final Collection<Long> userIds) {
System.out.println("not cache");
final Map<Long, User> mapUser = new HashMap<>();
userIds.forEach(userId -> {
mapUser.put(userId, User.builder().userId(userId).name("name").build());
});
return mapUser;
}
配置一个CacheManager
@Configuration
public class CacheConfig {
@Primary
@Bean(name = { "cacheManager" })
public CacheManager getCache() {
return new ConcurrentMapCacheManager("users");
}
API调用
@RestController
@RequestMapping("/api/cache")
public class CacheController {
@Autowired
private CacheSample cacheSample;
@GetMapping("/user/v1/1")
public List<User> getUser() {
return cacheSample.getUser(Arrays.asList(1L,2L)).values().stream().collect(Collectors.toList());
}
}
step3 debug 查看实现
demo跑起来后,就是debug看看代码如何实现的了。
因为直接看源代码的,没有调用关系,看起来会一头雾水。通过debug能够使你更快了解一个实现。
通过debug我们会发现主要控制逻辑是在切面CacheAspectSupport
会先根据cache key找缓存数据,没有的话put进去。
step4 实现扩展
知道如何使用Spring Cache后,我们需要进一步思考,就是如何扩展。那么带着问题出发。
比如Spring Cache不支持批量key的缓存,像上文我们举的例子,我们希望缓存的key是userId,而不是Collection userIds。以userId为key,这样的缓存命中率更高,存储的成本更小。
@Cacheable(cacheNames = "users")
public Map<Long, User> getUser(final Collection<Long> userIds) {}
所以我们要实现对Spring Cache进行扩展。step3中我们已经大致了解了Spring Cache的实现。那么实现这个扩展的功能就是拆分Collection userIds,缓存命中的从缓存中获取,没有命中的,调用源方法。
@Aspect
@Component
public class CacheExtenionAspect {
@Autowired
private CacheExtensionManage cacheExtensionManage;
/**
* 返回的结果中缓存命中的从缓存中获取,没有命中的调用原来的方法获取
* @param joinPoint
* @return
*/
@Around("@annotation(org.springframework.cache.annotation.Cacheable)")
@SuppressWarnings("unchecked")
public Object aroundCache(final ProceedingJoinPoint joinPoint) {
// 修改掉Collection值,cacheResult需要重新构造一个
args[0] = cacheResult.getMiss();
try {
final Map<Object, Object> notHit = CollectionUtils.isEmpty(cacheResult.getMiss()) ? null
: (Map<Object, Object>) (method.invoke(target, args));
final Map<Object, Object> hits = cacheResult.getHit();
if (Objects.isNull(notHit)) {
return hits;
}
// 设置缓存
cacheResult.getCache().putAll(notHit);
hits.putAll(notHit);
return hits;
}
}
然后扩展Cache,CacheManage
重写Cache的查找缓存方法,返回新的CacheResult
public static Object lookup(final CacheExtension cache, final Object key) {
if (key instanceof Collection) {
final Collection<Object> originalKeys = ((Collection) key);
if (originalKeys == null || originalKeys.isEmpty()) {
return CacheResult.builder().cache(cache).miss(
Collections.emptySet())
.build();
}
final List<Object> keys = originalKeys.stream()
.filter(Objects::nonNull).collect(Collectors.toList());
final Map<Object, Object> hits = cache.getAll(keys);
final Set<Object> miss = new HashSet(keys);
miss.removeAll(hits.keySet());
return CacheResult.builder().cache(cache).hit(hits).miss(miss).build();
}
return null;
}
CacheResult就是新的缓存结果格式
@Builder
@Setter
@Getter
static class CacheResult {
final CacheExtension cache;
// 命中的缓存结果
final Map<Object, Object> hit;
// 需要重新调用源方法的keys
private Set<Object> miss;
}
然后扩展CacheManager,没什么重写,就是自定义一种manager类型
为缓存指定新的CacheManager
@Primary @Bean public CacheManager getExtensionCache() { return new CacheExtensionManage("users2"); }
完整代码
https://github.com/FS1360472174/javaweb/tree/master/web/src/main/java/com/fs/web/cache
总结
本文主要介绍一种源码学习方法,纯属抛砖引玉,如果你有好的方法,欢迎分享。
如何进行高效的源码阅读:以Spring Cache扩展为例带你搞清楚的更多相关文章
- 以Spring Cache扩展为例介绍如何进行高效的源码的阅读
摘要 日常开发中,需要用到各种各样的框架来实现API.系统的构建.作为程序员,除了会使用框架还必须要了解框架工作的原理.这样可以便于我们排查问题,和自定义的扩展.那么如何去学习框架呢.通常我们通过阅读 ...
- (转) Spring源码阅读 之 Spring整体架构
标签(空格分隔): Spring 声明:本文系转载,原地地址:spring framework 4 源码阅读 Spring骨架 Spring的骨架,也是Spring的核心包.主要包含三个内容 cont ...
- Three.js源码阅读笔记-5
Core::Ray 该类用来表示空间中的“射线”,主要用来进行碰撞检测. THREE.Ray = function ( origin, direction ) { this.origin = ( or ...
- JDK 1.8源码阅读 TreeMap
一,前言 TreeMap:基于红黑树实现的,TreeMap是有序的. 二,TreeMap结构 2.1 红黑树结构 红黑树又称红-黑二叉树,它首先是一颗二叉树,它具体二叉树所有的特性.同时红黑树更是一颗 ...
- 【源码阅读】Java集合之三 - ArrayDeque源码深度解读
Java 源码阅读的第一步是Collection框架源码,这也是面试基础中的基础: 针对Collection的源码阅读写一个系列的文章,本文是第三篇ArrayDeque. ---@pdai JDK版本 ...
- java源码阅读Hashtable
1类签名与注释 public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, C ...
- 源码阅读之HashMap(JDK8)
概述 HashMap根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的. HashMap最多只允许一条记录的键为null,允许多条记录 ...
- 源码阅读之LinkedList(JDK8)
inkedList概述 LinkedList是List和Deque接口的双向链表的实现.实现了所有可选列表操作,并允许包括null值. LinkedList既然是通过双向链表去实现的,那么它可以被当作 ...
- JDK部分源码阅读与理解
本文为博主原创,允许转载,但请声明原文地址:http://www.coselding.cn/article/2016/05/31/JDK部分源码阅读与理解/ 不喜欢重复造轮子,不喜欢贴各种东西.JDK ...
随机推荐
- linux服务脚本编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Directory.GetFiles()获取多个类型格式的文件
第一种(用通配符) string[] fileNameX = Directory.GetFiles(@"D:\Sjdc", "*.xls?"); Array a ...
- Application.StartupPath和System.Environment.CurrentDirectory的区别
System.Environment.CurrentDirectory的含义是获取或设置当前工作路径,而Application.StartupPath是获取程序启动路径,表面上看二者没什么区别,但实际 ...
- oracle利用透明网关访问mssql
遇到一个客户,有个需求,想将mssql中的数据抽取到oracle中.经过上网查找,感觉gateway这个工具可以实现,因此就搭建实验环境进行测试.首先在oracle delivery上面下载对应的安装 ...
- QT中的各种对话框
大家可以参见QT中各种MessageBox的使用的这篇文章 界面效果图如下,大家可以用代码自己操作 diglog.h #ifndef DIALOG_H #define DIALOG_H #includ ...
- c# 9png实现(图片缩放)
跟据9png的实现原理自己写了个生成图片的函数,9png的原理是将图片切成9块如下 其中1.3.7.9不进行缩放,2,4,5,6,8进行缩放,这样就防止了放大后导致边界出现锯齿的问题 在实现过程中主要 ...
- 【Linux】Linux下设备网卡以及硬件管理等
这是Linux下网络硬件管理的基础知识,虽然平时用到的可能比软件的少一点,但是作为基础命令,还是需要记住,以免用时又得查询. 本文参考官方文档:https://wiki.ubuntu.com.cn/% ...
- Codility---Brackets
Task description A string S consisting of N characters is considered to be properly nestedif any of ...
- CI控制器
当控制器要继承自定义的控制器的时候,有特定的定义: application/core/MY_Controller <?php class MY_Controller extends CI_Con ...
- [MFC.Windows程序设计(第2版) 第一章
1,windows编程模型如下图: 2, windows的消息有成百上千种,以下列举10个: 3,消息处理函数的四个参数:窗口句柄(表示消息属于哪个窗口,32值.该窗口句柄引用一个数据结构,数据结构存 ...