Spring and redis correctly obtain the Jedis instance from jedisConnectionFactory, redisjedis
This example of spring combined with redis is circulating on the Internet to obtain a Jedis instance from jedisConnectionFactory. Others ignore the RedisService only, so there are many problems in obtaining the jedis instance.
In this way, the connection is not closed every time a new connection is created. When there are a lot of operations, redis connections will soon be fully occupied. Redis connection error is reported.
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;/*** encapsulate the redis Cache Server Service Interface * @ author hk ** 3:09:18 a.m. on February 16, */public class RedisService {/*** Delete (byte) through key) * @ param key */public void del (byte [] key) {this. getJedis (). del (key);}/*** Delete @ param key */public void del (String key) {this. getJedis (). del (key);}/*** add key value and set the survival time (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 the survival time * @ param key * @ param value * @ param liveTime */public void set (String 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 (byte) (serialization) * @ param key * @ param value */public void set (byte [] key, byte [] value) {this. getJedis (). set (key, value);}/*** get redis value (String) * @ param key * @ return */public String get (String key) {String value = this. getJedis (). get (key); return value;}/*** get redis value (byte []) (deserialization) * @ param key * @ return */public byte [] get (byte [] key) {return this. getJedis (). get (key);}/*** match keys through regular expressions * @ param pattern * @ return */public Set <String> keys (String pattern) {return this. getJedis (). keys (pattern);}/*** check whether the key already exists * @ param key * @ return */public boolean exists (String key) {return this. getJedis (). exists (key);}/*** clear all redis data * @ return */public String flushDB () {return this. getJedis (). flushDB ();}/*** check the data in redis */public long dbSize () {return this. getJedis (). dbSize ();}/*** check whether the connection is successful * @ return */public String ping () {return this. getJedis (). ping ();}/*** get a jedis client * @ return */private Jedis getJedis () {if (jedis = null) {return jedisConnectionFactory. getShardInfo (). createResource () ;}return jedis;} private RedisService () {}// operate the redis Client private static Jedis jedis; @ Autowired @ Qualifier ("jedisConnectionFactory") private JedisConnectionFactory jedisConnectionFactory ;}
There is no application connection pool. If we want to obtain the Jedis instance from jedisConnectionFactory, we want to use the connection pool. You can do this:
private Jedis getJedis() {if (jedis == null) { jedisConnection = jedisConnectionFactory.getConnection(); jedis = jedisConnection.getNativeConnection();return jedis;}return jedis;}
In this way, the Jedis instance is managed by the connection pool. However, after each operation, the connection must be released and put back into the connection pool. JedisConnection. close (); this close is not actually closed, but the connection is put back into the connection pool.