设为首页 收藏本站
查看: 834|回复: 0

[经验分享] redis 在 php 中的应用(List篇)

[复制链接]

尚未签到

发表于 2017-12-21 09:49:38 | 显示全部楼层 |阅读模式
  上一篇 : redis 在 php 中的应用(Hash篇)
  本文为我阅读了redis参考手册 之后编写,注意 php_redis 和 redis-cli 的区别(主要是返回值类型和参数用法)
  目录:

List(列表)
LPUSH
LPUSHX
RPUSH
RPUSHX
LPOP
RPOP
BLPOP
BRPOP
LLEN
LRANGE
LREM
LSET
LTRIM
LINDEX
LINSERT
RPOPLPUSH
BRPOPLUSH
  一、List(列表)
  1、LPUSH
  Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误。(在Redis 2.4版本以前的 LPUSH 命令,都只接受单个 value 值。)
  语法:
  redis 127.0.0.1:6379> LPUSH KEY_NAME VALUE1.. VALUEN
  返回值: 执行 LPUSH 命令后,列表的长度。
  可用版本: >= 1.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

($redis -> lPush('favorite_fruit','cherry'));     // key 不存在,创建一个新的列表, 返回 int 1  
($redis -> lPush('favorite_fruit','banana'));     // key 存在。但是 list 类型, 返回 int 2
  

  
$redis -> set('pats','dog');
  

($redis -> lPush('pats','cat'));     // key 存在。但不是是 list 类型, 返回 boolean false  

  2、LPUSHX
  Redis Lpushx 将一个或多个值插入到已存在的列表头部,列表不存在时操作无效。
  语法:
  redis 127.0.0.1:6379> LPUSHX KEY_NAME VALUE1.. VALUEN
  返回值: LPUSHX 命令执行之后,列表的长度。
  可用版本: >= 2.2.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

($redis -> rPush('favorite_fruit','cherry'));  

($redis -> rPush('favorite_fruit','banana'));  

($redis -> lPushx('favorite_fruit','apple'));     //  返回 int 3  
($redis -> lRange('favorite_fruit',0,-1));
  

//  array (size=3)  
//      0 => string 'apple' (length=5)
  
//      1 => string 'cherry' (length=6)
  
//      2 => string 'banana' (length=6)
  

  
($redis -> lPushx('fake_key','invalid_val'));     //  列表不存在时操作无效返回 int (0)
  

  3、RPUSH
  Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)。如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。(注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值)。
  语法:
  redis 127.0.0.1:6379> RPUSH KEY_NAME VALUE1..VALUEN
  返回值: 执行 RPUSH 操作后,列表的长度。
  可用版本: >= 1.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

($redis -> rPush('favorite_fruit','cherry'));     // key 不存在,创建一个新的列表, 返回 int 1  
($redis -> rPush('favorite_fruit','banana'));     // key 存在。但是 list 类型, 返回 int 2
  

  
$redis -> set('pats','dog');
  

($redis -> rPush('pats','cat'));     // key 存在。但不是是 list 类型, 返回 boolean false  

  4、RPUSHX
  Redis Rpushx 命令用于将一个或多个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效。
  语法:
  redis 127.0.0.1:6379> RPUSHX KEY_NAME VALUE1..VALUEN
  返回值:执行 Rpushx 操作后,列表的长度。
  可用版本: >= 2.2.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

($redis -> lPush('favorite_fruit','cherry'));  

($redis -> lPush('favorite_fruit','banana'));  

($redis -> rPushx('favorite_fruit','apple'));     //  返回 int 3  
($redis -> lRange('favorite_fruit',0,-1));
  

//  array (size=3)  
//      0 => string 'banana' (length=6)
  
//      1 => string 'cherry' (length=6)
  
//      2 => string 'apple' (length=5)
  

  
($redis -> rPushx('fake_key','invalid_val'));     //  列表不存在时操作无效返回 int (0)
  

  5、LPOP
  Redis Lpop 命令用于移除并返回列表的第一个元素。
  语法:
  redis 127.0.0.1:6379> LPOP KEY_NAME
  返回值:列表的第一个元素。 当列表 key 不存在时,返回 nil 。
  可用版本:>= 1.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

($redis -> lPush('favorite_fruit','cherry'));  

($redis -> lPush('favorite_fruit','banana'));  

($redis -> lPush('favorite_fruit','apple'));  

($redis -> lPop('favorite_fruit'));             // string apple  
($redis -> lRange('favorite_fruit',0,-1));
  

