当前位置:网站首页>面试官:Redis中有序集合的内部实现方式是什么?
面试官:Redis中有序集合的内部实现方式是什么?
2022-07-06 12:45:00 【51CTO】
面试官:Redis中基本的数据类型有哪些?
我:Redis的基本数据类型有:字符串(string)、哈希(hash)、列表(list)、集合(set)、有序集合(zset)。
面试官:有序集合的内部实现方式是什么?
我还沉浸在上一个问题的沾沾自喜中,顿时表情凝固了,手心开始冒出冷汗。“这个。。没有太深入了解”,我支支吾吾的说到。
面试官:回去等消息吧。
这句话说的干净利落,然后就没有然后了。失败是成功的妈妈,我不气馁,决定马上恶补一下。
有序集合的内部实现
有序集合的内部实现有两种,分别是:压缩列表(ziplist)和跳跃表(skiplist)。接下来,我们分别进行详细的了解。
以压缩列表作为内部实现
当有序集合的元素个数小于zset-max-ziplist-entries
(默认为128个),并且每个元素成员的长度小于zset-max-ziplist-value
(默认为64字节)的时候,使用压缩列表作为有序集合的内部实现。
每个集合元素由两个紧挨在一起的两个压缩列表结点组成,其中第一个结点保存元素的成员,第二个结点保存元素的分支。压缩列表中的元素按照分数从小到大依次紧挨着排列,有效减少了内存空间的使用。
举个例子,我们使用zadd
命令创建一个以压缩列表为实现的有序集合:
以跳跃表作为内部实现
当有序集合的元素个数大于等于zset-max-ziplist-entries
(默认为128个),或者每个元素成员的长度大于等于zset-max-ziplist-value
(默认为64字节)的时候,使用跳跃表作为有序集合的内部实现。
此时,在有序集合中其实包含了两个结构,一个是跳跃表,另一个是哈希表。
在跳跃表中,所有元素按照从小到大的顺序排列。跳跃表的结点中的object
指针指向元素成员的字符串对象,score
保存了元素的分数。通过跳跃表,Redis可以快速地对有序集合进行分数范围、排名等操作。
在哈希表中,为有序集合创建了一个从元素成员到元素分数的映射。键值对中的键指向元素成员的字符串对象,键值对中的值保存了元素的分数。通过哈希表,Redis可以快速查找指定元素的分数。
虽然有序集合同时使用跳跃表和哈希表,但是这两种数据结构都使用指针共享元素中的成员和分数,不会额外的内存浪费。
举个例子,我们使用zadd
命令创建一个以跳跃表为实现的有序集合:
内部实现的转换
当一个有序集合是以压缩列表作为内部实现时,再向这个有序集合添加较长的元素成员,或向这个有序集合的元素个数过多时,那么这个有序集合就会转换为以跳跃表作为内部实现。但是,以跳跃表作为内部实现的有序集合不会转换为以压缩列表作为内部实现。
举个例子,我们先创建一个以压缩列表作为内部实现的有序集合:
然后,再向它添加一个较长成员的元素,它就是转换为以跳跃表作为内部实现:
然后,再把那一个较长成员的元素从有序集合中移除,有序集合依然是以跳跃表作为内部实现:
总结
在Redis中,有序集合的内部实现有压缩列表(ziplist)和跳跃表(skiplist)两种,当集合中的所有元素的成员长度较短并元素个数较少时,使用压缩列表作为内部实现,否则使用跳跃表和哈希表作为内部实现。当条件不满足时,压缩列表可以转换为跳跃表,但跳跃表不能转换为压缩列表。
竟然已经看到这里了,你我定是有缘人,留下你的点赞和关注,他日必成大器。
边栏推荐
- 7、数据权限注解
- Entity alignment two of knowledge map
- I've seen many tutorials, but I still can't write a program well. How can I break it?
- Force deduction brush question - 98 Validate binary search tree
- Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
- Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
- Review questions of anatomy and physiology · VIII blood system
- Mtcnn face detection
- Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
- JMeter server resource indicator monitoring (CPU, memory, etc.)
猜你喜欢
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
Maximum likelihood estimation and cross entropy loss
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
01 basic introduction - concept nouns
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
Why do novices often fail to answer questions in the programming community, and even get ridiculed?
Event center parameter transfer, peer component value transfer method, brother component value transfer
Logic is a good thing
Web security - payload
随机推荐
Unity load AB package
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
Utilisation de l'écran OLED
OAI 5G NR+USRP B210安装搭建
7、数据权限注解
SAP UI5 框架的 manifest.json
No Yum source to install SPuG monitoring
Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
Detailed explanation of knowledge map construction process steps
APS taps home appliance industry into new growth points
[weekly pit] information encryption + [answer] positive integer factorization prime factor
JMeter server resource indicator monitoring (CPU, memory, etc.)
Review questions of anatomy and physiology · VIII blood system
Manifest of SAP ui5 framework json
[200 opencv routines] 220 Mosaic the image
Unity makes AB package
Huawei device command
Regular expression collection
Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
华为设备命令