当前位置:网站首页>面试官: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)两种,当集合中的所有元素的成员长度较短并元素个数较少时,使用压缩列表作为内部实现,否则使用跳跃表和哈希表作为内部实现。当条件不满足时,压缩列表可以转换为跳跃表,但跳跃表不能转换为压缩列表。
竟然已经看到这里了,你我定是有缘人,留下你的点赞和关注,他日必成大器。
边栏推荐
- Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
- R language visualizes the relationship between more than two classification (category) variables, uses mosaic function in VCD package to create mosaic plots, and visualizes the relationship between tw
- Basic knowledge of lists
- recyclerview gridlayout 平分中间空白区域
- 【每周一坑】输出三角形
- [DSP] [Part 1] start DSP learning
- 1_ Introduction to go language
- Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
- The mail command is used in combination with the pipeline command statement
- Utilisation de l'écran OLED
猜你喜欢
![[wechat applet] operation mechanism and update mechanism](/img/cf/58a62a7134ff5e9f8d2f91aa24c7ac.png)
[wechat applet] operation mechanism and update mechanism

SQL injection 2

2022 nurse (primary) examination questions and new nurse (primary) examination questions

OAI 5G NR+USRP B210安装搭建

JMeter server resource indicator monitoring (CPU, memory, etc.)

【OpenCV 例程200篇】220.对图像进行马赛克处理

SAP UI5 框架的 manifest.json

Distributed ID

OLED屏幕的使用
![[weekly pit] calculate the sum of primes within 100 + [answer] output triangle](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
随机推荐
Use of OLED screen
【每周一坑】正整数分解质因数 +【解答】计算100以内质数之和
What programming do children learn?
看过很多教程,却依然写不好一个程序,怎么破?
What is the problem with the SQL group by statement
Xcode6 error: "no matching provisioning profiles found for application"
【OpenCV 例程200篇】220.对图像进行马赛克处理
PHP online examination system version 4.0 source code computer + mobile terminal
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
C language operators
什么是RDB和AOF
Huawei device command
Variable star --- article module (1)
User defined current limiting annotation
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
华为设备命令
Zoom with unity mouse wheel: zoom the camera closer or farther
[weekly pit] output triangle
Value of APS application in food industry
Leetcode hot topic Hot 100 day 32: "minimum coverage substring"