//  array (size=2)  
//      0 => string 'banana' (length=6)
  
//      1 => string 'cherry' (length=6)
  

  6、RPOP
  Redis Rpop 命令用于移除并返回列表的最后一个元素。
  语法:
  redis 127.0.0.1:6379> RPOP KEY_NAME
  返回值:列表的最后一个元素。 当列表不存在时,返回 nil 。
  可用版本:>= 1.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','banana');  

$redis -> lPush('favorite_fruit','apple');  

($redis -> rPop('favorite_fruit'));             // string cherry  
($redis -> lRange('favorite_fruit',0,-1));
  

//  array (size=2)  
//      0 => string 'apple' (length=5)
  
//      1 => string 'banana' (length=6)
  

  7、BLPOP
  Redis Blpop 命令移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  (1)当给定多个key参数时,按参数key的先后顺序依次检查各个列表,弹出第一个非空列表的头元素。
  (2)超时参数timeout接受一个以秒为单位的数字作为值。超时参数设为0表示阻塞时间可以无限期延长
  (3)在MULTI/EXEC事务中的BLPOP,行为表现得就像LPOP一样,对空列表返回nil,对非空列表弹出列表元素,不进行任何阻塞操作。
  语法:
  redis 127.0.0.1:6379> BLPOP LIST1 LIST2 .. LISTN TIMEOUT
  返回值:如果列表为空,返回一个 nil 。 否则,返回一个含有两个元素的列表,第一个元素是被弹出元素所属的 key ,第二个元素是被弹出元素的值。
  可用版本:>= 2.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

// This first case: 非阻塞行为,最少有一个非空列表  
$redis -> lPush('favorite_fruit','cherry');
  

$redis -> lPush('favorite_fruit','banana');  

$redis -> lPush('favorite_fruit','apple');  

  

$redis -> lPush('pats','dog');  

$redis -> lPush('pats','cat');  

$redis -> lPush('pats','rabbit');  

  

($redis -> blPop('favorite_fruit',3));  

//  array (size=2)  
//      0 => string 'favorite_fruit' (length=14)
  
//      1 => string 'apple' (length=5)
  

  
$array_blpop = array('favorite_fruit','pats');
  
($redis -> blPop($array_blpop,3));          // 优先弹出第一个非空列表的头元素
  
//  array (size=2)
  
//      0 => string 'favorite_fruit' (length=14)
  
//      1 => string 'banana' (length=6)
  

  
($redis -> lRange('favorite_fruit',0,-1));
  
//  array (size=1)
  
//      0 => string 'cherry' (length=6)
  

  
// This second case: 阻塞行为, 所有给定key都不存在或包含空列表
  
($redis -> blPop('fake_key',2));    // 阻塞链接, 2s 之后超时结束,返回 array (size=0) empty
  

  8、BRPOP
  Redis Brpop 命令移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  (1)当给定多个key参数时,按参数key的先后顺序依次检查各个列表,弹出第一个非空列表的尾部元素。
  (2)超时参数timeout接受一个以秒为单位的数字作为值。超时参数设为0表示阻塞时间可以无限期延长
  (3)在MULTI/EXEC事务中的BLPOP,行为表现得就像RPOP一样,对空列表返回nil,对非空列表弹出列表元素,不进行任何阻塞操作。
  语法:
  redis 127.0.0.1:6379> BRPOP LIST1 LIST2 .. LISTN TIMEOUT
  返回值:假如在指定时间内没有任何元素被弹出,则返回一个 nil 和等待时长。 反之,返回一个含有两个元素的列表,第一个元素是被弹出元素所属的 key ,第二个元素是被弹出元素的值。
  可用版本:>= 2.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

// This first case: 非阻塞行为,最少有一个非空列表  
$redis -> lPush('favorite_fruit','cherry');
  

$redis -> lPush('favorite_fruit','banana');  

$redis -> lPush('favorite_fruit','apple');  

  

$redis -> lPush('pats','dog');  

$redis -> lPush('pats','cat');  

$redis -> lPush('pats','rabbit');  

  

($redis -> brPop('favorite_fruit',3));  

//  array (size=2)  
//      0 => string 'favorite_fruit' (length=14)
  
//      1 => string 'cherry' (length=6)
  

  
$array_brpop = array('favorite_fruit','pats');
  
($redis -> brPop($array_brpop,3));          // 优先弹出第一个非空列表的头元素
  
