redis数据类型:list

数据结构

  • 源码版本:7.2.2
  • 路径:src/adlist.h

关于list的 头文件中涉及到的这三个结构体如下

/* Node, List, and Iterator are the only data structures used currently. */
# 节点
typedef struct listNode {struct listNode *prev; # 前元素的指针struct listNode *next; # 后元素的指针void *value; # 节点的值
} listNode;
# 遍历
typedef struct listIter {listNode *next; 相邻节点的指针int direction; # 遍历方向
} listIter;
# list
typedef struct list {listNode *head; # 链表的头指针listNode *tail; # 链表的尾指针void *(*dup)(void *ptr); # Duplicate .void (*free)(void *ptr); # 释放资源int (*match)(void *ptr, void *key); # 匹配unsigned long len; # list 的长度
} list;
双向链表图示
next
next
next
prev
prev
prev
node
node
node
node

简介

Redis lists are linked lists of string values. Redis lists are frequently used to:

  • Implement stacks and queues.
  • Build queue management for background worker systems.

list 结构示意图,可参考 python 中的 list,特别是范围查找逻辑

在这里插入图片描述

list 类型支持双向查找:

  • 正序查找:如 0 到 5,从头开始算起,
  • 负序查找:如 0 到 -5,

通常用于实现栈和队列

  • 栈:先进后出,使用 list 的 lpush 可以实现栈的功能,依次向前插入数据,按正序索引取值的时候,可以实现栈的功能。
  • 队列:先进先出,使用 rpush可以实现队列的功能,依次向后插入数据,按正序索引取值的时候,可以实现队列的功能。

The max length of a Redis list is 2 32 − 1 2^{32} - 1 2321 (4,294,967,295) elements.

list 的相关命令配合使用的应用场景:

  • 栈和队列:插入和弹出命令的配合,亦可实现栈和队列的功能
    实现哪种数据结构,取决于插入和弹出命令的配合,如
  • 左插右出右插左出:这两种种方式实现先进先出的数据结构,即异侧配合可以实现队列结构。
  • 左插左出或者右插右出:这两种方式可以实现先进后出的数据结构,即同侧配合可以实现栈结构。
  • 消息队列:实现发布与订阅模型。
    • 生产者使用尾部插入命令RPUSH 将消息插入 list; 消费者使用LPOP 命令从 list 的左边消费消息
    • 生产者使用尾部插入命令LPUSH 将消息插入 list; 消费者使用RPOP 命令从 list 的左边消费消息
  • 限流:
    • 每次请求时向 List 添加时间戳,通过检查 List 长度来决定是否允许新的请求,实现 API 请求频率控制。
  • 缓存记录:聊天记录、文章推送,热点数据等。

命令

插入元素

  • 头部插入LPUSH adds a new element to the head of a list;
LPUSH key element [element ...]

该命令会向list 的头部依次插入给定的元素,在读取该命令插入数据的时候,就像在使用栈一样,元素先进后出。

  • 逐次插入元素示意图,这种方式可以将 list 当栈来使用
127.0.0.1:6379> lpush block jj1
(integer) 1
127.0.0.1:6379> lindex block 0
"jj1"
127.0.0.1:6379> lpush block jj2
(integer) 2
127.0.0.1:6379> lindex block 0
"jj2"
127.0.0.1:6379> 

在这里插入图片描述

  • 批量插入元示意图,取出元素的时候,可以发现符合栈的结构特点:先进后出。
127.0.0.1:6379> lpush block jj1 jj2 jj3 jj4 jj5
(integer) 5

上述命令执行逻辑是,元素依次入栈。jj1 将位于栈低,jj5将位于栈顶。

127.0.0.1:6379> lrange block 0 5
1) "jj5"
2) "jj4"
3) "jj3"
4) "jj2"
5) "jj1"

上述命令执行结果存储示意图

在这里插入图片描述

  • 尾部插入RPUSH adds to the tail.
RPUSH key element [element ...]

该命令会向list 的尾部依次插入给定的元素,在读取该命令插入数据的时候,就像在使用队列一样,元素先进先出。

127.0.0.1:6379> rpush block1 jj1 jj2 jj3 jj4 jj5
(integer) 5
127.0.0.1:6379> lindex block1 0
"jj1"
127.0.0.1:6379> 

查找元素的索引

LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
  • RANK rank : 排名,次序
    • 假设 list 中有多个 a,则 rank 2 表示查找 第二个 a 出现的位置,可参考案例
    • rank :可以是赋值,表示倒数第一个, rank -1 表示倒数第一个

The command returns the index of matching elements inside a Redis list. By default, when no options are given, it will scan the list from head to tail, looking for the first match of “element”. If the element is found, its index (the zero-based position in the list) is returned. Otherwise, if no match is found, nil is returned.

  1. 该命令返回 Redis 列表中匹配元素的索引。
  2. 默认情况下,如果没有给出任何选项,它将从头到尾扫描列表,寻找第一个匹配的“元素”。
  3. 如果找到元素,则返回其索引(列表中从零开始的位置)。否则,如果未找到匹配项,则返回 nil。
  • 查看现有数据
127.0.0.1:6379> lrange block 0 8
1) "jj5"
2) "a"
3) "jj4"
4) "a"
5) "a"
6) "jj3"
7) "jj2"
8) "jj1"
127.0.0.1:6379>
  • 查找第一个 a 所在的索引(位置): rank n 表示第一个,
    • n为正序:表示正数第几个
    • n为负数:表示倒数第几个:
127.0.0.1:6379> lpos block a
(integer) 1
127.0.0.1:6379> lpos block a rank 1
(integer) 1
127.0.0.1:6379> lpos block a rank -1  # 倒数第1个 a的位置
(integer) 4
127.0.0.1:6379> lpos block a rank -2  # 倒数第2个 a的位置
(integer) 3
127.0.0.1:6379>
  • 返回前n 个 指定元素所在的位置,如返回 list 中前两个 a 的位置:
127.0.0.1:6379> lpos block a count 2 # 前2 个所在的位置
1) (integer) 1
2) (integer) 3
127.0.0.1:6379>

元素的个数

  • LLEN returns the length of a list.

Returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned. An error is returned when the value stored at key is not a list.

返回按键存储的列表的长度。

  1. 如果键不存在,则将其解释为空列表并返回0。
  2. 如果键上存储的值不是列表,则返回错误。
  • 查看 block元素的个数
127.0.0.1:6379> llen block
(integer) 5
127.0.0.1:6379>
  • 查看所有的键
127.0.0.1:6379> keys *
1) "coinmarketapikey"
2) "block"
3) "exchangerate"
4) "apikeyexceeded"
5) "mysite"
127.0.0.1:6379>
  • 查看不存在的键的元素个数,返回 0
127.0.0.1:6379> llen block1
(integer) 0
127.0.0.1:6379>
  • 使用此命令查看 hash 类型:命令错误的使用方式
127.0.0.1:6379> llen mysite
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379>

删除元素

  • 头部移除LPOP removes and returns an element from the head of a list;

Removes and returns the first elements of the list stored at key.

By default, the command pops a single element from the beginning of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list’s length.

  1. 移除并返回 list 的第一个元素。
  2. 如果给定了个数 count,则会从 list前面移除指定个数的元素。
LPOP key [count]
  • 尾部移除RPOP does the same but from the tails of a list.
LPOP key [count]

Removes and returns the last elements of the list stored at key.

By default, the command pops a single element from the end of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list’s length.

  1. 移除并返回 list 的最后一个元素。
  2. 如果给定了个数 count,则会从 list后面移除指定个数的元素。

移走元素

LMOVE atomically moves elements from one list to another.

LMOVE source destination <LEFT | RIGHT> <LEFT | RIGHT>
  • <LEFT | RIGHT>
    • 第一个选项,指要移出位于source 的头部|尾部的元素
    • 第二个选项,从source 移除的元素要插入 destination 的位置头部|尾部

Atomically returns and removes the first/last element (head/tail depending on the wherefrom argument) of the list stored at source, and pushes the element at the first/last element (head/tail depending on the whereto argument) of the list stored at destination.

原子地返回并删除存储在源中的列表的第一个/最后一个元素(head/tail 取决于 where 参数) ,并将元素推送到存储在目的地的列表的第一个/最后一个元素(head/tail 取决于 where 参数)。

For example: consider source holding the list a,b,c, and destination holding the list x,y,z. Executing LMOVE source destination RIGHT LEFT results in source holding a,b and destination holding c,x,y,z.

举例说明:假设现有两个 list:

  • key 为 source 值为 a,b,c,
  • key 为 destination 值为 x,y,z

执行移走命令:

LMOVE source destination RIGHT LEFT 

结果为:

  • source 值为 a,b,
  • destination 值为 c,x,y,z

获取元素

按下表(索引)检索
LINDEX key index

Returns the element at index index in the list stored at key. The index is zero-based, so 0 means the first element, 1 the second element and so on. Negative indices can be used to designate elements starting at the tail of the list. Here, -1 means the last element, -2 means the penultimate and so forth.

When the value at key is not a list, an error is returned.

  1. 返回键存储的列表中索引位置的元素。
    1. 索引是从零开始的,所以0表示第一个元素,1表示第二个元素,依此类推。
    2. 负索引可用于指定从列表尾部开始的元素。在这里,-1表示最后一个元素,-2表示倒数第二个元素,依此类推。
  2. 当 key 值不是列表时,将返回错误。
按范围检索

可参考 python 中的 list

if __name__ == '__main__':list_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]print(list_[0:-1])print(list_[-5:-3])

LRANGE extracts a range of elements from a list,与 python 中的 list 有着同样的结构。

LRANGE key start stop
  • start:包含此位置的元素
  • stop:包含此位置的元素

Returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on.

These offsets can also be negative numbers indicating offsets starting at the end of the list. For example, -1 is the last element of the list, -2 the penultimate, and so on.

Out of range indexes will not produce an error. If start is larger than the end of the list, an empty list is returned. If stop is larger than the actual end of the list, Redis will treat it like the last element of the list.

  1. 返回按键存储的列表的指定元素。
    1. 偏移量 start 和 stop 是从零开始的索引,0是列表的第一个元素(列表的头) ,1是下一个元素,依此类推。
    2. 这些偏移量也可以是负数,表示从列表末尾开始的偏移量。例如,-1是列表的最后一个元素,-2是倒数第二个元素,依此类推。
  2. 索引越界:不存在这种异常,redis 会自动处理
127.0.0.1:6379> lrange block -8 -3
1) "jj5"
2) "a"
3) "jj4"
4) "a"
5) "a"
6) "jj3"
127.0.0.1:6379>

插入元素

LINSERT key <BEFORE | AFTER> pivot element
  • <BEFORE | AFTER> :要在指定值 pivot 前|后 插入 element
  • element :要插入的元素
  • pivot:list 中已存在的数据

Inserts element in the list stored at key either before or after the reference value pivot.

When key does not exist, it is considered an empty list and no operation is performed.

An error is returned when key exists but does not hold a list value.

  1. 在引用值 pivot 之前或之后按键存储的列表中插入元素。
  2. 如果键不存在,则将其视为空列表,不执行任何操作。
  3. 如果键存在但不包含列表值,则返回错误。
  • 插入一个元素
127.0.0.1:6379>  linsert block before  jj3 a
(integer) 6
127.0.0.1:6379> lrange block 0 8
1) "jj5"
2) "jj4"
3) "a"
4) "jj3"
5) "jj2"
6) "jj1"
127.0.0.1:6379>
  • 在不存在的值前后插入元素
127.0.0.1:6379> linsert block before  love me
(integer) -1
127.0.0.1:6379>

减少元素

LTRIM reduces a list to the specified range of elements.

Trim an existing list so that it will contain only the specified range of elements specified. Both start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the next element and so on.

For example: LTRIM foobar 0 2 will modify the list stored at foobar so that only the first three elements of the list will remain.

start and end can also be negative numbers indicating offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element and so on.

Out of range indexes will not produce an error: if start is larger than the end of the list, or start > end, the result will be an empty list (which causes key to be removed). If end is larger than the end of the list, Redis will treat it like the last element of the list.

  1. 修剪现有列表,使其仅包含指定的元素范围。
  2. Start 和 stop 都是从零开始的索引,其中0是列表的第一个元素(head) ,1是下一个元素,依此类推。
    1. 例如: LTRIM foobar 02将修改存储在 foobar 中的列表,以便只保留列表的前三个元素。
    2. Start 和 end 也可以是负数,表示从列表末尾开始的偏移量,其中 -1是列表的最后一个元素,-2是倒数第二个元素,依此类推。
  3. 超出范围的索引不会产生错误:
    1. 如果 start 大于列表的末尾,或 start > end,结果将是一个空列表(这将导致删除键)。
    2. 如果 end 大于列表的末尾,Redis 会将其视为列表的最后一个元素。
