From fe3b0f4a3c46892f08421dd4c4d0937fb8a87f93 Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: 星期二, 06 六月 2023 17:55:10 +0800
Subject: [PATCH] 优化代码

---
 java110-core/src/main/java/com/java110/core/cache/JedisClientPool.java |  123 ++++++++++++++++++++++++++++++++++------
 1 files changed, 104 insertions(+), 19 deletions(-)

diff --git a/java110-core/src/main/java/com/java110/core/cache/JedisClientPool.java b/java110-core/src/main/java/com/java110/core/cache/JedisClientPool.java
index 9f5698c..712d604 100644
--- a/java110-core/src/main/java/com/java110/core/cache/JedisClientPool.java
+++ b/java110-core/src/main/java/com/java110/core/cache/JedisClientPool.java
@@ -15,109 +15,194 @@
     @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();
+        }
     }
 }

--
Gitblit v1.8.0