Redis学习笔记(六)基础数据结构
2021-01-14 本文已影响0人
那些年搬过的砖
一、String键值对

二、List——相当于java中的LinkedList

插入删除时间复杂度O(1)
查询时间复杂度O(n)
实际上redis内部并不是一个简单的linkedlist,而是由多个具有双向指针的ziplist组成的quicklist,ziplist是一块连续的内存空间。

三、Hash——相当于java中的HashMap

四、set(无序集合)
set具有去重功能

五、zset(有序集合)相当于java中SortedSet和HashMap的结合
1、具有set的特性,即保证了value的唯一性
2、具备按score排序的特性
比如zet可以按下图存储粉丝列表,value代表某用户的粉丝,score表示关注时间。

底层数据结构
假设要把华山论剑各路英雄的武力排行存放到跳跃表中,则存储结构可能是下图这样。特性如下
1、所有元素都存储在L0层
2、部分元素会被抽取到L1层(随机50%)组成稀疏索引
3、部分元素被抽取到L2层(随机25%)组成更稀疏的索引
4、Redis总共设计了32层(最顶层是L31),越往上索引越稀疏

元素插入和删除时,会先在最上一层查找,然后根据范围依次往下一层下潜,直到找到L0层的具体的位置。