//  array (size=2)
  
//      0 => string 'favorite_fruit' (length=14)
  
//      1 => string 'banana' (length=6)
  

  
($redis -> lRange('favorite_fruit',0,-1));
  
//  array (size=1)
  
//      0 => string 'apple' (length=5)
  

  
// This second case: 阻塞行为, 所有给定key都不存在或包含空列表
  
($redis -> brPop('fake_key',2));    // 阻塞链接, 2s 之后超时结束,返回 array (size=0) empty
  

  9、LLEN
  Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误。
  语法:
  redis 127.0.0.1:6379> LLEN KEY_NAME
  返回值:列表的长度。
  可用版本:>= 1.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','banana');  

$redis -> lPush('favorite_fruit','apple');  

  

($redis -> lLen('favorite_fruit'));     // int 3  
($redis -> lLen('fake_key'));           // 列表不存在,返回 int 0
  

  10、LRANGE
  Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
  (1)超出范围的下标值不会引起错误。
  (2)如果start下标比列表的最大下标end(LLEN list减去1)还要大,或者start > stop,LRANGE返回一个空列表。
  (3)如果stop下标比end下标还要大,Redis将stop的值设置为end。
  语法:
  redis 127.0.0.1:6379> LRANGE KEY_NAME START END
  返回值:一个列表,包含指定区间内的元素。
  可用版本:>= 1.0.0
  时间复杂度:O(S+N),S为偏移量start,N为指定区间内元素的数量。
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','banana');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','peach');  

$redis -> lPush('favorite_fruit','pineapple');  

$redis -> lPush('favorite_fruit','grape');  

  

($redis -> lRange('favorite_fruit',1,3));  

//   array (size=3)  
//      0 => string 'pineapple' (length=9)
  
//      1 => string 'peach' (length=5)
  
//      2 => string 'apple' (length=5)
  

  
($redis -> lRange('favorite_fruit',6,2));     // 当 start > end 时,返回空数组, array (size=0) empty
  

  
($redis -> lRange('favorite_fruit',0,100));   //
当 end 大于列表长度时,按 end 值计算  
//  array (size=6)
  
//      0 => string 'grape' (length=5)
  
//      1 => string 'pineapple' (length=9)
  
//      2 => string 'peach' (length=5)
  
//      3 => string 'apple' (length=5)
  
//      4 => string 'banana' (length=6)
  
//      5 => string 'cherry' (length=6)
  

  11、LREM
  Redis Lrem 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。
  COUNT 的值可以是以下几种:


  • count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
  • count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
  • count = 0 : 移除表中所有与 VALUE 相等的值。
  语法:
  redis 127.0.0.1:6379> LREM KEY_NAME COUNT VALUE
  返回值:被移除元素的数量。 列表不存在时返回 0 。
  可用版本:>= 1.0.0
  时间复杂度:O(N),N为列表的长度。
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','peach');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','grape');  

  

($redis -> lRem('favorite_fruit','apple',2));   // int 2    // 从开头向结尾方向移除 2 个  
($redis -> lRange('favorite_fruit',0,-1));
  

//array (size=4)  
//  0 => string 'grape' (length=5)
  
//  1 => string 'peach' (length=5)
  
//  2 => string 'apple' (length=5)
  
//  3 => string 'cherry' (length=6)
  

  
($redis -> lRem('favorite_fruit','apple',-1));   // int1    // 从结尾向开头方向移除 1 个
  
($redis -> lRange('favorite_fruit',0,-1));
  
//array (size=3)
  
//  0 => string 'grape' (length=5)
  
//  1 => string 'peach' (length=5)
  
//  2 => string 'cherry' (length=6)
  

  
($redis -> lRem('favorite_fruit','peach',0));   // int 1    // 移除所有的 value
  
($redis -> lRange('favorite_fruit',0,-1));
  
//array (size=2)
  
//  0 => string 'grape' (length=5)
  
//  1 => string 'cherry' (length=6)
  

  12、LSET
  Redis Lset 通过索引来设置元素的值。当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误。
  语法:
  redis 127.0.0.1:6379> LSET KEY_NAME INDEX VALUE
  返回值:操作成功返回 ok ,否则返回错误信息。
  可用版本:>= 1.0.0
  时间复杂度:对头元素或尾元素进行LSET操作,复杂度为O(1)。其他情况下,为O(N),N为列表的长度。
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','peach');  

$redis -> lPush('favorite_fruit','grape');  

  

