First build a non-tiled connection pool Jedispool object and write a method for configuring Redis connections.
/*** Build Redis Tile Connection Pool * *@paramIP *@paramPort *@returnJedispool*/ Public StaticJedispool Getjedispool () {if(Jedispool = =NULL) { synchronized(lock) {//IP and port for Redis serverString Redisip = propertiesutils.getproperties ("Redis_server_ip"); Integer Redisport= Integer.valueof (Propertiesutils.getproperties ("Redis_server_port")); if(Jedispool = =NULL) {jedispoolconfig config=NewJedispoolconfig (); //Set Connection pool initialization size and maximum capacity//controls how many Jedis instances a pool can allocate, obtained through Pool.getresource ();//If the assignment is-1, it is unrestricted, and if the pool is already assigned a maxactive Jedis instance, the state of the pool is exhausted (exhausted) at this time. Config.setmaxtotal (-1); //controls the maximum number of Jedis instances in a pool that have an idle (idle) state. Config.setmaxidle (1000); //indicates that when borrow (introduced) a Jedis instance, the maximum wait time, if the wait time is exceeded, is thrown directly jedisconnectionexception;Config.setmaxwaitmillis (1000 * 30); //whether validate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available;Config.settestonborrow (true); //WriteJedispool =Newjedispool (config, Redisip, redisport,default_time_out); } } } returnJedispool; }
We all know that Redis is a key,value type. When it's a memory database, it's usually used for data caching, after all, you put tens of millions of of your data into memory--(although that's what I'm going to do), get value from key
/** * Get Data * * @param key * @return * /public static string getforstring (String key) { list<string> values = mgetforstring (key); if (values = = null) { return null; } else { return values.get (0);} }
You can also get a collection of value based on key
/** * Get Data * * @param key * @return * /public static list<string> mgetforstring (String ... key) { list<string> value = null; Jedispool pool = null; Jedis Jedis = null; try { pool = Getjedispool (); Jedis = Pool.getresource (); Value = Jedis.mget (key); } catch (Exception e) { log.error (e); } finally { //returns to Connection Pool Returnjedisresource (Jedis); } return value; }
The method of loading data into Redis is typically set. As in the following methods, this specifies that value is of type string and because my business relation turns value into a JSON string ~
Public Static voidsetforstring (String key,string value) {Jedispool pool=NULL; Jedis Jedis=NULL; Try{Pool=Getjedispool (); Jedis=Pool.getresource (); Jedis.set (key, value); } Catch(Exception e) {log.error (e); } finally { //return to Connection poolReturnjedisresource (Jedis); } }
You can also get the fields and values of the hash structure
/*** Set the field and value of the hash structure *@paramKey *@paramvalue*/ Public Static voidSetforhashobj (String key, map<string, string>value) {Jedispool Pool=NULL; Jedis Jedis=NULL; Try{Pool=Getjedispool (); Jedis=Pool.getresource (); Jedis.hmset (key, value); } Catch(Exception e) {log.error (e); } finally { //return to Connection poolReturnjedisresource (Jedis); } }
Here the map can also be changed to list<map<string, string>> values, in fact the same ~ and then traverse this map can ~
Redis Common methods