[Java] View plain copy package redis.clients.jedis.tests; import java.text.parseexception; import java.util.iterator; import java.util.List; import java.util.set; import org.junit.afterclass; import org.junit.beforeclass; import org.junit.test; import redis.clients.jedis.hostandport; import redis.clients.jedis.jedis; import redis.clients.jedis.protocol; import redis.clients.jedis.scanparams; import redis.clients.jedis.scanresult; import redis.clients.jedis.params.set.setparams; public class jedisallmethodtest { static jedis jedis = null; static hostandport hnp = new hostandport("10.0.50.11", protocol.default_port); /** * initializing connections * @author jackson * @date 2015-12-16 morning 8:54:01 * @throws Exception * @return void */ @BeforeClass // junit provides @beforeclass label, beforeclass tag only once public static void setup () throws Exception { //the first way to connect jedis jedis = new jedis (Hnp.gethost (), hnp.getport (), 500); jedis.connect (); Jedis.auth ("foobared");// redis auth command Verification redis password, need to open in redis.conf config file requirepass foobared Parameters, Jedis.configset ("timeout", "+");// redis config command jedis.flushall ();// redis flushall command Clear all key and clear all data //the second way to connect Jedis /* use uri Connect database jedis = new jedis (New URI (" REDIS://:FOOBARED@10.0.50.11:6379/4 "), 15000,15000);// 4 indicates that the connection is 5th database url, Time-out, Input stream timeout time &nbsP; jedis.connect (); jedis.flushall ();// redis flushall command Clear all key and clear all data */ // Third Way to connect Jedis /* using jedisShardInfo connecting redis Jedisshardinfo jedisshardinfo = new jedisshardinfo ("REDIS://:FOOBARED@10.0.50.11:6379/1") ;// /1 indicates that the connection is second database ;foobared database password jedisshardinfo.setconnectiontimeout (150000); Jedisshardinfo.setsotimeout (150000); jedis = New jedis (Jedisshardinfo),   jedis.connect (); jedis.flushall ();// redis flushall command Clear all key and clear all data */ } /** * Close connection * @author jackson * @date 2015-12-16 Morning 8:54:34 * @ return void */ @AfterClass public static void teardown () { // when tagged as afterclass beforeclass Time method must be static before after not required  &NBsp; jedis.disconnect ();// is ultimately called in Connection disconnect function is to close the stream, close socket } /** * redis to string and key operation demo * @author jackson * @date 2015-12-17 pm 1:15:10 * @throws interruptedexception * @throws ParseException * @return void */ /** * @author jackson * @date 2015-12-17 pm 1:48:00 * @throws interruptedexception * @throws ParseException * @ return void */ @Test public void jedisstringkeytest () throws interruptedexception, ParseException{ system.out.println ("= = = Test jedis begin=== ") // sotimeout set the time-out for the socket call InputStream read Data jedis.set ("Test", "test"); system.out.println (Jedis.get ("Test")); jedis.append ("Test", "Test2");// is ultimately converting the string to a byte array written in the form of a stream system.out.println (Jedis.get ("Test")); /* * nx| xx, nx --has this parameter only set does not exist key, if the key set value that already exists does not take effect, xx -- This parameter only sets the value of the key that already exists, does not exist * ex| px, key time of existence: ex = seconds; px = milliseconds * */ // Verification NX XX string status = jedis.set ("Test", "test234", Setparams.setparams (). NX ());// status to null The operation fails system.out.println ("Test setparams.setparams () . NX () to the already existing Key set value operation status: "+status + " Action Value "+jedis.get (" test "); // Output value TESTTEST2 &NBsp status = jedis.set ("Test1", "test2222", Setparams.setparams (). NX ());//status for ok System.out.println ("Test Setparams.setparams (). NX () gives non-existent Key set value operation status operation status:" +status + Operation value +jedis.get ("Test1"));// output value is test2222 status = jedis.set ("Test1", " Test342222 ", Setparams.setparams (). XX ());// status for ok system.out.println (" Test Setparams.setparams (). XX () to an already existing Key set value operation state Operation Status: "+status + " Action Value "+jedis.get (" test1 ");// output value is test342222 status = jedis.set ("test21", "test34212222", Setparams.setparams (). XX ());// status for null system.out.println (" Test Setparams.setparams (). XX () to Key set value not present operation status operation status operation status: "+status + " Action Value "+ Jedis.get ("test21"));// output value is null // Verification ex| px status = jedis.set ("TestEX", " TestEX ", Setparams.setparams (). EX (1)); System.out.println ("Test ex operation Status:" +status+ "Action Value:" +jedis.get ("TestEX"));// output testex thread.sleep (1002); system.out.println ("Test ex 1 seconds after the automatic destruction, output null operation status:" +status+ "Action Value:" + Jedis.get ("TestEX"));// 1 seconds after automatic destruction, output null status = Jedis.set ("testpx", "TestEX", Setparams.setparams (). PX (+)); system.out.println ("Test px operation Status:" +status+ "Action Value:" +jedis.get ("TESTPX"));// Output testEX thread.sleep (101); system.out.println ("Test px 1 seconds after the automatic destruction operation status:" +status+ "Action Value: "+jedis.get (" TESTPX "));// 1 seconds after automatic destruction, output null // Note: because the set command can be used with parameters to achieve and setnx , setex , and psetex three command effects, As a result, future versions of redis may be discarded and eventually removed, setnx , setex , and psetex three commands //setnx sets the value of key to value , when and only if key does not exist. If a given key already exists, then setnx does not do any action. SETNX is "set if not exists" (if not present, SET) shorthand // similar to set (KEY,VALUE,NX) // when key is not present long n = jedis.setnx ("Key6", "Value6"); system.out.println (n!=0? ") set Success "+jedis.get (" Key6 "):" set failed "); // set successfully returned 1 set failed to return 0 // when key is present n = jedis.setnx ("Key6", "Value6");          SYSTEM.OUT.PRINTLN (n!=0? ") set Success "+jedis.get (" Key6 ") +n:" set failed "+jedis.get (" Key6 ") +n); // setex Similar to execute set (key,value) in execution EXPIRE similar to set (KEY,VALUE,EX) set the time to live is second level status = jedis.setex ("Key7", 1, "Value7"); system.out.println (Status+jedis.get ("Key7")); thread.sleep (; ) System.out.println (Jedis.get ("Key7")); Jedis.psetex ("Key20", 10, "Value20");// sets the time-to-live for milliseconds, similar to set (KEY,VALUE,PX) // exists Checks if a given key (multiple key values) exists. return value: if All key does not exist, return 0 , if there is then return The number of key exists system.out.println ("= = = Test exists command: ===="); n = jedis.exists ("Test1 test test2"). Split (" ")); // int count = "Test1 test test2". Split (" ") .length - (int) n; system.out.println ( n != 0 ? " key exist Number: "+n+" non-existent number: "+count:" key does not exist "+n"; // exist Check Whether a single key value exists return value: if the key value exists returns  TRUE  Return to false boolean exists = jedis.exists ("test");// return true          SYSTEM.OUT.PRINTLN (exists); // del deletes the given one or more key . Non-existent key will be ignored return value: returns the number of deleted key , system.out.println ("= = = Test del command:=== "); n = jedis.del ("Test1 test test2". Split (" ")); system.out.println ("Delete key&nbThe number of SPS is "+n"; system.out.println (Jedis.get (" Test ") +jedis.get (" Test1 "));// return null description has been removed jedis.set ("Testdel", "Testdel"); n = jedis.del ("TestDel") ); system.out.println ("The number of deleted key is" +n); // type commands System.out.println ("= = = Test type command:=== "); string type = jedis.type ("test"); // test does not exist returns none         SYSTEM.OUT.PRINTLN ("Storage type:" +type); jedis.set ("Testtype", "Testtype"); type = jedis.type ("Testtype"); // test Returns string system.out.println ("Storage type:" +type); jedis.flushall (); // mset command batch insert Insert all values if key value already exists, overwrite insert success return OK Always insert success
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.