Jedis Operation redis--string Type

Source: Internet
Author: User

/*** String (String) * append,bitcount,bitop,bitfield,decr,decrby,get,getbit,getrange,getset,incr,incrby,incrbyfloat , Mget,mset,msetnx,psetex,set,setbit,setex,setnx,setrange,strlen*/ Public classStringtypetest {PrivateJedis Jedis; Private Static FinalString KEY = "Name"; Private Static FinalString VALUE = "Layman"; @Before Public voidsetUp () {jedisshardinfo info=NewJedisshardinfo ("192.168.133.188", 6379);  This. Jedis =NewJedis (info); Jedis.select (0); }    /*** SET key value [EX seconds] [PX milliseconds] [nx|     XX] * Associates string value values to key.     * If key already holds other values, the SET will overwrite the old value, ignoring the type.     * For a key with a time-to-live (TTL), when the SET command is successfully executed on this key, the original TTL of the key will be erased. * Optional Parameters * Starting with the Redis 2.6.12 version, the behavior of the SET command can be modified by a series of parameters: * EX Second: Set the key expiration time is second seconds.     The SET key value EX second effect is equivalent to Setex key second value. * PX millisecond: Set the key expiration time is millisecond milliseconds.     The SET key value PX millisecond effect is equivalent to Psetex key millisecond value. * NX: Sets the key only if the key does not exist.     The SET key value NX effect is equivalent to Setnx key value.     * XX: Only when the key already exists, the key is set operation. */@Test Public voidSET () {//Setting no key to name when setting its value to layman and expires after 15 secondsJedis.set (KEY, VALUE, "NX", "EX", 15);    Out (Jedis.get (KEY)); }    /*** MSET key value [key value ...] MGET key [Key ...] Returns the value of all (one or more) given key. If a key does not exist in the given key, the key returns a special value of nil.     Therefore, the command never fails.     * Set one or more key-value pairs at the same time.     * If a given key already exists, then MSET will overwrite the old value with the new value, if this is not the effect you want, consider using the MSETNX command: It will only be set if all the given key does not exist.     * MSET is an atomic (atomic) operation, all given keys are set at the same time, some given key is updated and other given key does not change, it cannot happen. */@Test Public voidMSET () {Jedis.mset ("Name", "Layman", "Age", "24"); Out (Jedis.mget ("Name", "Age")); }    /*** GET Key * Returns the string value associated with key.     * If key does not exist then return the special value nil.     * If the value stored by key is not a string type, an error is returned because GET can only be used to process string values. */@Test Public voidGET () {Out (Jedis.get (KEY)); }    /*** Getset Key value * Sets the value of the given key to value and returns the old value of the key.     * An error is returned when key exists but is not a string type. */@Test Public voidGetset () {SET (); Out (Jedis.getset (KEY,"Leo"));    GET (); }    /*** APPEND Key value * If key already exists and is a string, the APPEND command appends value to the end of the original value of the key.     * If key does not exist, APPEND simply sets the given key to value as if it were executing set key value. */@Test Public voidAPPEND () {SET (); Jedis.append ("Name", "APPEND");    GET (); }    /*** Setbit Key Offset value * Sets or clears a bit (bit) on the specified offset for the string value stored by key.     * bits are set or cleared depending on the value parameter, which can be either 0 or 1.     * When key does not exist, a new string value is automatically generated. * The string is stretched (grown) to ensure that it can save value at the specified offset.     When the string value is stretched, the blank position is filled with 0.     * The offset parameter must be greater than or equal to 0, less than 2^32 (bit mapping is limited to within MB). */@Test Public voidsetbit () {jedis.setbit (KEY,2l,true);    Bitcount (); }    /*** Bitcount key [start] [end] calculates the number of bits that are set to 1 in the given string.     * In general, the entire string given will be counted, and by specifying an additional start or end parameter, the count can be made only on a particular bit.     * The start and end parameters are set similarly to the GETRANGE command, and can be used with negative values: For example, 1 indicates the last byte, 2 is the penultimate byte, and so on.     * The nonexistent key is treated as an empty string, so a bitcount operation with a nonexistent key results in 0. */@Test Public voidBitcount () {Out (Jedis.bitcount (KEY)); }    /*** SETRANGE Key offset value * Overwrite (overwrite) the string value stored by the given key with the value parameter, starting at offset offsets.     * The nonexistent key is treated as a blank string. * The SETRANGE command ensures that the string is long enough to set the value at the specified offset, * if the given key is originally stored with a string length that is smaller than the offset (for example, the string is only 5 characters, but you set the offset to 10), * then the original character and the offset     The whitespace between shifts is populated with 0 bytes (zerobytes, "\x00"). */@Test Public voidSETRANGE () {SET (); Jedis.setrange (KEY,4L, "Leo");        GET (); /**Results * Layman * Laymleo*/    }    /*** GETRANGE Key Start end * Returns a substring of the string value in key, and the intercept range of the string is determined by the start and end two offsets (including start and end).     * Negative offset indicates the last count from the string, 1 for the last character, 2 for the penultimate, and so on. */@Test Public voidGETRANGE () {SET (); Out (Jedis.getrange (KEY,2,-3)); }    /*** Returns the length of the string value stored by key. */@Test Public voidSTRLEN () {SET ();    Out (Jedis.strlen (KEY)); }    /*** INCR Key * Increases the number of values stored in key.     * If key does not exist, then the value of key will be initialized to 0 before performing the INCR operation.     * Returns an error if the value contains the wrong type, or if the value of the string type cannot be represented as a number. */@Test Public voidINCR () {jedis.incr (KEY);    GET (); }    /*** Incrby key increment incrbyfloat key increment * Adds incremental increment to the value stored by key.     * If key does not exist, then the value of key will be initialized to 0 before executing the incrby command.     * Returns an error if the value contains the wrong type, or if the value of the string type cannot be represented as a number. */@Test Public voidIncrby () {Jedis.incrby (KEY,24);        GET (); Jedis.incrbyfloat (KEY,0.22225);    GET (); }    Private voidout (Object msg) {System.out.println (msg); }}

Jedis operation redis--string 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.