Java operation Redis requires importing two jars:
- Commons-pool2-2.4.2.jar
- Jedis-2.1.0.jar
Packagecom.chinasofti.test;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.logging.Logger;ImportOrg.junit.Before;Importorg.junit.Test;ImportRedis.clients.jedis.Jedis;/*** Java Connectivity redis Test *@authorLiuhl **/ Public classConnredis {Jedis Jedis; @Before Public voidinit () {Jedis=NewJedis ("localhost", 6379, 30); Jedis.auth ("Admin"); } /*** Test Redis operations on Strings * SET (): Add a new String * APPEND (): Append value to existing key * DEL (): Delete a key * Mset (): Set multiple key-value pairs * INCR (k): +1 operation is possible *@authorLiuhl*/@Test Public voidtestredisstring () {Jedis.set ("username", "SMith kevens"); Jedis.append ("Username", "a Redis developer"); System.out.println (Jedis.get ("username") + "------------"); //Delete a keyJedis.del ("Date"); System.out.println (Jedis.get ("Date") + "======"); //set multiple key-value pairsJedis.mset ("Nicheng", "Shubiao", "Qage", "3", "Qqnumber", "906058683", "Sex", "man"); JEDIS.INCR ("Qage");//Perform +1 operationsSystem.out.println (Jedis.get ("Nicheng") + ":" +jedis.get ("qage") + ":" +jedis.get ("Qqnumber") + ":" +jedis.get ("Sex")); } /*** Redis action on List * Llen (): Get list Size * Lpush (): Insert element to left of list * Rpush (): Insert element to right of list * Lrange ("", 0,-1): Get list With elements *@authorLiuhl*/@Test Public voidtestredislist () {//remove all the content before you startJedis.del ("Javaframe work"); System.out.println (Jedis.lrange ("Java framework", 0, 1)); //define a list of frame work and add three data to itJedis.lpush ("Javaframe work", "Struts2"); Jedis.lpush ("Javaframe work", "Spring"); Jedis.lpush ("Javaframe work", "Hibernate"); System.out.println (Jedis.llen ("Javaframe work") + "one"); System.out.println (Jedis.lrange ("Javaframe work", 0,-1)); //add an element to the right of the listJedis.rpush ("Javaframe work", "Ibatis"); Jedis.rpush ("Javaframe work", "MyBatis"); Jedis.rpush ("Javaframe work", "SPRINGMVC"); System.out.println (Jedis.lrange ("Javaframe work", 0,-1)); } /***jedis Action unordered list (Set) *sadd (): Inserts an element into an out-of-order listing *smembers (): Gets all added value *srem (): Remove *sismember (): Determine if a value is a set Returns a Boolean *scard (): Returns the number of collection elements *srandmember (): Returns a random element in the collection *@authorLiuhl *@returnvoid * @category*/@Test Public voidTestredisset () {//inserting an element into an unordered collectionJedis.sadd ("User", "Yanbao"); Jedis.sadd ("User", "Chengyi"); Jedis.sadd ("User", "Fanwei"); //removed fromJedis.srem ("User", "Chengyi"); System.out.println (Jedis.smembers ("User")); System.err.println (Jedis.sismember ("User", "Yunfei")); System.out.println (Jedis.scard ("User")); System.out.println (Jedis.srandmember ("User")); } /*** Test List sort * Redis:lists is a doubly linked list * Before sorting out lists, then adding data test *@exceptioninterruptedexception: Check for exceptions *@authorLiuhl*/@Test Public voidTestlistsort ()throwsinterruptedexception{Jedis.del ("Arr"); Jedis.lpush ("Arr", "4"); Jedis.lpush ("Arr", "78"); Jedis.lpush ("Arr", "2"); Jedis.lpush ("Arr", "9"); Jedis.lpush ("Arr", "7"); Jedis.rpush ("Arr", "120"); Jedis.rpush ("Arr", "91"); Jedis.rpush ("Arr", "88"); Jedis.rpush ("Arr", "31"); Jedis.rpush ("Arr", "10"); System.out.println ("Before Sorting:" +jedis.lrange ("arr", 0,-1)); System.out.println ("After sorting:" +jedis.sort ("arr")); } /*** * Test Redis ordered Collection * Com.chinasofti.test * Method Name: Testredisshortzset * Creator: Liuhl * Time: 2017-4-5-AM 1 0:15:33 * return:void *@exception * @since1.0.0 * ZADD (): Inserts an element into an ordered collection (sorts zsets) * Zrange (): Lists all element values only. */@Test Public voidTestredisshortzset () {//inserting elements into an ordered collection sorts setsJedis.zadd ("Lhlzset", 6, "http://localhost:8080"); Jedis.zadd ("Lhlzset", 6, "sort"); //list only elements of an ordered collectionSystem.out.println (Jedis.zrange ("Lhlzset", 0, 1)); System.out.println (Jedis.zrangebyscore ("Lhlzset", 0, 6)); } /*** * Test HASHS Hash * com.chinasofti.test * method Name: TESTHASHS * Creator: Liuhl * Time: 2017-4-6-9:46:04 * Return:void *@exception * @since1.0.0 * Hmset (): Establish HASHS and assign value * Hmget (): List HASHS related*/@Test Public voidTesthashs () {//establish HASHS and assign valuesmap<string, string> map =NewHashmap<>(); Map.put ("Capital", "Beijing"), Map.put ("Henan", "Luoyang"), Map.put ("Shaanxi", "Xi ' an"); Jedis.hmset ("China", map); //List the contents of HashsSystem.out.println (Jedis.hmget ("China", "Henan")); }}
Packagecom.chinasofti.test;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;/*** Redis Connection Pool * *@authorAdministrator **/ Public classRedisutils {//Redis Server IP Private StaticString ADDR = "127.0.0.1"; //Redis Port number Private Static intPORT = 6379; //Access Password Private StaticString AUTH = "admin"; /*** Maximum number of available connection instances, default is 8, if assignment is-1, it means no limit, * If pool is already assigned maxactive Jedis instance, then the state of pool is exhausted (exhausted). */ Private Static intMax_active = 1024; //controls the maximum number of Jedis instances in a pool that have an idle (idle) state, and the default value is 8. Private Static intMax_idle = 200; //The maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means that never times out. If the waiting time is exceeded, the jedisconnectionexception is thrown directly; Private Static intMax_wait = 10000; Private Static intTIMEOUT = 10000; //whether validate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available; Private Static BooleanTest_on_borrow =true; Private StaticJedispool Jedispool =NULL; //Initializing the connection pool Static { Try{jedispoolconfig config=NewJedispoolconfig (); Config.setmaxactive (max_active); Config.setmaxidle (Max_idle); //config.setmaxwait (max_wait);Config.settestonborrow (Test_on_borrow); //Jedispool = new Jedispool (config, ADDR, PORT, TIMEOUT, AUTH); //Note these two behaviors are caused by the JDK version problem, which can be ignored}Catch(Exception e) {e.printstacktrace (); } } /*** Get Redis instances*/ Public synchronized StaticJedis Getjedis () {Try { if(Jedispool! =NULL) {Jedis Jedis=Jedispool.getresource (); returnJedis; } Else { return NULL; } } Catch(Exception e) {e.printstacktrace (); return NULL; } } /** * * @paramJedis*/ Public Static voidReturnresource (FinalJedis Jedis) { if(Jedis! =NULL) {Jedispool.returnresource (Jedis);//The close operation has been processed here } } }
Java uses Jedis to manipulate Redis Daquan