22 Jun 2024 |
- 抽取公关逻辑到接口command

它的实现类就是各个命令。

- 通过commands注册所有命令

- 通过Reply封装redis返回数据类型

包括整形、错误、简单字符串、复杂字符串和数组。

- LPUSH命令的实现
- 实现command接口
- 重写name方法
- 实现执行逻辑exec:获取参数和值,在缓存中实现具体逻辑,返回int类型结果。

在cache中lpush逻辑如下,借用LinkedList的addFirst实现。

底层使用Map作为缓存数据结构, CacheEntry封装值的类型


- LPOP命名的实现
- 实现command接口
- 重写name方法
- 实现执行逻辑exec:获取参数和值,在缓存中实现具体逻辑,返回bulkString类型结果。

在cache中缓存的具体逻辑实现如下,借用LinkedList的removeFirst实现。

- 测试
通过redis客户端都可以实现命令lpush/lpop/rpush/rpop/lindex/lrange/llen。

- REDIS 协议规范 https://redis.com.cn/topics/protocol.html在 RESP 中,数据的类型依赖于首字节:单行字符串(Simple Strings): 响应的首字节是 “+”错误(Errors): 响应的首字节是 “-“整型(Integers): 响应的首字节是 “:”多行字符串(Bulk Strings): 响应的首字节是”$“数组(Arrays): 响应的首字节是 “*“RESP协议的不同部分总是以 “\r\n” (CRLF**) 结束。
源码:https://github.com/midnight2104/midnight-cache
19 Jun 2024 |
- 自定义缓存MidnightCache

- 实现redis协议

integr编码处理

错误编码处理

数组编码处理

负责字符串处理

- 测试

- REDIS 协议规范 https://redis.com.cn/topics/protocol.html在 RESP 中,数据的类型依赖于首字节:单行字符串(Simple Strings): 响应的首字节是 “+”错误(Errors): 响应的首字节是 “-“整型(Integers): 响应的首字节是 “:”多行字符串(Bulk Strings): 响应的首字节是”$“数组(Arrays): 响应的首字节是 “*“RESP协议的不同部分总是以 “\r\n” (CRLF**) 结束。
源码:https://github.com/midnight2104/midnight-cache
18 Jun 2024 |
实现原理如下:

- 定义插件接口
- init初始化方法;
- startup启动方法;
- shutdown停止方法;

- 实现CacheServer
- 声明为bean
- 定义端口6379;
- 定义boss和worker

在startup方法中定义netty server,添加CacheHandle和CacheDecoder

在shutdown方法中,关闭channel,boss和worker

- 实现CacheDecoder
继承ByteToMessageDecoder,从ByteBuf中读取输入,转成String输出

- 实现CacheHandle
继承SimpleChannelInboundHandler,重写channelRead0方法。

在channelRead0方法,读取参数,解析命令,支持命令COMMAND,PING,INFO。*和$都是redis协议规范。

解析命令后,构造输出。

- 实现MidnightApplicationListener
事件ApplicationReadyEvent启动插件,完成初始化。ContextClosedEvent事件关闭插件。

- 测试
启动redis client,命令都可以正常解析。

源码:https://github.com/midnight2104/midnight-cache