1. 程式人生 > >redis 列表(list)函數

redis 列表(list)函數

head 超出 整數 == sam this uri out ESS

列表(list)函數

lPush 命令/方法/函數
Description

Adds the string value to the head (left) of the list. Creates the list if the key didn‘t exist. If the key exists and is not a list, FALSE is returned.

添加一個字符串值到LIST容器的頂部(左側),如果KEY不存在,曾創建一個LIST容器,如果KEY存在並且不是一個LIST容器,那麽返回FLASE。



Parameters

key

value String, value to push in key



Return value

LONG The new length of the list in case of success, FALSE in case of Failure.

返回LIST容器最新的長度,如果ADD成功。失敗則返回FALSE。



Examples

$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 命令/方法/函數
Description

Adds the string value to the tail (right) of the list. Creates the list if the key didn‘t exist. If the key exists and is not a list, FALSE is returned.

添加一個字符串值到LIST容器的底部(右側),如果KEY不存在,曾創建一個LIST容器,如果KEY存在並且不是一個LIST容器,那麽返回FLASE。



Parameters

key

value String, value to push in key



Return value

LONG The new length of the list in case of success, FALSE in case of Failure.

返回LIST容器最新的長度,如果ADD成功。失敗則返回FALSE。



Examples

$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‘ ] */
lPushx 命令/方法/函數
Description

Adds the string value to the head (left) of the list if the list exists.

添加一個VALUE到LIST容器的頂部(左側)如果這個LIST存在的話。



Parameters

key

value String, value to push in key



Return value

LONG The new length of the list in case of success, FALSE in case of Failure.

如果ADD成功, 返回LIST容器最新的長度。失敗則返回FALSE。



Examples

$redis->delete(‘key1‘);

$redis->lPushx(‘key1‘, ‘A‘); // returns 0

$redis->lPush(‘key1‘, ‘A‘); // returns 1

$redis->lPushx(‘key1‘, ‘B‘); // returns 2

$redis->lPushx(‘key1‘, ‘C‘); // returns 3

/* key1 now points to the following list: [ ‘A‘, ‘B‘, ‘C‘ ] */
rPushx 命令/方法/函數
Description

Adds the string value to the tail (right) of the list if the ist exists. FALSE in case of Failure.

添加一個VALUE到LIST容器的底部(右側)如果這個LIST存在的話。



Parameters

key

value String, value to push in key



Return value

LONG The new length of the list in case of success, FALSE in case of Failure.

如果ADD成功, 返回LIST容器最新的長度。失敗則返回FALSE。



Examples

$redis->delete(‘key1‘);

$redis->rPushx(‘key1‘, ‘A‘); // returns 0

$redis->rPush(‘key1‘, ‘A‘); // returns 1

$redis->rPushx(‘key1‘, ‘B‘); // returns 2

$redis->rPushx(‘key1‘, ‘C‘); // returns 3

/* key1 now points to the following list: [ ‘A‘, ‘B‘, ‘C‘ ] */
lPop 命令/方法/函數
Description

Return and remove the first element of the list.

返回LIST頂部(左側)的VALUE,並且從LIST中把該VALUE彈出。



Parameters

key



Return value

STRING if command executed successfully BOOL FALSE in case of failure (empty list)

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



Example

$redis->rPush(‘key1‘, ‘A‘);

$redis->rPush(‘key1‘, ‘B‘);

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

$redis->lPop(‘key1‘); /* key1 => [ ‘B‘, ‘C‘ ] */
rPop 命令/方法/函數
Description

Returns and removes the last element of the list.

返回LIST底部(右側)的VALUE,並且從LIST中把該VALUE彈出。



Parameters

key



Return value

STRING if command executed successfully BOOL FALSE in case of failure (empty list)

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



Example

$redis->rPush(‘key1‘, ‘A‘);

$redis->rPush(‘key1‘, ‘B‘);

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

$redis->rPop(‘key1‘); /* key1 => [ ‘A‘, ‘B‘ ] */
blPop, brPop 命令/方法/函數
Description

Is a blocking lPop(rPop) primitive. If at least one of the lists contains at least one element, the element will be popped from the head of the list and returned to the caller. Il all the list identified by the keys passed in arguments are empty, blPop will block during the specified timeout until an element is pushed to one of those lists. This element will be popped.

lpop命令的block版本即阻塞版本。如果LIST容器中有VAULE,將會返回ARRAY(‘listName‘‘element‘)。如果TIMEOUT參數為空,那麽如果LIST為空,blPop或者brPop將或結束調用。如果設置了timeout參數,blPop或者brPop將被掛起暫停運行TIMEOUT參數的時間,在此期間如果LIST被PUSH了元素,將在TIMEOUT時間結束後,被POP出來。



Parameters

ARRAY Array containing the keys of the lists INTEGER Timeout Or STRING Key1 STRING Key2 STRING Key3 ... STRING KeynINTEGER Timeout



Return value

ARRAY array(‘listName‘, ‘element‘)



Example

/* Non blocking feature */

$redis->lPush(‘key1‘, ‘A‘);

$redis->delete(‘key2‘);



$redis->blPop(‘key1‘, ‘key2‘, 10); /* array(‘key1‘, ‘A‘) */

/* OR */

$redis->blPop(array(‘key1‘, ‘key2‘), 10); /* array(‘key1‘, ‘A‘) */



$redis->brPop(‘key1‘, ‘key2‘, 10); /* array(‘key1‘, ‘A‘) */

/* OR */

