当前位置:网站首页>快速整明白Redis中的压缩列表到底是个啥
快速整明白Redis中的压缩列表到底是个啥
2022-07-01 12:36:00 【51CTO】
压缩列表简介
压缩列表(ziplist)是由一个连续内存组成的顺序型数据结构。一个压缩列表可以包含任意多个节点,每个节点上可以保存一个字节数组或整数值。它是Redis为了节省内存空间而开发的。
压缩列表(ziplist)是哈希(hash)和有序集合(zset)的内部编码之一。当哈希(hash)中的元素个数比较少并且每个元素的值占用空间比较小的时候,Redis就会使用压缩列表做为哈希的内部编码。当有序集合(zset)中的元素个数比较少并且每个元素的值占用空间比较小的时候,Redis也会使用压缩列表做为有序集合的内部编码。
压缩列表结构
接下来,我们来看以下压缩列表的内部构造,压缩列表由以下几个部分组成:
zlbytes:表示整个压缩列表占用的内存字节数。xltail:表示压缩列表起始地址到最后一个节点的字节数,可以快速找到最后一个节点。zllength:表示压缩列表包含的节点个数。entries:节点列表,一个挨着一个地紧凑存储。zlend:特殊值0xFF(十进制为255),表示压缩列表的结束。
压缩列表节点结构
每个压缩列表的节点由三部分组成: prevlen、 encoding和content。
prevlen
prevlen:表示该节点前一个节点的字节长度。 prevlen的长度可能是1个字节,也可能是5个字节。
当前一个节点的长度小于254个字节时, prevlen的长度为1个字节,直接存储前一个节点的字节长度;
当前一个节点的长度大于或等于254个字节时, prevlen的长度为5个字节,其中的第一个字节被设置为0xFE,随后的四个字节保存前一个节点的字节长度。
可以通过 prevlen和压缩列表结构中的xltail逆序遍历压缩列表。
encoding
encoding表示该节点中保存数据的类型和长度。
当encoding的最高位以00开头时,表示最大长度为63的短字符串,此时encoding的长度为1个字节,其后面6个位表示字符串的字节长度;
当encoding的最高位以01开头时,表示最大长度为16383的中等长度的字符串,此时encoding的长度为2个字节,其后面14个位表示字符串的字节长度;
当encoding的最高位以10开头时,表示最大长度为4294967295的特长的字符串,此时encoding的长度为5个字节,其后面4个字节表示字符串的字节长度;
当encoding的最高位以11开头时,表示整数值,此时encoding的长度为1个字节,其后面6个位表示整数值的类型和长度。
content
content用于存储节点的值,节点的值可以是一个字节数组,也可以是正数,其类型和长度由encoding决定。
总结
压缩列表(ziplist)是由一个连续内存组成的顺序型数据结构。
一个压缩列表可以包含任意多个节点,每个节点上可以保存一个字节数组或整数值。
压缩列表(ziplist)是哈希(hash)和有序集合(zset)的内部编码之一。
竟然已经看到这里了,你我定是有缘人,留下你的点赞和关注,他日必成大器。
边栏推荐
- Accept different views with an open mind
- 阿霍的三个阶段
- Understanding of NAND flash deblocking
- System test UI test summary and questions (interview)
- 【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告
- Double linked list related operations
- Onenet Internet of things platform - mqtt product equipment upload data points
- Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
- 6.30模拟赛总结
- CPI tutorial - asynchronous interface creation and use
猜你喜欢

Queue operation---

栈-------

ROS2 Foxy depthai_ros教程

A hole in solder paste

工具箱之 IKVM.NET 项目新进展

Four years after graduation: work, resign, get married, buy a house

How to use opcache, an optimization acceleration component of PHP

第十四章 信号(四)- 多进程任务示例

JS reverse | m3u8 data decryption of a spring and autumn network

Typora adds watermarks to automatically uploaded pictures
随机推荐
codeforces -- 4B. Before an Exam
Need your own cognition
Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
【20211129】Jupyter Notebook遠程服務器配置
微信小程序 – 80个实用的微信小程序项目实例
[some notes]
redis探索之缓存一致性
类的初始化与实例化
JS reverse | m3u8 data decryption of a spring and autumn network
Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
ASTM D 3801 vertical burning test of solid plastics
腾讯总考epoll, 很烦
Mobile note application
[20211129] configuration du serveur distant du carnet de notes jupyter
How to install php7 and perform performance test using yum
The difference between memcpy and strcpy
ROS2 Foxy depthai_ros教程
【datawhale202206】pyTorch推荐系统:召回模型 DSSM&YoutubeDNN
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE