首页 > 数据库 >Redis数据结构之列表

Redis数据结构之列表

时间:2022-10-16 21:24:12浏览次数:40  
标签:127.0 mylist 0.1 list Redis 列表 6379 LRANGE 数据结构

目录

Redis数据结构之列表

Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)一个列表最多可以包含 2^32 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。

查看命令帮助

127.0.0.1:6379> help @list

  BLPOP key [key ...] timeout
  summary: Remove and get the first element in a list, or block until one is available
  since: 2.0.0

  BRPOP key [key ...] timeout
  summary: Remove and get the last element in a list, or block until one is available
  since: 2.0.0

  BRPOPLPUSH source destination timeout
  summary: Pop an element from a list, push it to another list and return it; or block until one is available
  since: 2.2.0

  LINDEX key index
  summary: Get an element from a list by its index
  since: 1.0.0

  LINSERT key BEFORE|AFTER pivot element
  summary: Insert an element before or after another element in a list
  since: 2.2.0

  LLEN key
  summary: Get the length of a list
  since: 1.0.0

  LPOP key
  summary: Remove and get the first element in a list
  since: 1.0.0

  LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
  summary: Return the index of matching elements on a list
  since: 6.0.6

  LPUSH key element [element ...]
  summary: Prepend one or multiple elements to a list
  since: 1.0.0

  LPUSHX key element [element ...]
  summary: Prepend an element to a list, only if the list exists
  since: 2.2.0

  LRANGE key start stop
  summary: Get a range of elements from a list
  since: 1.0.0

  LREM key count element
  summary: Remove elements from a list
  since: 1.0.0

  LSET key index element
  summary: Set the value of an element in a list by its index
  since: 1.0.0

  LTRIM key start stop
  summary: Trim a list to the specified range
  since: 1.0.0

  RPOP key
  summary: Remove and get the last element in a list
  since: 1.0.0

  RPOPLPUSH source destination
  summary: Remove the last element in a list, prepend it to another list and return it
  since: 1.2.0

  RPUSH key element [element ...]
  summary: Append one or multiple elements to a list
  since: 1.0.0

  RPUSHX key element [element ...]
  summary: Append an element to a list, only if the list exists
  since: 2.2.0

创建列表

从左边插入元素

LPUSH

127.0.0.1:6379> LPUSH mylist ddfff 21 nb
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "nb"
2) "21"
3) "ddfff"

从右边插入数据

RPUSH

127.0.0.1:6379> RPUSH mylist qq ll nn
(integer) 6
127.0.0.1:6379> LRANGE mylist 0 -1
1) "nb"
2) "21"
3) "ddfff"
4) "qq"
5) "ll"
6) "nn"

若list存在,则从左边依次追加元素,不存在则忽略

LPUSHX

127.0.0.1:6379> LPUSHX mylist ww ee
(integer) 8
127.0.0.1:6379> LRANGE mylist 0 -1
1) "ee"
2) "ww"
3) "nb"
4) "21"
5) "ddfff"
6) "qq"
7) "ll"
8) "nn"
127.0.0.1:6379> LPUSHX youlist ww ee
(integer) 0
127.0.0.1:6379> LRANGE youlist 0 -1
(empty array)

若list存在,则从右边依次追加元素,不存在则忽略

RPUSHX

127.0.0.1:6379> RPUSHX mylist rr nn
(integer) 10
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "ee"
 2) "ww"
 3) "nb"
 4) "21"
 5) "ddfff"
 6) "qq"
 7) "ll"
 8) "nn"
 9) "rr"
10) "nn"
127.0.0.1:6379> RPUSHX youlist rr nn
(integer) 0
127.0.0.1:6379> LRANGE youlist 0 -1
(empty array)

从list中指定的元素前/后插入一个新元素

LINSERT

127.0.0.1:6379> LRANGE mylist 0 -1
 1) "ee"
 2) "ww"
 3) "nb"
 4) "21"
 5) "ddfff"
 6) "qq"
 7) "ll"
 
 #在指定元素前插入新元素
127.0.0.1:6379> LINSERT mylist before ddfff www
(integer) 8
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "ee"
 2) "ww"
 3) "nb"
 4) "21"
 5) "www"
 6) "ddfff"
 7) "qq"
 8) "ll"
 
 #在指定元素后插入新元素
 127.0.0.1:6379> LINSERT mylist after ddfff eee
(integer) 9
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "ee"
 2) "ww"
 3) "nb"
 4) "21"
 5) "www"
 6) "ddfff"
 7) "eee"
 8) "qq"
 9) "ll"

删除数据

从列表左侧开始删除

LREM

127.0.0.1:6379> LRANGE mylist 0 -1
 1) "we"
 2) "fp"
 3) "we"
 4) "ed"
 5) "we"
 6) "om"
 7) "we"
 8) "ig"
 9) "we"
10) "rn"
11) "we"
12) "df"
127.0.0.1:6379> LREM mylist 2 we		#从列表左侧开始,删除2个we元素
(integer) 2
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "fp"
 2) "ed"
 3) "we"
 4) "om"
 5) "we"
 6) "ig"
 7) "we"
 8) "rn"
 9) "we"
10) "df"

修改数据

LSET

127.0.0.1:6379> LRANGE mylist 0 -1
 1) "fp"		#第0个元素
 2) "ed"		#第1个元素
 3) "we"		#第2个元素
 4) "om"
 5) "we"
 6) "ig"
 7) "we"
 8) "rn"
 9) "we"
