Redis string-type API uses

Source: Internet
Author: User

Tag: SIM indicates flush--gets SWA port starts SETNX

 Packagecom.daxin.jedis_datastructure;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;ImportRedis.clients.jedis.Jedis;/*** Unit test for simple App.*/ Public classapptest {Jedis Jedis=NULL; @Before Public voidbefore () {Jedis=Redisutils.getjedis (); Jedis.flushdb ();} @After Public voidAfter () {jedis.close ();}/*** Simple Key/value settings*/@Test Public voidJedis_set_get () {//Set KeyJedis.set ("Redis_key", "Redis_value"); System.out.println (Jedis.get ("Redis_key"));//append to the specified key afterJedis.append ("Redis_key", "_redis_value"); System.out.println (Jedis.get ("Redis_key"));//The parameters are not explained//NX is set if it does not exist, otherwise it is not set. XX Set if present//ex represents seconds. PX represents milliseconds//The last parameter indicates how long the time expiresJedis.set ("Redis_key", "123456789", "XX", "EX", 500L); System.out.println (Jedis.get ("Redis_key")); Jedis.set ("123456789", "123456789", "NX", "EX", 500L); System.out.println (Jedis.get ("123456789"));}/*** There is no int type in Redis, a string is stored inside, and a string is transferred to int and then to string storage when int is added and minus .*/@Test Public voidJedis_incr_incrby () {System.out.println ("------------incrBy10------------");//Add tenLong r1 = Jedis.incrby ("Top", 10);//there is no int type in Redis, a string is stored in it, and a string is transferred to int and then to string storage when int is added and minus .System.out.println (R1); System.out.println ("------------incr------------");//plus 1R1 = JEDIS.INCR ("Top"); SYSTEM.OUT.PRINTLN (R1); System.out.println ("------------incrBy2------------"); R1= Jedis.incrby ("Top", 2); SYSTEM.OUT.PRINTLN (r1);} @Test Public voidJedis_decr_decrby () {Long R1= Jedis.incrby ("Top", 10); SYSTEM.OUT.PRINTLN (R1);//minus 1 OperationR1 = JEDIS.DECR ("Top"); SYSTEM.OUT.PRINTLN (R1);//minus 4 OperationsR1 = Jedis.decrby ("Top", 4); SYSTEM.OUT.PRINTLN (r1);} @Test Public voidJedis_getset () {/*** First get in Settings*/String R1= Jedis.getset ("Daxin", "first"); SYSTEM.OUT.PRINTLN (R1); R1= Jedis.getset ("Daxin", "first"); SYSTEM.OUT.PRINTLN (r1);} @Test Public voidJedis_setex ()throwsException {String R1= Jedis.setex ("Loginstate", 5, "yes"); SYSTEM.OUT.PRINTLN (R1);//return to OKSystem.out.println (Jedis.get ("Loginstate")); Thread.Sleep (6000);//SleepingSystem.out.println (Jedis.get ("loginstate"));//Expired}/*** The value of key is set only when key does not exist. * @throwsException*/@Test Public voidJedis_setnx ()throwsException {//The value of key is set only if key does not exist. Long r1 = jedis.setnx ("Top", "1");//return value 1 setting ook, 0 failure settingSystem.out.println (R1); R1=jedis.setnx ("Top", "2");//0 Failure SettingsSystem.out.println (r1);} @Test Public voidJedis_mget () {//set multiple key/value at once, must appear in pairsString r1 = Jedis.mset ("Daxin", "First", "La", "Laji"); SYSTEM.OUT.PRINTLN (R1); System.out.println (Jedis.get ("Daxin")); System.out.println (Jedis.get ("La"));}/*** Use the value parameter to write to the string value stored by the key, starting with offset offsets. */@Test Public voidJedis_range () {Jedis.set ("Top", "Top-k"); Jedis.setrange ("Top", 2, "*");//here is the overlay, not the insertSystem.out.println (Jedis.get ("Top"));}/*** Returns the length of the string value stored by key. */@Test Public voidJedis_strlen () {Jedis.set ("Top", "Top-k"); System.out.println (Jedis.strlen ("Top"));}/*** Set one or more key-value pairs at the same time, when and only if all given key does not exist. */@Test Public voidjedis_msetnx () {Jedis.set ("Top", "Top-k");//Note: All keys do not exist before they can be inserted, otherwise they are all not insertedJedis.msetnx ("Top", "Toptop", "111", "1111"); System.out.println (Jedis.get ("Top")); System.out.println (Jedis.get ("111"));}/*** This command is similar to the Setex command, but it sets the lifetime of the key in milliseconds, rather than the Setex command, in seconds. * @throwsException*/@SuppressWarnings ("Deprecation") @Test Public voidJedis_psetex ()throwsException {Jedis.psetex ("Top", 1000*60, "one Minute Lapse"); System.out.println (Jedis.get ("Top")); Thread.Sleep (1000*60); System.out.println (Jedis.get ("Top"));}}

Redis string-type API uses

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.