127.0.0.1:6379> lrange block 0 100
1) "jj5" # 0
2) "a" # 1
3) "jj4" # 2
4) "a"
5) "a"
6) "jj3" # -3
7) "jj2" #-2
8) "jj1"  #-1
127.0.0.1:6379> ltrim block 2 -3
OK
127.0.0.1:6379> lrange block 0 100
1) "jj4"
2) "a"
3) "a"
4) "jj3"
127.0.0.1:6379>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/64941.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机网络(网络层)

1、ARP协议(已知ip地址得到mac地址) 在传输一个 IP 数据报的时候&#xff0c;确定了源 IP 地址和目标 IP 地址后&#xff0c;就会 通过主机「路由表」确定 IP 数据包下一跳。然而&#xff0c;网络层的下一层是数据链路层&#xff0c;所以我们还要知道「下一跳」的 MAC 地址。由…

springboot容器无法获取@Autowired对象,报null对象空指针问题的解决方式

示例错误代码&#xff1a; package com.uniin.ib.provider.iot.handle;Slf4j Component public class FireStringInboundHandler extends ChannelInboundHandlerAdapter {Autowiredprivate RsFireMonitoringMapper rsFireMonitoringMapper;Autowiredprivate RsFireAlertMapper…

5G CPE 主控CPU(上位机) 数据转发性能评估--基于5G模组和NEO3开发板评估

5G CPE ARM 主控CPU 数据转发性能评估--基于5G模组和NEO3开发板评估 摘要NEO3介绍演示架构网络拓扑硬件连接固件及驱动NEO固件NEO3串口驱动拔号及网络配置配置AT接口拔号NEO3网络接口性能测试5G空口性能NEO千兆网口性能验证NEO3 高速接口数据转发性能摘要 当前主流CPE多采用集…

Linux下学【MySQL】表中插入和查询的进阶操作(配实操图和SQL语句通俗易懂)

绪论​ 每日激励&#xff1a;挫折是会让我们变得越来越强大的重点是我们敢于积极的面对它。—Jack叔叔 绪论​&#xff1a; 本章是表操作的进阶篇章&#xff08;没看过入门的这里是传送门&#xff0c;本章将带你进阶的去学习表的插入insert和查找select&#xff0c;本质也就是…

[Rust开发]actix_webmiddleware 中间件

actix_web::middleware 在 Actix Web 框架中扮演着重要的角色&#xff0c;它允许开发者在处理 HTTP 请求和响应的过程中插入自定义的逻辑。中间件可以在请求到达处理函数之前或响应返回给客户端之前执行&#xff0c;从而实现日志记录、身份验证、数据验证、错误处理等功能。 为…

WebLogic T3反序列化漏洞(CVE-2018-2628)--vulhub

WebLogic T3反序列化漏洞(CVE-2018-2628) WebLogic在通信过程中使用T3协议传输数据&#xff0c;涉及到了序列化和反序列化操作。 T3协议概述 T3协议是Oracle的私有协议&#xff0c;所以公开的相关资料比较少&#xff0c;这里结合其他师傅的博客简单对T3协议进行一个简要分析…

redis和mysql的区别

Redis是一种非关系型数据库&#xff08;NoSQL&#xff09;&#xff0c;将数据存储在缓存中&#xff0c;这虽然提高了运行效率&#xff0c;但是保存时间却很短。将数据存储在内存中&#xff0c;因此读写速度非常快&#xff0c;特别适合用于需要高速访问的场景&#xff0c;如缓存…

OpenFeign快速入门 示例:黑马商城

使用起因 之前我们利用了Nacos实现了服务的治理,利用RestTemplate实现了服务的远程调用。这样一来购物车虽然通过远程调用实现了调用商品服务的方法,但是远程调用的代码太复杂了: 解决方法 并且这种调用方式比较复杂&#xff0c;一会儿远程调用&#xff0c;一会儿本地调用。 因…

计算机的错误计算(一百九十)

摘要 用两个大模型计算cot(1.234). 其中&#xff0c;1.234是以弧度为单位的角度。结果保留10位有效数字。实验表明&#xff0c;两个的计算公式虽然不同&#xff0c;但是都是正确的。然而&#xff0c;数值计算则是有问题的---包括每一个中间运算与结果。 例1. 计算cot(1.234)…

【QSS样式表 - ⑥】:QPushButton控件样式

