Redis内部数据结构详解之双向链表(linkedlist)
一、双向链表简介 双向链表作为一种常见的数据结构,在严蔚敏数据结构书里有详细的讲解,双向链表的每个数据节点都有两个指针,分别指向后继与前驱节点,因此从双向链表中的任意一个节点开始都可以很方便地访问其前驱与后继节点。 二、Redis中双向链表数据结
一、双向链表简介
双向链表作为一种常见的数据结构,在严蔚敏数据结构书里有详细的讲解,双向链表的每个数据节点都有两个指针,分别指向后继与前驱节点,因此从双向链表中的任意一个节点开始都可以很方便地访问其前驱与后继节点。
二、Redis中双向链表数据结构以及相关宏定义
typedef struct listNode { struct listNode *prev;//前驱指针 struct listNode *next;//后继指针 void *value; //节点的值 } listNode; typedef struct listIter {//链表迭代器 listNode *next; int direction;//遍历方向 } listIter; typedef struct list {//链表 listNode *head;//链表头 listNode *tail;//链表尾 void *(*dup)(void *ptr); //复制函数指针 void (*free)(void *ptr); //释放内存函数指针 int (*match)(void *ptr, void *key); //比较函数指针 unsigned long len; //链表长度 } list;
宏名称 |
作用 |
listLength |
获取链表的长度值 |
listFirst |
获取链表的首指针 |
listLast |
获取链表的尾指针 |
listPrevNode |
获取当前节点的前驱节点指针 |
listNextNode |
获取当前节点的后继节点指针 |
listNodeValue |
获取当前节点所存储的值 |
listSetDupMethod |
设置链表节点value的复制函数 |
listSetFreeMethod |
设置链表节点value的释放内存函数 |
listSetMatchMethod |
设置链表节点value的比较函数 |
listGetDupMethod |
获取链表节点value的复制函数 |
listGetFree |
获取链表节点value的释放内存函数 |
listGetMatchMethod |
获取链表节点value的比较函数 |
三、Redis中双向链表API介绍
名称 |
作用 |
复杂度 |
listCreate |
创建一个新双向链表 |
O(1) |
listRelease |
释放一个双向链表以及包含的节点内存 |
O(N) |
listAddNodeHead |
将一个节点添加到链表的表头 |
O(1) |
listAddNodeTail |
将一个节点添加到链表的表尾 |
O(1) |
listInsertNode |
将一个节点添加到给定节点的之后或之前 |
O(1) |
listDelNode |
删除给定的节点 |
O(1) |
listGetIterator |
生成双向链表的迭代器 |
O(1) |
listReleaseIterator |
释放双向链表的迭代器 |
O(1) |
listNext |
通过迭代器获取下一个节点 |
O(1) |
listDup |
创建给定链表的副本 |
O(N) |
listSearchKey |
查找与给定key相同值的节点 |
O(N) |
listIndex |
根据给定的索引值,返回相应的节点 |
O(N) |
listRewind |
重新初始化迭代器,迭代方向从头至尾 |
O(1) |
listRewindTail |
重新初始化迭代器,迭代方向从尾至头 |
O(1) |
listRotate |
取出链表尾节点并插入到头部 |
O(1) |
四、Redis双向链表性能分析
Redis中的双向链表也许是Redis中最简单最容易实现的数据结构,对于API就不多说了,都很简单,也没啥可以说的,下面简单分析一下双向链表的性能。
listNode拥有prev前驱指针和next后继指针,因此通过迭代器可以很方便的对链表从从头至尾或从尾至头遍历;
list拥有header头指针和tail为指针,对于在链表的头部或尾部进行插入节点的时间复杂度全部为O(1),高效地实现了Redis中一些指令的操作;
list自带保存链表长度的字段len,使得计算链表长度的时间复杂度为O(1)。
五、小结
双向链表主要有两个作用:作为Redis列表数据类型的底层实现方法之一;作为通用数据结构可以被其他功能模块使用。
双向链表实现简单,Redis对双向链表加以改造,添加保存节点长度的字段,以及实现自己的迭代指针,使得一些数据操作变得简单。
最后感谢黄健宏(huangz1990)的Redis设计与实现及其他对Redis2.6源码的相关注释对我在研究Redis2.8源码方面的帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
