关于代码评审

很多团队都有这样的问题,开发人员水平不一,想法不同,代码各有各的风格,Review起来又费时费力,感觉很难让所有成员保持统一风格。所以参考他人的一些经验,也总结了一些关于代码质量管理与提升的思考。

Redis缓存常见问题

引入缓存是一种非常常见的策略,可以提高上层的查询性能,同时减轻下层存储的压力。但同时也引入了更多的问题,主要围绕命中率一致性两个方面,如果命中率过低,那么缓存就形同虚设,如果不能保证缓存与真实数据一致,那么就会造成错误,不如不用缓存。下面主要总结下使用redis作为缓存时常见的问题和解决办法

Netty NioEventLoop

Java Nio是对多路复用IO模型的实现,其核心在于借助操作系统提供的选择器实现对连接的监听,然后应用服务只需对就绪的连接事件进行处理即可,比如可读可写等,这样就降低了应用服务在并发场景下的线程资源压力。

Netty中设计了专门的NioEventLoop来封装对Nio事件的处理,其思路是给每个NioEventLoop初始化一个选择器Selector,然后将通道对Selector的注册转化成对NioEventLoop的注册,这样可以让每个NioEventLoop各司其职,只负责处理注册到自己身上的通道事件,从而即实现了通道事件的多线程处理,同时又避免了过程中的线程安全问题。

Netty Future & Promise

在Java中,对于异步操作结果的获取抽象出了一个接口Future<V>,它提供了一些方法来检查或等待操作是否完成,以及获取操作的结果,同时还提供了取消操作的能力,以及提供了检测操作是正常结束还是被取消的方法。不过如果操作已经完成了,就不能再对其进行取消了。在某些场景下,如果只是想借助Future实现操作取消的能力,而不关心操作结果,那么可以返回空即可。

A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled. If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future<?> and return null as a result of the underlying task.