Jedis Operation Redis--list Type

Source: Internet
Author: User

/*** List (listing) * Blpop,brpop,brpoplpush,lindex,linsert,llen,lpop,lpush,lpushx,lrange,lrem,lset,ltrim, RPOP, Rpoplpush,rpush,rpushx*/ Public classListtypetest {PrivateJedis Jedis; Private Static FinalString KEY = "List"; Private Static FinalString VALUE = "Layman"; @Before Public voidsetUp () { This. Jedis =NewJedis (NewJedisshardinfo ("192.168.133.188", 6379)); }    /*** Lpush key value [value ...] Insert one or more value values into the table header of the list key * If there are multiple value values, then each value value is inserted into the header sequentially from left to right: for example, to execute a command on an empty list mylist Lpush mylist a b C, the value of the list     will be c b A, * this equates to the atomic execution of Lpush mylist A, Lpush mylist B and Lpush mylist c three commands.     * If key does not exist, an empty list is created and the Lpush operation is performed.     * <p/> * LPUSHX Key value * Inserts value values into the table header of the list key when and only if key exists and is a list.     * As opposed to the Lpush command, when key does not exist, the LPUSHX command does nothing.     * <p/> * Rpush key value [value ...] inserts one or more value values into the footer of the list key (rightmost).     * <p/> * RPUSHX Key value * Inserts values value into the footer of the list key when and only if key exists and is a list.     * As opposed to the Rpush command, when key does not exist, the RPUSHX command does nothing. */@Test Public voidLpush () {Jedis.lpush (KEY, value, value+ "1");    Lrange (); }    /*** Lpop Key * Removes and returns the header element of the list key.     * <p/> * Rpop key * To remove and return the tail element of the list key. */@Test Public voidLpop () {lrange ();        Jedis.lpop (KEY);    Lrange (); }    /*** Blpop key [key ...] timeout * Blpop is the list of blocked (blocking) pop-up primitives.     * It is a blocked version of the Lpop command, and the connection will be blocked by the Blpop command until a wait time-out or a popup element is found when no elements in the given list are available to eject.     * When a number of key parameters are given, check each list in the order of key parameters, and pop up the first non-empty list header element.     * Non-blocking behavior: * When Blpop is called, if there is at least one non-empty list in the given key, the first non-empty list that is encountered pops up the head element, and together with the name of the list to which the popup element belongs, the result is returned to the caller.     * When there are more than one given key, blpop the order of the given key parameters, and then checks each list in turn. * Assuming there are three lists of job, command, and request, where job does not exist, command and request both hold a non-empty list. Consider the following command: Blpop job command Request 0 * <p/> * Timeout parameter timeout takes a number in seconds as the value.     Setting the timeout parameter to 0 indicates that the blocking time can be extended indefinitely (block indefinitely).      * <p/> * brpop key [key ...] timeout * It is a blocked version of the Rpop command, and when no element in the given list is available to eject, the connection will be blocked by the Brpop command until the wait time-out or discovery can eject the element.     * Brpop In addition to the location of the popup element and the Blpop are different, the other performance is consistent. */@Test Public voidBlpop ()throwsinterruptedexception {lrange (); //The returned list first element is the key value of the return value list, and the second element is the value returnedlist<string> Blpop = Jedis.blpop (5, KEY);        System.out.println (Blpop); Jedis.blpop (5, KEY); }    /*** Rpoplpush Source Destination * Command Rpoplpush in an atomic time, perform the following two actions: * 1, the last element in the list source (the tail element) pops up and returns to the client.     * 2, insert the source popup element into the list destination, as the head element of the destination list. * For example, you have two lists of source and destination, the source list has elements a, B, C, destination list has element x, Y, z, * Execute Rpoplpush Source Destinat     After ion, the source list contains elements a,b,destination the list contains elements c, x, Y, z, and element c is returned to the client.     * If source does not exist, the value nil is returned and no other action is performed. * If source and destination are the same, the footer element in the list is moved to the table header and returned to this element, which can be viewed as a rotation of the list (rotation) * <p/> * Brpoplpush source de     Stination Timeout * Brpoplpush is a blocking version of Rpoplpush, and Brpoplpush behaves like Rpoplpush when the given list source is not empty.     * When the list source is empty, the Brpoplpush command blocks the connection until the wait times out, or if another client executes the Lpush or Rpush command against the source. * Timeout parameter timeout takes a number in seconds as the value.     Setting the timeout parameter to 0 indicates that the blocking time can be extended indefinitely (block indefinitely). */@Test Public voidRpoplpush () {Lpush ();        Jedis.rpoplpush (key, key);    Lrange (); }    /*** LINDEX Key index * Returns the element in the list key, subscript as index.     * Subscript (Index) parameter start and stop all base on 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.     * You can also use a negative subscript, 1 for the last element of the list, 2 for the penultimate element of the list, and so on.     * If key is not a list type, an error is returned. */@Test Public voidLINDEX () {System.out.println (Jedis.lindex (KEY,1)); }    /*** LSET Key index value * Sets the values of the elements of the list key labeled Index to value.     * An error is returned when the index parameter is out of range, or an empty list (key does not exist) is LSET.     * For more information on List subscripts, please refer to the LINDEX command. */@Test Public voidLSET () {lrange (); Jedis.lset (KEY,0, VALUE + "0");    Lrange (); }    /*** Linsert Key before|     After Pivot value * Inserts a value in the list key, either before or after the value pivot.     * No action is taken when pivot does not exist in the list key.     * When key does not exist, key is treated as an empty list, and no action is taken.     * If key is not a list type, an error is returned. */@Test Public voidLinsert () {lrange (); Jedis.linsert (KEY, binaryclient.list_position. After, VALUE,"Insert");    Lrange (); }    /*** LTRIM key start stop * trims a list (trim), which means that the list retains only the elements within the specified interval, and the elements that are not within the specified range are deleted.     * For example, execute the command LTRIM list 0 2, which means that only the first three elements of the list are preserved, and all the remaining elements are deleted.     * Subscript (Index) parameter start and stop all base on 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.     * You can also use a negative subscript, 1 for the last element of the list, 2 for the penultimate element of the list, and so on. */@Test Public voidLTRIM () {lrange (); Jedis.ltrim (KEY,0, 1);    Lrange (); }    /*** Lrem Key Count value * Removes the element in the list that is equal to the parameter value, based on the value of the parameter count.     * The value of count can be one of the following: * count > 0: Searches from the header to the end of the table, removes the element equal to value and counts as count.     * Count < 0: Searches from the end of the table to the header, removing the element equal to value, the number is the absolute value of count.     * Count = 0: Removes all values that are equal to value in the table. */@Test Public voidLrem () {Jedis.rpush (KEY,"A", "B", "a", "C", "a", "D");        Lrange (); Jedis.lrem (KEY,-2, "A");    Lrange (); }    /*** Llen Key * Returns the length of the list key.     * If key does not exist, then key is interpreted as an empty list, returning 0.     * If key is not a list type, an error is returned. */@Test Public voidLlen () {System.out.println (Jedis.llen (KEY)); }    /*** Lrange key Start Stop * Returns the element within the specified interval in the list key, with the interval specified by the offset start and stop.     * Subscript (Index) parameter start and stop all base on 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.     * You can also use a negative subscript, 1 for the last element of the list, 2 for the penultimate element of the list, and so on. */@Test Public voidLrange () {System.out.println (Jedis.lrange (KEY,0,-1)); }}

Jedis Operation Redis--list Type

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.