Redis List命令

12 篇文章 0 订阅

lPush

添加一个字符串值到LIST容器的顶部(左侧),

如果KEY存在并且不是一个LIST容器,那么返回FLASE,如果成功返回List容器最新长度

$redis->delete('key1');
$redis->lPush('key1', 'C'); // returns 1
$redis->lPush('key1', 'B'); // returns 2
$redis->lPush('key1', 'A'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */

rPush

添加一个字符串值到LIST容器的底部(右侧)

如果KEY存在并且不是一个LIST容器,那么返回FLASE,如果成功返回List容器最新长度

$redis->delete('key1');
$redis->rPush('key1', 'A'); // returns 1
$redis->rPush('key1', 'B'); // returns 2
$redis->rPush('key1', 'C'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */

lPop

返回LIST顶部(左侧)的VALUE,并且从LIST中把该VALUE弹出。

取得VALUE成功,返回TURE。如果是一个空LIST则返回FLASE。

$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->lPop('key1'); /* key1 => [ 'B', 'C' ] */

rPop

返回LIST底部(右侧)的VALUE,并且从LIST中把该VALUE弹出

取得VALUE成功,返回TURE。如果是一个空LIST则返回FLASE。

$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->rPop('key1'); /* key1 => [ 'A', 'B' ] */

lSize

获取list容器的长度

如果KEY存在并且为LIST且有元素,那么返回KEY的长度,为空或者不存在返回0

如果指定的KEY的数据类型不是LIST或者不为空,那么返回FALSE

$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->lSize('key1');/* 3 */
$redis->rPop('key1'); 
$redis->lSize('key1');/* 2 */

lGet

根据索引值返回指定KEY LIST中的元素

如果指定了一个不存在的索引值,则返回FLASE

$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->lGet('key1', 0); /* 'A' */
$redis->lGet('key1', -1); /* 'C' */
$redis->lGet('key1', 10); /* `FALSE` */

lSet

根据索引值设置新的VAULE

Iset函数更像是UPDATE或者EDIT的概念,只能在LIST本身的长度范围内,而不能超出

如果设置成功返回TURE,如果KEY所指向的不是LIST,或者索引值超出LIST本身的长度范围,则返回flase。

$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->lGet('key1', 0); /* 'A' */
$redis->lSet('key1', 0, 'X');
$redis->lGet('key1', 0); /* 'X' */ 

lRange

取得指定索引值范围内的所有元素

$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C');
$redis->lRange('key1', 0, -1); /* array('A', 'B', 'C') */

lTrim

截取LIST中指定范围内的元素组成一个新的LIST并指向KEY,相当于将本身的部分值赋给本身,

$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C');
$redis->lRange('key1', 0, -1); /* array('A', 'B', 'C') */
$redis->lTrim('key1', 0, 1);
$redis->lRange('key1', 0, -1); /* array('A', 'B') */

lRem

IRem,IRemove函数,首先要去判断count参数,如果count参数为0,那么所有符合删除条件的元素都将被移除。如果count参数为整数,将从左至右删除count个符合条件的元素,如果为负数则从右至左删除count个符合条件的元素。

$redis->lPush('key1', 'A');
$redis->lPush('key1', 'B');
$redis->lPush('key1', 'C'); 
$redis->lPush('key1', 'A'); 
$redis->lPush('key1', 'A'); 

$redis->lRange('key1', 0, -1); /* array('A', 'A', 'C', 'B', 'A') */
$redis->lRem('key1', 'A', 2); /* 2 */
$redis->lRange('key1', 0, -1); /* array('C', 'B', 'A') */

lInsert

在指定LIST中的指定中枢VALUE的左侧或者右侧插入VALUE。如果这个LIST不存在,或者这个pivot(key position)不存在,那么这个VALUE不会被插入。

$redis->delete('key1');
$redis->lInsert('key1', Redis::AFTER, 'A', 'X'); /* 0 */

$redis->lPush('key1', 'A');
$redis->lPush('key1', 'B');
$redis->lPush('key1', 'C');

$redis->lInsert('key1', Redis::BEFORE, 'C', 'X'); /* 4 */
$redis->lRange('key1', 0, -1); /* array('A', 'B', 'X', 'C') */

$redis->lInsert('key1', Redis::AFTER, 'C', 'Y'); /* 5 */
$redis->lRange('key1', 0, -1); /* array('A', 'B', 'X', 'C', 'Y') */

$redis->lInsert('key1', Redis::AFTER, 'W', 'value'); /* -1 */

rpoplpush

从源LIST的最后弹出一个元素,并且把这个元素从目标LIST的顶部(左侧)压入目标LIST。

返回移动的元素,即源list中的最后一个元素

$redis->delete('x', 'y');

$redis->lPush('x', 'abc');
$redis->lPush('x', 'def');//array('def','abc')
$redis->lPush('y', '123');
$redis->lPush('y', '456');//array('456','123')

// move the last of x to the front of y.
var_dump($redis->rpoplpush('x', 'y'));//返回移动的x中最后一个元素abc
var_dump($redis->lRange('x', 0, -1));//array('def'),abc被弹出
var_dump($redis->lRange('y', 0, -1));//array('abc','456','123')





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值