There is an example of spring-based Redis, which gets Jedis instances from jedisconnectionfactory . The rest of the section ignores only Redisservice, so there are many problems with getting Jedis instances.
This way, each new connection is innovated without closing the connection. The Redis connection will be full soon when there is a lot of operation. reported Redis connection error.
Package Com.mkfree.redis.test;import Java.util.set;import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.data.redis.connection.jedis.jedisconnectionfactory;import redis.clients.jedis.Jedis;/** * Encapsulating the Redis Cache Server service Interface * @author HK * * 2012-12-16 a.m. 3:09:18 */public class Redisservice {/** * Delete (bytes) by key * @para M key */public void del (byte [] key) {This.getjedis (). del (key); }/** * Delete * @param key */public void del (String key) {This.getjedis (). del (key) via key; }/** * Add key value and set the time to Live (byte) * @param key * @param value * @param livetime */public void Set (Byte [] key,byte [] value,int livetime) {this.set (key, value); This.getjedis (). Expire (key, livetime); }/** * Add key value and set survival time * @param key * @param value * @param livetime */public void Set (St Ring key,string Value,int livetime) {This.set (key, value); This.getjedis (). Expire (key, livetime); }/** * Add key value * @param key * @param value */public void Set (String key,string value) { This.getjedis (). Set (key, value); }/** Add key value (bytes) (serialized) * @param key * @param value */public void set (byte [] key,byte [] value) { This.getjedis (). Set (key, value); }/** * Gets Redis value (string) * @param key * @return */Public String get (string key) {Strin G value = This.getjedis (). get (key); return value; }/** * gets Redis value (Byte []) (deserialization) * @param key * @return */public byte[] Get (byte [] key) { Return This.getjedis (). get (key); }/** * matches keys by regular @param pattern * @return */public set<string> keys (String pattern) { Return This.getjedis (). keys (pattern); }/** * Check if key already exists * @param key * @return */public BOolean exists (String key) {return This.getjedis (). exists (key); /** * Clears all redis data * @return */public String flushdb () {return This.getjedis (). FLUSHDB (); /** * See how much data is in Redis */public long dbsize () {return This.getjedis (). Dbsize (); }/** * Check if the connection is successful * @return */public String ping () {return This.getjedis (). Ping (); }/** * Gets a Jedis client * @return */private Jedis Getjedis () {if (Jedis = null) {return Jedisconnectionfactory.getshardinfo (). Createresource (); } return Jedis; } private Redisservice () {}//operation Redis Client private static Jedis Jedis; @Autowired @Qualifier ("jedisconnectionfactory") private jedisconnectionfactory jedisconnectionfactory;
And no connection pooling is applied. If we want to get Jedis instances from Jedisconnectionfactory and want to use connection pooling. Can do this:
Private Jedis Getjedis () {if (Jedis = = null) { jedisconnection = jedisconnectionfactory.getconnection (); Jedis = Jedisconnection.getnativeconnection (); return Jedis;} return Jedis;}
This way the Jedis instance is managed by the connection pool. But every time we're done, we have to release the connection and put it back in the connection pool. Jedisconnection.close (); This close is not a real shutdown, but instead puts the connection back into the connection pool.
Spring combines redis to get Jedis instances correctly from Jedisconnectionfactory