Redis初探04——Redis的list类型及操作

来源:互联网 发布:淘宝的免费开店在哪里 编辑:程序博客网 时间:2024/06/06 16:37

List是一个链表结构,主要功能是push,pop,获取一个范围的所有值等等,操作中key理解为链表的名字。redis的list类型起其实就是一个每个子元素都是String类型的双向链表。我们可以通过push,pop操作从链表的头部或者尾部添加元素、删除元素,这样list既可以作为栈,又可以作为队列。
操作:
指向头的方向为前。
1、lpush
在key对应的list的头部添加字符串元素。lpush list名称 value;栈的方式:先进后出

127.0.0.1:6379> lpush list1 hello(integer) 1127.0.0.1:6379> lpush list1 world(integer) 2127.0.0.1:6379> lrange list1 0 -11) "world"2) "hello"

2、lrange list名称 0 -1 取出栈中栈顶到栈底的元素。0栈顶/对头下标,-1 栈底/队列底下标

3、rpush
在key对应list的尾部添加字符串元素。rpush list名称 value;相当于队列:先进先出

127.0.0.1:6379> rpush list2 zhaojw(integer) 1127.0.0.1:6379> rpush list2 zhanr(integer) 2127.0.0.1:6379> lrange list2 0 -11) "zhaojw"2) "zhanr"

4、linsert
在key对应的list的特定位置前或后添加元素;linsert list名称 before/after 特定位置的value 要插入的value;

127.0.0.1:6379> rpush list3 world(integer) 1127.0.0.1:6379> linsert list3 before world hello(integer) 2127.0.0.1:6379> lrange list3 0 -11) "hello"2) "world"127.0.0.1:6379> linsert list3 after world zhaojw(integer) 3127.0.0.1:6379> lrange list3 0 -11) "hello"2) "world"3) "zhaojw"

5、lset
设置list中指定下标的元素值。lset list名称 下标 要更新的值。

127.0.0.1:6379> lpush list5 hello(integer) 1127.0.0.1:6379> lpush list5 world(integer) 2127.0.0.1:6379> lpush list5 zhaojw(integer) 3127.0.0.1:6379> lrange list5 0 -11) "zhaojw"2) "world"3) "hello"127.0.0.1:6379> lset list5 1 helloOK127.0.0.1:6379> lset list5 2 worldOK127.0.0.1:6379> lrange list5 0 -11) "zhaojw"2) "hello"3) "world"

6、lrem
从key对应list中删除n个和value相同的元素。(n<0从尾删除,n=0全部删除),返回删除掉元素的个数。lrem list名称 删除元素个数n 删除的value值。

127.0.0.1:6379> rpush list6 hello(integer) 1127.0.0.1:6379> rpush list6 world(integer) 2127.0.0.1:6379> rpush list6 hello(integer) 3127.0.0.1:6379> lrange list6 0 -11) "hello"2) "world"3) "hello"127.0.0.1:6379> lrem list6 1 hello(integer) 1127.0.0.1:6379> lrange list6 0 -11) "world"2) "hello"

7、ltrim
保留指定key的值范围内的数据。ltrim list名称 开始下标 结束下标

127.0.0.1:6379> rpush list7 hello(integer) 1127.0.0.1:6379> rpush list7 world(integer) 2127.0.0.1:6379> rpush list7 zhaojw(integer) 3127.0.0.1:6379> rpush list7 zhanr(integer) 4127.0.0.1:6379> lrange list7 0 -11) "hello"2) "world"3) "zhaojw"4) "zhanr"127.0.0.1:6379> ltrim list7 2 -1OK127.0.0.1:6379> lrange list7 0 -11) "zhaojw"2) "zhanr"

8、lpop
从list的头部删除元素,并返回删除元素。lpop list名称 。

127.0.0.1:6379> lpush list8 hello(integer) 1127.0.0.1:6379> lpush list8 world(integer) 2127.0.0.1:6379> lpush list8 zhaojw(integer) 3127.0.0.1:6379> lrange list8 0 -11) "zhaojw"2) "world"3) "hello"127.0.0.1:6379> lpop list8"zhaojw"127.0.0.1:6379> lrange list8 0 -11) "world"2) "hello"127.0.0.1:6379> rpush list88 hello(integer) 1127.0.0.1:6379> rpush list88 world(integer) 2127.0.0.1:6379> rpush list88 zhaojw(integer) 3127.0.0.1:6379> lpop list88"hello"127.0.0.1:6379> lrange list88 0 -11) "world"2) "zhaojw"127.0.0.1:6379> lpop list88"world"127.0.0.1:6379> lrange list88 0 -11) "zhaojw"

9、rpop
从list的尾部删除元素,并返回删除元素。rpop list名称 。

127.0.0.1:6379> lpush list9 zhaojw(integer) 1127.0.0.1:6379> lpush list9 world(integer) 2127.0.0.1:6379> lpush list9 hello(integer) 3127.0.0.1:6379> lrange list9 0 -11) "hello"2) "world"3"zhaojw"127.0.0.1:6379> rpop list9"zhaojw"127.0.0.1:6379> lrange list9 0 -11) "hello"2) "world"127.0.0.1:6379> rpop list9"world"127.0.0.1:6379> lrange list9 0 -11) "hello"

10、rpoplpush
从第一个list的尾部移除元素并添加到第二个list的头部。rpoplpush 第一个list的名称 第二个list的名称

127.0.0.1:6379> lpush list10 zhaojw(integer) 1127.0.0.1:6379> lpush list10 world(integer) 2127.0.0.1:6379> lpush list10 zhaojw(integer) 3127.0.0.1:6379> lrange list10 0 -11) "zhaojw"2) "world"3) "zhaojw"127.0.0.1:6379> rpush list11 hello(integer) 1127.0.0.1:6379> rpush list11 world(integer) 2127.0.0.1:6379> lrange list11 0 -11) "hello"2) "world"127.0.0.1:6379> rpoplpush list10 list11"zhaojw"127.0.0.1:6379> lrange list10 0 -11) "zhaojw"2) "world"127.0.0.1:6379> lrange list11 0 -11) "zhaojw"2) "hello"3) "world"  

11、lindex
返回list中的index位置的元素。lindex list名称 index位置

127.0.0.1:6379> lpush list12 hello(integer) 1127.0.0.1:6379> lpush list12 world(integer) 2127.0.0.1:6379> lpush list12 zhaojw(integer) 3127.0.0.1:6379> lpush list12 hello(integer) 4127.0.0.1:6379> lrange list12 0 -11) "hello"2) "zhaojw"3) "world"4) "hello"127.0.0.1:6379> lindex list12 2"world"127.0.0.1:6379> lindex list12 1"zhaojw"127.0.0.1:6379> lindex list12 -1"hello"127.0.0.1:6379> rpop list12"hello"127.0.0.1:6379> lrange list12 0 -11) "hello"2) "zhaojw"3) "world"127.0.0.1:6379> lindex list12 -1"world"

12、llen
返回list的长度,即元素个数

127.0.0.1:6379> lrange list12 0 -11) "hello"2) "zhaojw"3) "world"127.0.0.1:6379> llen list12(integer) 3
0 0
原创粉丝点击