java110 3 anos atrás
pai
commit
c8b3b13e07

+ 3 - 7
java110-core/src/main/java/com/java110/core/cache/Java110RedisConfig.java

@@ -59,8 +59,9 @@ public class Java110RedisConfig extends CachingConfigurerSupport {
         }
     }
 
-    @Bean(name = "jedisCluster")
-    @Autowired
+
+//    @Bean(name = "jedisCluster")
+//    @Autowired
     public JedisCluster jedisCluster(@Qualifier("jedis.pool.config") JedisPoolConfig config,
                                      @Value("${jedis.pool.host}") String host,
                                      @Value("${jedis.pool.port}") int port,
@@ -70,10 +71,6 @@ public class Java110RedisConfig extends CachingConfigurerSupport {
         if (timeout == 0) {
             timeout = 2000;
         }
-        if (!host.contains(",")) {
-            return null;
-        }
-
         String[] hosts = host.split(",");
         Set<HostAndPort> nodes = new HashSet<>();
         String[] tmpHosts = null;
@@ -83,7 +80,6 @@ public class Java110RedisConfig extends CachingConfigurerSupport {
         }
         JedisCluster cluster = new JedisCluster(nodes);
         return cluster;
-
     }
 
     @Bean(name = "jedis.pool.config")

+ 104 - 19
java110-core/src/main/java/com/java110/core/cache/JedisClientPool.java

@@ -15,109 +15,194 @@ public class JedisClientPool implements Jedis {
     @Override
     public String set(String key, String value) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().set(key, value);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+           return jedis.set(key, value);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public String set(byte[] key, byte[] value) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().set(key, value);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.set(key, value);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public String set(String key, String value, String nxxx, String expx, int time) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().set(key, value,nxxx,expx,time);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.set(key, value,nxxx,expx,time);
+        }finally {
+            jedis.close();
+        }
     }
 
 
     @Override
     public String get(String key) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().get(key);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.get(key);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public byte[] get(byte[] key) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().get(key);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.get(key);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Boolean exists(String key) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().exists(key);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.exists(key);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Long expire(String key, int seconds) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().expire(key, seconds);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.expire(key, seconds);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Long ttl(String key) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().ttl(key);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.ttl(key);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Long incr(String key) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().incr(key);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.incr(key);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Long hset(String key, String field, String value) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().hset(key, field, value);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.hset(key, field, value);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public String hget(String key, String field) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().hget(key, field);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.hget(key, field);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Long hdel(String key, String... field) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().hdel(key, field);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.hdel(key, field);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Long del(String key) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().del(key);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.del(key);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Long del(byte[] key) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().del(key);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.del(key);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public void close() {
-        JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        jedisPool.getResource().close();
+//        JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
+//        jedisPool.getResource().close();
     }
 
     @Override
     public Set<String> keys(String pattern) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().keys(pattern);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.keys(pattern);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Object eval(String script, int keyCount, String... params) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().eval(script,keyCount,params);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.eval(script,keyCount,params);
+        }finally {
+            jedis.close();
+        }
     }
 
     @Override
     public Object eval(String script, List<String> keys, List<String> args) {
         JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
-        return jedisPool.getResource().eval(script,keys,args);
+        redis.clients.jedis.Jedis jedis = jedisPool.getResource();
+        try {
+            return jedis.eval(script,keys,args);
+        }finally {
+            jedis.close();
+        }
     }
 }

+ 2 - 0
java110-utils/src/main/java/com/java110/utils/cache/BaseCache.java

@@ -13,6 +13,8 @@ public class BaseCache {
 
     public final static String JEDIS_DEFAULT_POOL = "jedisClientPool"; // 单节点模式  集群模式 jedisClientCluster
 
+
+
     protected static Jedis getJedis(){
 //        JedisPool jedisPool = (JedisPool) ApplicationContextFactory.getBean("jedisPool");
 //        return jedisPool.getResource();