($redis -> lSet('favorite_fruit','1','pineapple'));   // 将第一个元素替换为 pineapple  
($redis -> lRange('favorite_fruit',0,-1));
  

//  array (size=4)  
//      0 => string 'grape' (length=5)
  
//      1 => string 'pineapple' (length=9)
  
//      2 => string 'apple' (length=5)
  
//      3 => string 'cherry' (length=6)
  

  
($redis -> lSet('favorite_fruit','100','pitaya'));   // boolean false , 对索引超过范围进行设置,设置不成功
  
($redis -> lSet('fake_key',1,'mango'));              // boolean false , 对不存在的 key 进行设置,设置不成功
  

  13、LTRIM
  Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。(下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推)。
  (1)超出范围的下标值不会引起错误。
  (2)如果start下标比列表的最大下标end(LLEN list减去1)还要大,或者start > stop,LTRIM返回一个空列表(因为LTRIM已经将整个列表清空)。
  (3)如果stop下标比end下标还要大,Redis将stop的值设置为end。
  语法:
  redis 127.0.0.1:6379> LTRIM KEY_NAME START STOP
  返回值:命令执行成功时,返回 ok
  可用版本:>= 1.0.0
  时间复杂度:O(N),N为被移除的元素的数量。
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','peach');  

$redis -> lPush('favorite_fruit','grape');  

  

($redis -> ('favorite_fruit',1,-1));  

($redis -> lRange('favorite_fruit',0,-1));  

//  array (size=3)  
//      0 => string 'peach' (length=5)
  
//      1 => string 'apple' (length=5)
  
//      2 => string 'cherry' (length=6)
  

  
($redis -> ('favorite_fruit',1,10));   // end > list 的长度,那就将 stop 值设为 end
  
($redis -> lRange('favorite_fruit',0,-1));
  

//  array (size=2)  
//      0 => string 'apple' (length=5)
  
//      1 => string 'cherry' (length=6)
  

  
($redis -> ('favorite_fruit',7,1));        // start > end 或 start > stop , 清空整个 list
  
($redis -> lRange('favorite_fruit',0,-1));     // 返回 array (size=0) empty
  

  14、LINDEX
  Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
  语法:
  redis 127.0.0.1:6379> LINDEX KEY_NAME INDEX_POSITION
  返回值:列表中下标为指定索引值的元素。 如果指定索引值不在列表的区间范围内,返回 nil 。
  可用版本:>= 1.0.0
  时间复杂度:O(N),N为到达下标index过程中经过的元素数量, 对列表的头元素和尾元素执行LINDEX命令,复杂度为O(1)。
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','peach');  

$redis -> lPush('favorite_fruit','grape');  

  

($redis -> lIndex('favorite_fruit',2));     // string 'apple'  
($redis -> lRange('favorite_fruit',0,-1));  // 原 list 表不变
  
//  array (size=4)
  
//      0 => string 'grape' (length=5)
  
//      1 => string 'peach' (length=5)
  
//      2 => string 'apple' (length=5)
  
//      3 => string 'cherry' (length=6)
  

  15、LINSERT
  Redis Linsert 命令用于在列表的元素前或者后插入元素。 当指定元素不存在于列表中时,不执行任何操作。 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误。
  语法:
  redis 127.0.0.1:6379> LINSERT KEY_NAME BEFORE EXISTING_VALUE NEW_VALUE
  返回值:如果命令执行成功,返回插入操作完成之后,列表的长度。 如果没有找到指定元素 ,返回 -1 。 如果 key 不存在或为空列表,返回 0 。
  可用版本:>= 1.0.0
  时间复杂度:O(N),N 为寻找pivot过程中经过的元素数量。
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','peach');  

$redis -> lPush('favorite_fruit','grape');  

  

// The first case : 成功插入,返回列表长度  
($redis -> lInsert('favorite_fruit','before','apple','Mango'));     // int 5
  
($redis -> lRange('favorite_fruit',0,-1));
  

//array (size=5)  
//  0 => string 'grape' (length=5)
  
//  1 => string 'peach' (length=5)
  
//  2 => string 'Mango' (length=5)
  
//  3 => string 'apple' (length=5)
  
//  4 => string 'cherry' (length=6)
  

  
// The seconde case : 没有找到指定元素 ,返回 -1
  
($redis -> lInsert('favorite_fruit','before','not_exists','pitaya'));     // int -1
  
