1. Maven Configuration
<Dependency> <groupId>Redis.clients</groupId> <Artifactid>Jedis</Artifactid> <version>2.9.0</version></Dependency>
2. Java Operation Redis Data interface
PackageCom.coshaho.learn.redis;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportRedis.clients.jedis.Jedis;/*** * Myredislearn.java Create on November 4, 2017 4:40:39 * class function Description: Redis Java Interface Test * * Copyright:copyright (c) * Company:coshaho * @Version 1.0 * @Author Coshaho*/ Public classMyredislearn {PrivateJedis Jedis; Public Static voidMain (string[] args) {Myredislearn Redis=NewMyredislearn (); Redis.init (); Redis.teststring (); Redis.testmap (); Redis.testlist (); Redis.testset (); } Public voidinit () {Jedis=NewJedis ("127.0.0.1", 6379); Jedis.auth ("Coshaho"); } Public voidteststring () {//Setting the valueJedis.del ("string1"); Jedis.set ("String1", "Coshaho"); System.out.println (Jedis.get ("String1")); //string concatenationJedis.append ("string1", "Is my Name");//StitchingSystem.out.println (Jedis.get ("string1")); //Delete a stringJedis.del ("string1"); System.out.println (Jedis.get ("String1")); //set multiple key-value pairsJedis.del ("name"); Jedis.del ("Age"); Jedis.mset ("Name", "Coshaho", "Age", "28"); //plus 1 OperationJEDIS.INCR ("Age"); System.out.println (Jedis.get ("Name") + "-" + jedis.get ("age"))); } Public voidTestMap () {Jedis.del ("User"); Map<string, string> map =NewHashmap<string, string>(); Map.put ("Name", "Coshaho"); Map.put ("Age", "28"); Map.put ("Sex", "male"); Jedis.hmset ("User", map); //parameter 1 key parameter 2 filed parameter 3 fieldlist<string> Rsmap = jedis.hmget ("User", "name", "Age", "sex"); System.out.println (RSMAP); //Delete a key value from a mapJedis.hdel ("User", "age"); System.out.println (Jedis.hmget ("User", "age")); System.out.println (Jedis.hlen ("User")); System.out.println (Jedis.exists ("User")); System.out.println (Jedis.hkeys ("User")); System.out.println (Jedis.hvals ("User")); Iterator<String> Iter=jedis.hkeys ("User"). iterator (); while(Iter.hasnext ()) {String key=Iter.next (); SYSTEM.OUT.PRINTLN (Key+ ":" +jedis.hmget ("user"), key)); } } Public voidtestlist () {//Delete ListJedis.del ("Language"); System.out.println (Jedis.lrange ("Language", 0,-1)); Jedis.lpush ("Language", "中文版"); Jedis.lpush ("Language", "Chinese"); Jedis.lpush ("Language", "Japanese"); //The first one is key, the second is the starting position, the third is the end position, Jedis.llen gets the length-1 means get allSystem.out.println (Jedis.lrange ("Language", 0,-1)); Jedis.del ("Language"); Jedis.rpush ("Language", "Java"); Jedis.rpush ("Language", "C"); Jedis.rpush ("Language", "Python"); System.out.println (Jedis.lrange ("Language", 0,-1)); //bidirectional list, Jedis sortJedis.del ("a"); Jedis.rpush ("A", "1"); Jedis.lpush ("A", "6"); Jedis.lpush ("A", "3"); Jedis.lpush ("A", "9"); System.out.println (Jedis.lrange ("A", 0,-1)); System.out.println (Jedis.sort (A)); System.out.println (Jedis.lrange ("A", 0,-1)); } Public voidTestset () {Jedis.del ("B"); Jedis.sadd ("B", "Zhang San"); Jedis.sadd ("B", "John Doe"); Jedis.sadd ("B", "Harry"); Jedis.sadd ("B", "Win seven"); Jedis.sadd ("B", "segment Nine"); //removed fromJedis.srem ("B", "Win seven"); System.out.println (Jedis.smembers ("B")); System.out.println (Jedis.sismember ("B", "Win seven")); System.out.println (Jedis.srandmember ("B")); System.out.println (Jedis.scard ("B")); } }
3. Creating a Redis connection pool in Java
PackageCom.coshaho.learn.redis;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig; Public Final classRedispool {//Redis Server IP Private StaticString ADDR = "127.0.0.1"; //the port number of the Redis Private Static intPORT = 6379; //Access Password Private StaticString AUTH = "Coshaho"; //the maximum number of available connection instances, with a default value of 8;//If the assignment is-1, it is unrestricted, and if the pool is already assigned a max_total Jedis instance, the state of the pool is exhausted (exhausted) at this time. Private Static intMax_total = 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 Redis connection pool*/ Static { Try{jedispoolconfig config=NewJedispoolconfig (); Config.setmaxidle (Max_idle); Config.settestonborrow (Test_on_borrow); Config.setmaxtotal (max_total); Config.setmaxwaitmillis (max_wait); Jedispool=Newjedispool (config, ADDR, PORT, TIMEOUT, AUTH); } Catch(Exception e) {e.printstacktrace (); } } /*** Get Jedis instances *@return */ Public synchronized StaticJedis Getjedis () {Try { if(Jedispool! =NULL) {Jedis resource=Jedispool.getresource (); returnresource; } Else { return NULL; } } Catch(Exception e) {e.printstacktrace (); return NULL; } } /*** Release Jedis resources *@paramJedis*/ Public Static voidCloseFinalJedis Jedis) { if(Jedis! =NULL) {jedis.close ();; } } Public Static voidMain (string[] args) {Redispool.getjedis (). Set ("Pool", "Connection pool"); System.out.println (Redispool.getjedis (). Get ("Pool")); }}
Redis Entry--java Interface