redis--list(列表)

本文详细介绍了如何使用Redis的List数据结构进行左添加(lpush),右添加(rpush),元素追加(lpushx/rpushx),指定位置插入(linsert),修改值(lset),查看值(lindex/lrange),获取长度(llen),删除元素(lpop/rpop/lrem/ltrim),以及元素移动(rpoplpush)等操作。通过实例展示了在实际应用中如何通过Redis进行高效的数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

命令列表:


操作命令参数返回值
左添加插入lpushLPUSH key value [value …]列表当前长度
右添加插入rpushRPUSH key value [value …]列表当前长度
已经存在的列表的左追加lpushxLPUSHX key value列表当前长度
已经存在的列表的右追加rpushxRPUSHX key value列表当前长度
指定元素前后插入元素linsertLINSERT key BEFOREAFTER pivot value
修改指定下标的值lsetLSET key index value布尔
查看具体下标的值lindexLINDEX key index返回具体的值
查看指定下标范围的元素列表(位移)lrangeLRANGE key start stop列表
查看列表长度llenLLEN key数字
左删除lpopLPOP key左边第一个元素的值
右删除rpopRPOP key右边第一个元素的值
删除指定VALUElremLREM key count value删除的数量
删除指定区域外的元素ltrimLTRIM key start stop布尔
元素移动rpoplpushRPOPLPUSH source destination移动的值
阻塞删除blpopBLPOP key [key …] timeout列表:列表,元素

左添加插入:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)


#列表的添加,插入(头添加)
print cli.lpush("list","1","a",2)

右添加插入:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)


#列表的添加,插入(尾添加)
print cli.rpush("list","1","a",2)

已经存在的列表的左追加:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#对已经存在列表的追加(头追加);
print cli.lpushx("list","hello,word")
print cli.lpushx("list1","no")

已经存在的列表的右追加:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#对已经存在列表的追加(尾追加);
print cli.rpushx("list","hello,word")
print cli.rpushx("list1","no")

指定元素前后插入元素:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#在指定元素前后插入元素
print cli.linsert(name="list",where="before",refvalue="hello,word",value="lijing")
print cli.linsert(name="list",where="after",refvalue="hello,word",value="lijing")

修改指定下标的值:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)
#修改指定下标的值
print cli.lset(name="list",index=0,value="liuyy")

查看具体下标的值:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#查看具体下标的值
print cli.lindex("list",1)

查看指定下标范围的元素列表(位移):

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#查看指定下标范围的元素列表(位移)
print cli.lrange(name="list",start=2,end=3)

查看列表长度:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#返回列表的长度
print cli.llen(name="list")

左删除:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#删除列表的头元素,返回值为删除的值
print cli.lpop(name="list")

右删除:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#删除列表的尾元素,返回值为删除的值
print cli.rpop("list")

删除指定的value:

删除count个等于value的元素,且根据count的值决定方向

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#删除count个等于value的元素,且根据count的值决定方向
print cli.lrem(name="list",count=0,value=2)
print cli.lrem(name="list",count=-1,value=1)
print cli.lrem(name="list",count=1,value="a")

删除指定区域外的元素:

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#删除列表中指定区域外的元素
print cli.ltrim(name="list",start=0,end=5)

元素移动:

  • 将一个列表的尾元素添加到另一个列表的头元素,并将其打印出来
  • 安全队列;
  • 循环模式:1.获取元素的一个新方式 2.多个客户端处理同一组数据

  • 命令 RPOPLPUSH 在一个原子时间内,执行以下两个动作:

  • 将列表 source 中的最后一个元素(尾元素)弹出,并返回给客户端。

  • 将 source 弹出的元素插入到列表 destination ,作为 destination 列表的的头元素。

##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

cli.lpush("list1","start")

#将一个列表的尾元素添加到另一个列表的头元素,并将其打印出来{安全队列;循环模式:1.获取元素的一个新方式 2.多个客户端处理同一组数据}
print cli.rpoplpush(src="list",dst="list1")
print cli.rpoplpush(src="list",dst="list")

阻塞删除:

  • 删除列表的头元素,当头元素不存在时,等待timeout时长(秒)
  • 同时存在命令:blpop,brpoplpush
##----以集群形式连接redis----
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli = sentinel.master_for(service_name=service_name,db=db)

#删除列表的头元素,当头元素不存在时,等待timeout时长(秒)
#同时存在命令:blpop,brpoplpush
print cli.blpop(keys="list1",timeout=10)

参考链接:http://redisdoc.com/list/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值