---
title: "Redis详解：lists数据类型及操作"
author: "songzhan"
date: "2014-08-26T10:47:09Z"
tags: []
categories: ["企业架构"]
slug: "redis-lists"
draft: false
lastmod: "2014-08-26T10:47:09Z"
id: 2109009
url: "https://songzhan.iteye.com/blog/2109009"
---

原文章地址：http://tech.it168.com/a2011/0926/1251/000001251881.shtml

 

 

【IT168 专稿】 list是一个链表结构，主要功能是push、pop、获取一个范围的所有值等等，操作中key理解为链表的名字。

　　系列文章：

　　[Redis详解：strings数据类型及操作](http://tech.it168.com/a2011/0901/1241/000001241205.shtml)

　　[Redis详解：hashes数据类型及操作](http://tech.it168.com/a2011/0913/1245/000001245822.shtml)

　　Redis的list类型其实就是一个每个子元素都是string类型的双向链表。链表的最大长度是(2的32次方)。我们可以通过push,pop操作从链表的头部或者尾部添加删除元素。这使得list既可以用作栈，也可以用作队列。

　 　有意思的是list的pop操作还有阻塞版本的，当我们\[lr]pop一个list对象时，如果list是空，或者不存在，会立即返回nil。但是阻塞 版本的b\[lr]pop可以则可以阻塞，当然可以加超时时间，超时后也会返回nil。为什么要阻塞版本的pop呢，主要是为了避免轮询。举个简单的例子如 果我们用list来实现一个工作队列。执行任务的thread可以调用阻塞版本的pop去获取任务这样就可以避免轮询去检查是否有任务存在。当任务来时候 工作线程可以立即返回，也可以避免轮询带来的延迟。说了这么多，接下来看一下实际操作的方法吧：

　　**1、lpush**

　　在key对应list的头部添加字符串元素：

redis 127.0.0.1:6379&gt; lpush mylist "world"  
(integer) 1  
redis 127.0.0.1:6379&gt; lpush mylist "hello"  
(integer) 2  
redis 127.0.0.1:6379&gt; lrange mylist 0-1  
1\) "hello"  
2\) "world"  
redis 127.0.0.1:6379&gt;

　　在此处我们先插入了一个world，然后在world的头部插入了一个hello。其中lrange是用于取mylist的内容。

　　**2、rpush**

　　在key对应list的尾部添加字符串元素：

redis 127.0.0.1:6379&gt; rpush mylist2 "hello"  
(integer) 1  
redis 127.0.0.1:6379&gt; rpush mylist2 "world"  
(integer) 2  
redis 127.0.0.1:6379&gt; lrange mylist2 0-1  
1\) "hello"  
2\) "world"  
redis 127.0.0.1:6379&gt;

　　在此处我们先插入了一个hello，然后在hello的尾部插入了一个world。

　　**3、linsert**

　　在key对应list的特定位置之前或之后添加字符串元素：

redis 127.0.0.1:6379&gt; rpush mylist3 "hello"  
(integer) 1  
redis 127.0.0.1:6379&gt; rpush mylist3 "world"  
(integer) 2  
redis 127.0.0.1:6379&gt; linsert mylist3 before "world""there"  
(integer) 3  
redis 127.0.0.1:6379&gt; lrange mylist3 0-1  
1\) "hello"  
2\) "there"  
3\) "world"  
redis 127.0.0.1:6379&gt;

　　在此处我们先插入了一个hello，然后在hello的尾部插入了一个world，然后又在world的前面插入了there。

　　**4、lset**

　　设置list中指定下标的元素值(下标从0开始)：

redis 127.0.0.1:6379&gt; rpush mylist4 "one"  
(integer) 1  
redis 127.0.0.1:6379&gt; rpush mylist4 "two"  
(integer) 2  
redis 127.0.0.1:6379&gt; rpush mylist4 "three"  
(integer) 3  
redis 127.0.0.1:6379&gt; lset mylist4 0"four"  
OK  
redis 127.0.0.1:6379&gt; lset mylist4 -2"five"  
OK  
redis 127.0.0.1:6379&gt; lrange mylist4 0-1  
1\) "four"  
2\) "five"  
3\) "three"  
redis 127.0.0.1:6379&gt;

　　在此处我们依次插入了one,two,three，然后将标是0的值设置为four，再将下标是-2的值设置为five。

　　**5、lrem**

　　从key对应list中删除count个和value相同的元素。

　　count&gt;0时，按从头到尾的顺序删除，具体如下：

redis 127.0.0.1:6379&gt; rpush mylist5 "hello"  
(integer) 1  
redis 127.0.0.1:6379&gt; rpush mylist5 "hello"  
(integer) 2  
redis 127.0.0.1:6379&gt; rpush mylist5 "foo"  
(integer) 3  
redis 127.0.0.1:6379&gt; rpush mylist5 "hello"  
(integer) 4  
redis 127.0.0.1:6379&gt; lrem mylist5 2"hello"  
(integer) 2  
redis 127.0.0.1:6379&gt; lrange mylist5 0-1  
1\) "foo"  
2\) "hello"  
redis 127.0.0.1:6379&gt;

　　count&lt;0时，按从尾到头的顺序删除，具体如下：

redis 127.0.0.1:6379&gt; rpush mylist6 "hello"  
(integer) 1  
redis 127.0.0.1:6379&gt; rpush mylist6 "hello"  
(integer) 2  
redis 127.0.0.1:6379&gt; rpush mylist6 "foo"  
(integer) 3  
redis 127.0.0.1:6379&gt; rpush mylist6 "hello"  
(integer) 4  
redis 127.0.0.1:6379&gt; lrem mylist6 -2"hello"  
(integer) 2  
redis 127.0.0.1:6379&gt; lrange mylist6 0-1  
1\) "hello"  
2\) "foo"  
redis 127.0.0.1:6379&gt;

　　count=0时，删除全部，具体如下：

redis 127.0.0.1:6379&gt; rpush mylist7 "hello"  
(integer) 1  
redis 127.0.0.1:6379&gt; rpush mylist7 "hello"  
(integer) 2  
redis 127.0.0.1:6379&gt; rpush mylist7 "foo"  
(integer) 3  
redis 127.0.0.1:6379&gt; rpush mylist7 "hello"  
(integer) 4  
redis 127.0.0.1:6379&gt; lrem mylist7 0"hello"  
(integer) 3  
redis 127.0.0.1:6379&gt; lrange mylist7 0-1  
1\) "foo"  
redis 127.0.0.1:6379&gt;