$redis->brPop(array(‘key1‘, ‘key2‘), 10); /* array(‘key1‘, ‘A‘) */



/* Blocking feature */



/* process 1 */

$redis->delete(‘key1‘);

$redis->blPop(‘key1‘, 10);

/* blocking for 10 seconds */



/* process 2 */

$redis->lPush(‘key1‘, ‘A‘);



/* process 1 */

/* array(‘key1‘, ‘A‘) is returned*/
lSize 命令/方法/函數
Returns the size of a list identified by Key. If the list didn‘t exist or is empty, the command returns 0. If the data type identified by Key is not a list, the command return FALSE.

根據KEY返回該KEY代表的LIST的長度,如果這個LIST不存在或者為空,那麽ISIZE返回0,如果指定的KEY的數據類型不是LIST或者不為空,那麽返回FALSE。所以在這裏多說一句,當用ISize返回判斷值的時候,===就有用處了,這裏FLASE和0是兩個概念了。



Parameters

Key



Return value

LONG The size of the list identified by Key exists.

如果KEY存在並且為LIST且有元素,那麽返回KEY的長度,為空或者不存在返回0。



BOOL FALSE if the data type identified by Key is not list

如果KEY的數據類型不為空或者LIST,則返回FALSE。



Example

$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 命令/方法/函數
Description

Return the specified element of the list stored at the specified key. 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ... Return FALSE in case of a bad index or a key that doesn‘t point to a list.

根據索引值返回指定KEY LIST中的元素。0為第一個元素,1為第二個元素。-1為倒數第一個元素,-2為倒數第二個元素。如果指定了一個不存在的索引值,則返回FLASE。



Parameters

key index



Return value

String the element at this index

Bool FALSE if the key identifies a non-string data type, or no value corresponds to this index in the list Key.



Example

$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 命令/方法/函數
Set the list at index with the new value.

根據索引值設置新的VAULE



Parameters

key index value



Return value

BOOL TRUE if the new value is setted. FALSE if the index is out of range, or data type identified by key is not a list.

如果設置成功返回TURE,如果KEY所指向的不是LIST,或者索引值超出LIST本身的長度範圍,則返回flase。



Iset函數更像是UPDATE或者EDIT的概念,而不是INSERT或者ADD的概念,顧只能在LIST本身的長度範圍內,而不能超出。



Example

$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 命令/方法/函數
Returns the specified elements of the list stored at the specified key in the range [start, end]. start and stop are interpretated as indices: 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ...

取得指定索引值範圍內的所有元素。



Parameters

key start end



Return value

Array containing the values in specified range.



Example

$redis->rPush(‘key1‘, ‘A‘);

$redis->rPush(‘key1‘, ‘B‘);

$redis->rPush(‘key1‘, ‘C‘);

$redis->lRange(‘key1‘, 0, -1); /* array(‘A‘, ‘B‘, ‘C‘) */
lTrim 命令/方法/函數
Trims an existing list so that it will contain only a specified range of elements.

它將截取LIST中指定範圍內的元素組成一個新的LIST並指向KEY。簡短解說就是截取LIST。

這個可不是JS,或者PHP中清空空格的意思。



Parameters

key start stop



Return value

Array

Bool return FALSE if the key identify a non-list value.



Example

$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 命令/方法/函數
Removes the first count occurences of the value element from the list. If count is zero, all the matching elements are removed. If count is negative, elements are removed from tail to head.

IRem,IRemove函數,首先要去判斷count參數,如果count參數為0,那麽所有符合刪除條件的元素都將被移除。如果count參數為整數,將從左至右刪除count個符合條件的元素,如果為負數則從右至左刪除count個符合條件的元素。



Note: The argument order is not the same as in the Redis documentation. This difference is kept for compatibility reasons.

函數參數的順序不一定要一致,這樣做是為了保持兼容性。



Parameters

key

value

count



Return value

LONG the number of elements to remove

BOOL FALSE if the value identified by key is not a list.



Example

$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 命令/方法/函數
Insert value in the list before or after the pivot value. the parameter options specify the position of the insert (before or after). If the list didn‘t exists, or the pivot didn‘t exists, the value is not inserted.

在指定LIST中的指定中樞VALUE的左側或者右側插入VALUE。如果這個LIST不存在,或者這個pivot(key position)不存在,那麽這個VALUE不會被插入。



Parameters

key position Redis::BEFORE | Redis::AFTER pivot value



Return value

The number of the elements in the list, -1 if the pivot didn‘t exists.



Example

$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 命令/方法/函數
Pops a value from the tail of a list, and pushes it to the front of another list. Also return this value.

從源LIST的最後彈出一個元素,並且把這個元素從目標LIST的頂部(左側)壓入目標LIST。



Parameters

Key: srckey

Key: dstkey



Return value

STRING The element that was moved in case of success, FALSE in case of failure.



Example

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



$redis->lPush(‘x‘, ‘abc‘);

$redis->lPush(‘x‘, ‘def‘);

$redis->lPush(‘y‘, ‘123‘);

$redis->lPush(‘y‘, ‘456‘);



// move the last of x to the front of y.

var_dump($redis->rpoplpush(‘x‘, ‘y‘));

var_dump($redis->lRange(‘x‘, 0, -1));

var_dump($redis->lRange(‘y‘, 0, -1));



Output:

string(3) "abc"

array(1) {

  [0]=>

  string(3) "def"

}

array(3) {

  [0]=>

  string(3) "abc"

  [1]=>

  string(3) "456"

  [2]=>

  string(3) "123"

}

redis 列表(list)函數