10) "df"
#将第1个元素修改为clearlove7
127.0.0.1:6379> LSET mylist 1 clearlove7
OK
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "fp"
 2) "clearlove7"
 3) "we"
 4) "om"
 5) "we"
 6) "ig"
 7) "we"
 8) "rn"
 9) "we"
10) "df"

截取数据

LTRIM

127.0.0.1:6379> LRANGE mylist 0 -1
 1) "fp"
 2) "clearlove7"
 3) "we"
 4) "om"
 5) "we"
 6) "ig"
 7) "we"
 8) "rn"
 9) "we"
10) "df"
#截取列表第1到第4个元素,替换源列表
127.0.0.1:6379> LTRIM mylist 1 4
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"

查看列表

查看数据

LRANGE

#查看全部元素
127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"
#查看第1到第2个元素
127.0.0.1:6379> LRANGE mylist 1 2
1) "we"
2) "om"

根据下标查看某个元素

LINDE

127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"
127.0.0.1:6379> LINDEX mylist 0
"clearlove7"

查看列表长度

LLEN

127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"
127.0.0.1:6379> LLEN mylist
(integer) 4

从左边查看并删除元素

LPOP

127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"
127.0.0.1:6379> LPOP mylist
"clearlove7"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "we"
2) "om"
3) "we"

从右边查看并删除元素

RPOP

127.0.0.1:6379> LRANGE mylist 0 -1
1) "we"
2) "om"
3) "we"
127.0.0.1:6379> RPOP mylist
"we"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "we"
2) "om"

查看一个元素并移动到另一个列表中

RPOPLPUSH

127.0.0.1:6379> LRANGE mylist 0 -1
1) "11"
2) "10"
3) "01"
4) "00"
5) "we"
6) "om"
127.0.0.1:6379> LRANGE testlist 0 -1
1) "ff"
2) "dd"
127.0.0.1:6379> RPOPLPUSH mylist testlist
"om"
127.0.0.1:6379> RPOPLPUSH mylist testlist
"we"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "11"
2) "10"
3) "01"
4) "00"
127.0.0.1:6379> LRANGE testlist 0 -1
1) "we"
2) "om"
3) "ff"
4) "dd"

BLPOP

从列表左侧开始查询,返回列表的key和左侧第一个元素

若查询的列表中没有元素,则原地阻塞至设置的timeout过后,返回空

若在阻塞状态时,列表新增了元素,则返回列表的key和左侧第一个元素,并删除该元素

#服务器A
#查询列表,此时列表为空,阻塞并等待100秒
127.0.0.1:6379> BLPOP testlist 100

#服务器B
#此时有人向列表插入新元素
127.0.0.1:6379> LPUSH testlist we
(integer) 1

#服务器A
#检测到新增元素,则返回列表的key和左侧第一个元素,并删除该元素
127.0.0.1:6379> BLPOP testlist 100
1) "testlist"
2) "we"
(22.24s)

#服务器B
#新增元素后查看,发现元素被删除
127.0.0.1:6379> LRANGE testlist 0 -1
(empty array)

标签:127.0,mylist,0.1,list,Redis,列表,6379,LRANGE,数据结构
From: https://www.cnblogs.com/c-moon/p/16797207.html

相关文章

  • Redis数据结构之哈希
    目录Redis数据结构之哈希写入获取数据修改数据删除数据删除所有数据查看key中指定的field是否存在若value中没有相应的field,则创建获取多个值获取所有的key和value获取所......
  • Redis数据类型之无序集合
    目录Redis数据类型之无序集合查看命令帮助增加数据查看数据删除数据移动数据返回集合中成员的个数随机返回指定个数的数据判断对象是否存在于集合中随机返回并删除一个成员......
  • 【数据结构】栈的定义以及接口函数的C语言代码实现(仅供学习交流使用)
    1、栈的定义栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last......
  • spark通过pipline方式批量插入redis集群方式
    spark通过pipline方式批量插入redis集群网上资料比较少,但是有一大堆都是单机的方式,spring倒是也有写入redis集群的实现代码,以下整理了spark通过pipline批量写入的方式,速度......
  • 【数据结构】二叉树的概念和简单实现(仅供学习交流使用)
    1、树1、树的概念   树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合。把它叫做树是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝......
  • 17.Redis之分布式锁
    参考1:https://www.cnblogs.com/wangyingshuo/p/14510524.html参考2: https://blog.csdn.net/Me_xuan/article/details/124418176参考3:https://blog.csdn.net/a745233700......
  • 18.Redis常见的面试题
    参考1:https://lebron.blog.csdn.net/article/details/121456167参考2:https://lebron.blog.csdn.net/article/details/120817994......
  • 【以练促学】(数据结构)1.绪论篇
    (持续刷题持续更新...) 1.数据结构的三要素:逻辑结构、物理结构、数据运算 eg.以下属于逻辑结构的( )A.顺序表   B.哈希表   C.有序表  D.单链......
  • 04 队列 | 数据结构与算法
    1.队列1.队列的概念队列:操作受限的线性表,只允许在一端进行元素的插入,另一端进行元素的删除空队列:不含有任何元素的队列队头和队尾:进行删除的一端叫队头front,进行插......
  • redis的发布订阅模式
    redis的发布订阅模式redis发布订阅(pub/sub)是一种消息通信模式,消息的发布者不会将消息发送给特定的订阅者,而是通过消息通道(频道)广播出去,让订阅该消息主题(频道)的订阅......