文章目录 QPushBUtton控件样式QSS示例 QPushBUtton控件样式 常用子控件 常用伪状态 QSS示例 代码&#xff1a; QPushButton {background-color: #99B5D1;color: white;font-weigth: bold;border-radius: 20px; }QPushButton:hover {background-color: red; }QPushButton:p…

深度学习与图像处理(国产深度学习框架——飞桨官方指定教材)

计算机视觉从小白到大师之路 《深度学习与图像处理&#xff08;PaddlePaddle版&#xff09;》这一本就够了 1.引言 随着人工智能技术的飞速发展&#xff0c;各行各业对深度学习、图像处理相关领域的人才需求日益迫切。本书旨在通过系统的理论讲解与丰富的实战案例&#xff0…

Scala项目(图书管理系统)

3、service BookService package org.app package serviceimport org.app.dao.{BookDAO, BorrowRecordDAO} import org.app.models.{BookModel, BorrowRecordModel}import java.time.LocalDateTime import scala.collection.mutable.ListBuffer// 图书业务逻辑层 class BookS…

HCIA-Access V2.5_6_3_GPON关键技术

GPON关键技术 GPON关键技术包含三个&#xff0c;测距&#xff0c;DBA&#xff0c;下行AES加密。 为什么需要测距 由于每一个ONU到OLT的距离是不一样的&#xff0c;虽然上行有TDMA技术&#xff0c;让每一个ONU在不同的时间段发送数据&#xff0c;但是可能由于距离的原因&#x…

仿真中产生的simv文件

simv 文件是使用 Synopsys VCS&#xff08;Verilog Compiler Simulator&#xff09;进行硬件描述语言&#xff08;HDL&#xff09;仿真时生成的可执行文件。VCS 是一种流行的仿真工具&#xff0c;用于验证 Verilog、SystemVerilog 和 VHDL 设计的功能正确性。当你编译和链接你的…

v3s点RGB屏 40pin 800x480,不一样的点屏,不通过chosen。

一、背景、目的、简介。 一般来说&#xff0c;通过uboot将屏幕参数传给kernel&#xff0c;是通过修改设备树。 uboot和kernel都需要屏幕点亮。uboot侧重于显示一张图片。而kernel则多是动画。 在这里&#xff0c;我先是找到了一个裸机点屏的代码。将其编译成静态库后&#x…

基于深度学习(HyperLPR3框架)的中文车牌识别系统-0

参考链接&#xff1a;https://zhuanlan.zhihu.com/p/476193567&#xff0c; 结合自己的一些开发经验&#xff0c;从搭建开发环境到实现各主要功能&#xff0c;一点点的撸代码&#xff0c;最终实现了整套系统。整体思路&#xff1a;使用Python调用HyperLPR3&#xff0c;实现车牌…

web复习(三)

简答题 1.简述HTML与HTML5的区别。 取消了一些过时的HTML4标记&#xff0c;如包含显示效果的标记等&#xff0c;已经被CSS所取代&#xff0c;、 等标记则被完全的去掉了。除此之外&#xff0c;在HTML5中加入了大量的新标记&#xff0c;如 、 、 、 、 等&#xff0c;以便在制…

Java全栈项目 - 智能停车场管理系统

项目介绍 智能停车场管理系统是一个基于 Java 全栈技术开发的现代化停车场解决方案。本系统旨在提供便捷的车辆进出管理、实时车位监控、收费管理等功能&#xff0c;提高停车场运营效率。 技术栈 后端 Spring Boot 2.7.xSpring SecurityMyBatis-PlusMySQL 8.0RedisJWT 前端…

电脑丢失bcrypt.dll文件是什么原因?找不到bcrypt.dll文件修复办法来啦!

电脑运行时常见问题及解决方案&#xff1a;文件丢失、文件损坏与系统报错 作为一名软件开发从业者&#xff0c;深知电脑在日常使用中难免会遇到各种问题&#xff0c;如文件丢失、文件损坏和系统报错等。这些问题不仅影响工作效率&#xff0c;还可能带来数据丢失的风险。今天&a…

C++设计模式:享元模式 (附文字处理系统中的字符对象案例)

什么是享元模式&#xff1f; 享元模式是一个非常实用的结构型设计模式&#xff0c;它的主要目的是节省内存&#xff0c;尤其在需要创建大量相似对象时。 通俗解释&#xff1a; 想象我们在写一本书&#xff0c;每个字母都需要表示出来。如果每个字母都单独用对象表示&#xff…