($redis -> lRange('favorite_fruit',0,-1));      // 原 list 不变
  
//array (size=5)
  
//  0 => string 'grape' (length=5)
  
//  1 => string 'peach' (length=5)
  
//  2 => string 'Mango' (length=5)
  
//  3 => string 'apple' (length=5)
  
//  4 => string 'cherry' (length=6)
  

  
// The third case : 没有找到指定元素 ,返回 0
  
($redis -> lInsert('fake_key','before','apple','watermelon'));     // int 0
  

  16、RPOPLPUSH
  Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回。
  语法:
  redis 127.0.0.1:6379> RPOPLPUSH SOURCE_KEY_NAME DESTINATION_KEY_NAME
  返回值:被弹出的元素。
  可用版本:>= 1.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','peach');  

$redis -> lPush('favorite_fruit','grape');  

  

// The first case : 若 source 和 desitination 相同,则尾旋转操作  
($redis -> rpoplpush('favorite_fruit','favorite_fruit'));   // cherry
  
($redis -> lRange('favorite_fruit',0,-1));
  

//array (size=4)  
//  0 => string 'cherry' (length=6)
  
//  1 => string 'grape' (length=5)
  
//  2 => string 'peach' (length=5)
  
//  3 => string 'apple' (length=5)
  

  
// The second case : 移动操作
  
($redis -> rpoplpush('favorite_fruit','other_list'));   // apple
  
($redis -> lRange('favorite_fruit',0,-1));
  
//array (size=3)
  
//  0 => string 'cherry' (length=6)
  
//  1 => string 'grape' (length=5)
  
//  2 => string 'peach' (length=5)
  

  
($redis -> lRange('other_list',0,-1));
  
//array (size=1)
  
//  0 => string 'apple' (length=5)
  

  17、BRPOPLPUSH
  Redis Brpoplpush 命令从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  (1)当给定列表source不为空时,BRPOPLPUSH的表现和RPOPLPUSH一样。
  (2)当列表source为空时,BRPOPLPUSH命令将阻塞连接,直到等待超时,或有另一个客户端对source执行LPUSH或RPUSH命令为止。
  (3)超时参数timeout接受一个以秒为单位的数字作为值。超时参数设为0表示阻塞时间可以无限期延长
  语法:
  redis 127.0.0.1:6379> BRPOPLPUSH LIST1 ANOTHER_LIST TIMEOUT
  返回值:假如在指定时间内没有任何元素被弹出,则返回一个 nil 和等待时长。 反之,返回一个含有两个元素的列表,第一个元素是被弹出元素的值,第二个元素是等待时长。
  可用版本:>= 2.0.0
  时间复杂度:O(1)
  具体实例:
  

<?php  

$redis = new redis();  

$redis -> connect('127.0.0.1',6379);  

$redis -> flushAll();  

  

$redis -> lPush('favorite_fruit','cherry');  

$redis -> lPush('favorite_fruit','apple');  

$redis -> lPush('favorite_fruit','peach');  

$redis -> lPush('favorite_fruit','grape');  

  

// The first case : 若 source 和 desitination 相同,则尾旋转操作  
($redis -> brpoplpush('favorite_fruit','favorite_fruit',2));   // cherry ,并没有返回 time
  
($redis -> lRange('favorite_fruit',0,-1));
  

//array (size=4)  
//  0 => string 'cherry' (length=6)
  
//  1 => string 'grape' (length=5)
  
//  2 => string 'peach' (length=5)
  
//  3 => string 'apple' (length=5)
  

  
// The second case : 移动操作
  
($redis -> brpoplpush('favorite_fruit','other_list',2));   // apple ,并没有返回 time
  
($redis -> lRange('favorite_fruit',0,-1));
  
//array (size=3)
  
//  0 => string 'cherry' (length=6)
  
//  1 => string 'grape' (length=5)
  
//  2 => string 'peach' (length=5)
  

  
($redis -> lRange('other_list',0,-1));
  
//array (size=1)
  
//  0 => string 'apple' (length=5)
  

  
($redis -> brpoplpush('fake_key','desination_key',2));  // 等待 2s 后返回 false  ,并没有返回 time
  

  下一篇:redis在php中的应用(Set篇)
  如有转载,请注明出处:http://www.cnblogs.com/chrdai/p/6841474.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426356-1-1.html 上篇帖子: 第9章 scrapy-redis分布式爬虫 下篇帖子: redis 在 php 中的应用(key篇)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表