四)redis 列表类型

栏目: 数据库 · 发布时间: 9年前

内容简介:四)redis 列表类型

列表类型,可以存储一个有序的字符串列表,常用的操作是向列表两端添加元素,或者获得列表的某一个片段

列表类型内部是使用双向链表实现的,所以向列表两端添加元素的时间复杂度是 o(1),获取越接近两端的元素速度就越快

链表的代价是通过索引访问元素比较慢

向列表两端添加元素

向列表左边添加元素,返回增加后列表的长度

LPUSH key value1 value2

127.0.0.1:6379> LPUSH number 1  
(integer) 1
127.0.0.1:6379> LPUSH number 1 2 3  
(integer) 4

向列表右边增加元素,返回增加后列表的长度

RPUSH key value1 value2

127.0.0.1:6379> RPUSH number 7 8 9  
(integer) 7

向列表两端移除元素

向左边移除元素,返回被移除的元素

127.0.0.1:6379> LPOP number  
"3"

向右边移除元素,返回被移除的元素

127.0.0.1:6379> RPOP number  
"9"

获取列表中元素的个数

当键不存在是,返回 0

127.0.0.1:6379> LLEN number  
(integer) 5

获取列表片段

可以根据索引范围获取某一段数据,索引从 0 开始

LRANGE key startIndex stopIndex
127.0.0.1:6379> LRANGE number 0 1  
1) "2"  
2) "1"

LRANGE 也支持负索引,比如 -1 表示最右边的第一个元素

127.0.0.1:6379> LRANGE number -2 -1  
1) "7"  
2) "8"

所以,我们使用 LRANGE number 0 -1 可以获取列表中的所有元素

如果 start 的索引位置比 stop 靠后,返回空列表

如果 stop 大于实际索引范围,则会返回到列表最右边的元素

删除列表中指定的值

LREM key count value

会删除列表中前 count 个值为 value 的元素,返回实际删除的元素个数

  • 当 count > 0 时,会从左边开始删除
  • 当 count < 0 时,会从右边开始删除
  • 当 count = 0 时,会删除所有
127.0.0.1:6379> LRANGE number 0 -1  
1) "2"  
2) "1"  
3) "1"  
4) "7"  
5) "8"  
127.0.0.1:6379> LREM number 1 2  
(integer) 1
127.0.0.1:6379> LRANGE number 0 -1  
1) "1"  
2) "1"  
3) "7"  
4) "8"

获取和设置指定索引的元素值

LINDEX key index

127.0.0.1:6379> LRANGE number 0 -1  
1) "1"  
2) "1"  
3) "7"  
4) "8"  
127.0.0.1:6379> LINDEX number 1  
"1"

LSET key index value

127.0.0.1:6379> LSET number 1 2  
OK  
127.0.0.1:6379> LRANGE number 0 -1  
1) "1"  
2) "2"  
3) "7"  
4) "8"

只保留列表指定片段

删除指定范围之外的所有元素,指定范围和 LRANGE 一致

LTRIM key startIndex endIndex

127.0.0.1:6379> LRANGE number 0 -1  
1) "1"  
2) "2"  
3) "7"  
4) "8"  
127.0.0.1:6379> LTRIM number 1 2  
OK  
127.0.0.1:6379> LRANGE number 0 -1  
1) "2"  
2) "7"

向列表中插入元素

LINSERT key BEFORE | AFTER pivot value

LINSERT 会在列表中从左到右查找值为 pivot 的元素,然后根据第二个参数 BEFORE | AFTER 决定将值插入到该元素后面还是前面

127.0.0.1:6379> LRANGE number 0 -1  
1) "2"  
2) "7"  
127.0.0.1:6379> LINSERT number AFTER 2 3  
(integer) 3
127.0.0.1:6379> LRANGE number 0 -1  
1) "2"  
2) "3"  
3) "7"  
127.0.0.1:6379> LINSERT number BEFORE 7 4  
(integer) 4
127.0.0.1:6379> LRANGE number 0 -1  
1) "2"  
2) "3"  
3) "4"  
4) "7"

将元素从一个列表转移到另一个列表

RPOPLPUSH source destination

把 source 右边的第一个元素弹出给 destination

127.0.0.1:6379> LRANGE number 0 -1  
1) "2"  
2) "3"  
3) "4"  
4) "7"  
127.0.0.1:6379> RPOPLPUSH number numberCopy  
"7"
127.0.0.1:6379> LRANGE numberCopy 0 -1  
1) "7"  
127.0.0.1:6379> LRANGE number 0 -1  
1) "2"  
2) "3"  
3) "4"

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to the Analysis of Algorithms

An Introduction to the Analysis of Algorithms

Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99

This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具