Redis list Function

Source: Internet
Author: User

List Function

Lpush command/method/function descriptionadds 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. add a string value to the top (left) of the list container. If the key does not exist, a list container is created. If the key exists and is not a list container, flase is returned. Parameterskeyvalue string, value to push in keyreturn valuelong the new length of the list in case of success, false in case of failure. Return the latest length of the list container if the add operation is successful. Otherwise, false is returned. 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 command/method/function descriptionadds 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. add a string value to the bottom (right side) of the list container. If the key does not exist, a list container is created. If the key exists and is not a list container, flase is returned. Parameterskeyvalue string, value to push in keyreturn valuelong the new length of the list in case of success, false in case of failure. Return the latest length of the list container if the add operation is successful. Otherwise, false is returned. 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 command/method/function descriptionadds the string value to the head (left) of the list if the list exists. add a value to the top (left) of the list container if the list exists. Parameterskeyvalue string, value to push in keyreturn valuelong the new length of the list in case of success, false in case of failure. If add is successful, the latest length of the list container is returned. Otherwise, false is returned. 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 command/method/function descriptionadds the string value to the tail (right) of the list if the IST exists. false in case of failure. add a value to the bottom (right) of the list container if the list exists. Parameterskeyvalue string, value to push in keyreturn valuelong the new length of the list in case of success, false in case of failure. If add is successful, the latest length of the list container is returned. Otherwise, false is returned. 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 command/method/function descriptionreturn and remove the first element of the list. Return the value at the top (left) of the List, and bring the value up from the list. Parameterskeyreturn valuestring if Command executed successfully bool false in case of failure (empty list) gets the value successfully and returns true. If it is an empty list, flase is returned. Example $ redis-> rpush ('key1', 'A'); $ redis-> rpush ('key1', 'B '); $ redis-> rpush ('key1', 'C');/* key1 => ['A', 'B ', 'C'] */$ redis-> lpop ('key1');/* key1 => ['B', 'C'] */
Rpop command/method/function descriptionreturns and removes the last element of the list. Return the value at the bottom (right) of the List, and bring the value out from the list. Parameterskeyreturn valuestring if Command executed successfully bool false in case of failure (empty list) gets the value successfully and returns true. If it is an empty list, flase is returned. 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 command/method/function descriptionis 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 poppe D. The block version of The lpop command is the blocking version. If the list container contains vaule, array ('listname' 'element') is returned '). If the timeout parameter is null, if the list is empty, blpop or brpop will or will end the call. If the timeout parameter is set, blpop or brpop will be suspended for running the timeout parameter. During this period, if the list is pushed to an element, it will pop out after the timeout time ends. Parametersarray array containing the keys of the lists integer timeout or string key1 string key2 string key3... string keyninteger timeoutreturn valuearray 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 command/method/function 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. return the length of the list represented by the key based on the key. If the list does not exist or is empty, isize returns 0. If the data type of the specified key is not list or empty, then false is returned. So here I would like to say that when I use isize to return the judgment value, = is useful. Here, flase and 0 are two concepts. Parameterskeyreturn valuelong the size of the list identified by key exists. If the key exists and is a list and has elements, the length of the key is returned. If the key is null or does not exist, 0 is returned. Bool false if the data type identified by key is not list if the data type of the key is not empty or list, false is returned. 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 command/method/function descriptionreturn 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. returns the elements in the specified key list based on the index value. 0 is the first element, and 1 is the second element. -1 indicates the penultimate element, and-2 indicates the penultimate element. If a non-existent index value is specified, flase is returned. Parameterskey indexreturn valuestring the element at this indexbool 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 command/method/function set the list at index with the new value. set the new vauleparameterskey index valuereturn valuebool 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. if the setting is successful, true is returned. If the key is not directed to the list, or the index value exceeds the length range of the list, flase is returned. Iset functions are more like update or edit, rather than insert or add. They can only be within the length range of the list, but cannot exceed. 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 command/method/function 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... obtains all elements within the specified index value range. Parameterskey start endreturn valuearray 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 ')*/
The ltrim command/method/function trims an existing list so that it will contain in only a specified range of elements. It splits the elements within the specified range in the list into a new list and points to the key. The short description is to intercept the list. This is not JavaScript, or empty spaces in PHP. Parameterskey start stopreturn valuearraybool 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 command/method/function 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. the irem and iremove functions first determine the Count parameter. If the Count parameter is 0, all elements that meet the deletion criteria will be removed. If the Count parameter is an integer, the Count elements that meet the conditions are deleted from the left to the right. If the Count parameter is a negative number, the Count elements that meet the conditions are deleted from the right to the left. Note: The argument order is not the same as in the redis documentation. This difference is kept for compatibility reasons. The order of function parameters is not necessarily the same, so as to maintain compatibility. Parameterskeyvaluecountreturn valuelong the number of elements to removebool 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 command/method/function insert value in the list before or after the specified value. the Parameter options specify the position of the insert (before or after ). if the list didn't exists, or the specified didn't exists, the value is not inserted. insert value to the left or right of the specified central value in the specified list. If the list does not exist or the vertex (key position) does not exist, the value will not be inserted. Parameterskey position redis: Before | redis: After creating valuereturn valuethe number of the elements in the list,-1 if the specified 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 command/method/function pops a value from the tail of a list, and pushes it to the front of another list. also return this value. an element pops up at the end of the source list, and this element is pushed from the top (left) of the target list to the target list. Parameterskey: srckeykey: dstkeyreturn valuestring 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', '000000'); $ redis-> lpush ('y ', '123'); // 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 Function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.