redis的LIst列表

List列表(双向链表结构 )

Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)

list即可以作为“栈”也可以作为"队列"。
操作:

lpush list1 "world" //在list1头部压入一个字串
lpush list1 "hello" // 在list1头部压入一个字串
lrange list1 0 -1 //获取list1中内容
0:表示开头 -1表示结尾。

rpush list2 "world" //在list2尾部压入一个字串
rpush list2 "hello" // 在list2尾部压入一个字串
lrange list2 0 -1 //获取list2中内容
0:表示开头 -1表示结尾。

linsert list2 before hello there
在key对应list的特定位置前或后添加字符串

lset list2 1 "four"
修改指定索引位置上的值

lrem list2 2 "hello" //删除前两个hello值
lrem list2 -2 "hello" //删除后两个hello值
lrem list2 0 "hello" //删除所有hello值

ltrim mylist8 1 3 //删除此范围外的值

lpop list2 //从list2的头部删除元素,并返回删除元素
rpop list2 //从list2的尾部删除元素,并返回删除元素
rpoplpush list1 list2 //将list1的尾部一个元素移出到list2头部。并返回

lindex list2 1 //返回list2中索引位置上的元素
llen list2 //返回list2上长度

代码实例

127.0.0.1:6379> lpush list01 hello
(integer) 1
127.0.0.1:6379> lpush list02 world
(integer) 1
127.0.0.1:6379> lrange list01 0 -1
1) "hello"
127.0.0.1:6379> lpush list01 world
(integer) 2
127.0.0.1:6379> lrange list01 0 -1
1) "world"
2) "hello"
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> rpush list02 'hello'
(integer) 2
127.0.0.1:6379> rpush list02 'world'
(integer) 3
127.0.0.1:6379> lrange list02 0 -1
1) "world"
2) "hello"
3) "world"
127.0.0.1:6379> linsert list02 before hello there
(integer) 4
127.0.0.1:6379> lrange list02 0 -1
1) "world"
2) "there"
3) "hello"
4) "world"
127.0.0.1:6379> lset list2 3 four
(error) ERR no such key
127.0.0.1:6379> lset list02 3 four
OK
127.0.0.1:6379> lrange list02 0 -1
1) "world"
2) "there"
3) "hello"
4) "four"
127.0.0.1:6379> ltrim list02 1 3
OK
127.0.0.1:6379> lrange list02 0 -1
1) "there"
2) "hello"
3) "four"
127.0.0.1:6379> lrem list2 2 hello
(integer) 0
127.0.0.1:6379> lrem list02 2 hello
(integer) 1
127.0.0.1:6379> lpop list02
"there"
127.0.0.1:6379> rpop list02
"four"
127.0.0.1:6379> lrange list02 0 -1
(empty list or set)
127.0.0.1:6379> lpush list03 'wo'
(integer) 1
127.0.0.1:6379> lpush list03 'shi'
(integer) 2
127.0.0.1:6379> lpush list03 'sunziheng'
(integer) 3
127.0.0.1:6379> lindex list03 1
"shi"
127.0.0.1:6379> lindex list03 0
"sunziheng"
127.0.0.1:6379> lindex list03 2
"wo"
127.0.0.1:6379> llen list03
(integer) 3
127.0.0.1:6379> 

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Redis 的 list 类型其实就是一个每个子元素都是 string 类型的双向链表。主要功能是 push、po...
    鬭闢阅读 351评论 0 0
  • redis的列表是链表的数据结构 在操作方式上来看,既可以看做队列,又可以看做为栈 就TM把它当做栈来理解! 可以...
    吐痰高手阅读 9,680评论 0 1
  • 前言 Redis的作者antirez(Salvatore Sanfilippo)曾经发表了一篇名为Redis宣言(...
    OzanShareing阅读 1,554评论 0 20
  • 本文主要介绍了 Redis 的简介、安装、常用命令和基础类型。 简介和安装 1. NoSQL 简介 NoSQL,泛...
    七弦桐语阅读 629评论 0 5
  • 【挑战版】 环顾四周,选择一个以自己名字命名的商品,然后分析一下,这个产品做得好的原因是什么?列举至少5条,并且把...
    夏鸢的暖心小筑阅读 220评论 0 0

友情链接更多精彩内容