Redis learning (c) list

I. Overview

    In the Redis, List type of insertion order are sorted string list. And data structures in general list, we can and tail (right) to add a new element in its head (left). When inserted, if the key does not exist, Redis will create a new list for that key. On the contrary, if all the elements in the list are removed, then the key will also be deleted from the database. List the number of elements can be included in the maximum is 4294967295.
      From the perspective of efficiency element insertion and deletion of view, if we are to insert or remove elements at both ends of the list, this will be a very efficient operation, even though the list has been stored millions of records, this operation can also be in constant time completed within. However, it should be noted that, if the element insert or delete operation is applied to the middle of the list, it would be very inefficient. 

Two, list operation command

    Example: lpush key val

    Usage: Insert a list of values ​​to the left (head)

    Returns: execution lpush command, the length of the list.

 

    Example: rpush key val

    Usage: inserts a value to the right of the list (tail)

    Returns: execution rpush command, the length of the list.

 

    Example: lpushhx key val

    Usage: inserts a value to the left of the list (the head), if the value does not exist when no action

    Returns: execution command, the length of the list.

 

    Example: rpushhx key val

    Usage: inserts a value to the right of the list (tail), if the value does not exist when no action

    Returns: execution command, the length of the list.

 

    Example: lpop key

    Directions: Head to remove a value, and that value is returned

    Returns: there is a list of the first value is returned, there is no return nil

 

    Example: rpop key

    Usage: remove the tail of a value and returns the value

    Returns: there is a list of the last value is returned, there is no return nil

 

    Example: rpop key

    Usage: remove the tail of a value and returns the value

    Returns: there is a list of the last value is returned, there is no return nil

 

    Example: llen key

    Usage: Returns the length of the list. If the list key does not exist, then the key is interpreted as an empty list, return 0. If the key is not the type of list, an error is returned.

    Returns: the length of the list

 

    Example: lrem key count val

    Usage: remove the elements in the list count value of the tail val

    Return: count is greater than 0: search element from the beginning to count the value val and remove

               count is less than 0: Start from the end of a count value of the search elements and remove val

               count is equal to 0: Remove all the elements in the list to val

               Finally Returns the number removed

 

    Example: lrange key start end

    Usage: Remove the element from the list to the start end of the range, the offset range specified in the START and END. Wherein 0 indicates the first element in the list, a list showing the second element, and so forth. You can also use negative subscripts to the last element of the list represented -1, -2 represent the penultimate element of the list, and so on.

    Returns: Returns a list containing a range of elements

 

    Example: ltrim key start end

    Usage: For a list of trim (trim), that is, make a list of only the specified retention element within the range of elements within the range of not specified will be deleted.

              The first element index 0 for list 1 indicates the second element of the list, and so on. You can also use negative index to an element of the list indicates the most -1, -2 represent the penultimate element of the list, and so on.

    Returns: operation successful return OK

 

    Example: linsert befort | after pivot val

    Usage: for a list of elements before or after insertion elements. When the specified element does not exist in the list, it does not perform any operation.

               When the list does not exist, it is regarded as an empty list, no action.

               If the key is not the type of list, an error is returned.

    Returns: Returns the length of the list of successful operation. List does not exist returns 0, val absence -1

 

    Example: L / Rpoppush sourcekey destkey

    Usage: Used to remove the last element of the list, and adds the element to another list and return.

    Returns: the pop-up element values

127.0.0.1:6379> lpush message update clean full ctrl_switch close   一次添加多个val到list中
(integer) 5
127.0.0.1:6379> lrange message 0 -1                  查询一个范围内的值,返回了一个列表
1) "close"
2) "ctrl_switch"
3) "full"
4) "clean"
5) "update"
127.0.0.1:6379> linsert message after full change   向指定的val之后插入一个值
(integer) 6
127.0.0.1:6379> lrange message 0 -1                    在full的after之后存在了change这个值
1) "close"
2) "ctrl_switch"
3) "full"
4) "change"
5) "clean"
6) "update"
127.0.0.1:6379> 

 

Published 22 original articles · won praise 9 · views 8817

Guess you like

Origin blog.csdn.net/ljm_c_bok/article/details/104845889