Selaa lähdekoodia

Update RedisUtil.java

shuzheng 9 vuotta sitten
vanhempi
sitoutus
07365ec105
1 muutettua tiedostoa jossa 97 lisäystä ja 30 poistoa
  1. 97 30
      zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java

+ 97 - 30
zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java

@@ -107,13 +107,18 @@ public class RedisUtil {
 	 * @param value
 	 */
 	public synchronized static void set(String key, String value) {
+		Jedis jedis = null;
 		try {
 			value = StringUtils.isBlank(value) ? "" : value;
-			Jedis jedis = getJedis();
+			jedis = getJedis();
 			jedis.set(key, value);
-			jedis.close();
+			
 		} catch (Exception e) {
 			_log.error("Set key error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}
 
@@ -123,12 +128,17 @@ public class RedisUtil {
 	 * @param value
 	 */
 	public synchronized static void set(byte[] key, byte[] value) {
+		Jedis jedis = null;
 		try {
-			Jedis jedis = getJedis();
+			jedis = getJedis();
 			jedis.set(key, value);
-			jedis.close();
+			
 		} catch (Exception e) {
 			_log.error("Set key error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}
 
@@ -139,13 +149,18 @@ public class RedisUtil {
 	 * @param seconds 以秒为单位
 	 */
 	public synchronized static void set(String key, String value, int seconds) {
+		Jedis jedis = null;
 		try {
 			value = StringUtils.isBlank(value) ? "" : value;
-			Jedis jedis = getJedis();
+			jedis = getJedis();
 			jedis.setex(key, seconds, value);
-			jedis.close();
+			
 		} catch (Exception e) {
 			_log.error("Set keyex error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}
 
@@ -156,13 +171,18 @@ public class RedisUtil {
 	 * @param seconds 以秒为单位
 	 */
 	public synchronized static void set(byte[] key, byte[] value, int seconds) {
+		Jedis jedis = null;
 		try {
-			Jedis jedis = getJedis();
+			jedis = getJedis();
 			jedis.set(key, value);
 			jedis.expire(key, seconds);
-			jedis.close();
+			
 		} catch (Exception e) {
 			_log.error("Set key error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}
 
@@ -172,12 +192,22 @@ public class RedisUtil {
 	 * @return value
 	 */
 	public synchronized static String get(String key) {
-		Jedis jedis = getJedis();
-		if (null == jedis) {
-			return null;
+		Jedis jedis = null;
+		String value = null;
+		try {
+			jedis = getJedis();
+			if (null == jedis) {
+				return null;
+			}
+			value = jedis.get(key);
+			
+		} catch (Exception e) {
+			_log.error("Get value error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
-		String value = jedis.get(key);
-		jedis.close();
 		return value;
 	}
 
@@ -187,12 +217,24 @@ public class RedisUtil {
 	 * @return value
 	 */
 	public synchronized static byte[] get(byte[] key) {
-		Jedis jedis = getJedis();
-		if (null == jedis) {
-			return null;
+		Jedis jedis = null;
+		byte[] value = null;
+		
+		try {
+			jedis = getJedis();
+			if (null == jedis) {
+				return null;
+			}
+			value = jedis.get(key);
+			
+		} catch (Exception e) {
+			_log.error("Get byte value error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
-		byte[] value = jedis.get(key);
-		jedis.close();
+		
 		return value;
 	}
 
@@ -201,12 +243,17 @@ public class RedisUtil {
 	 * @param key
 	 */
 	public synchronized static void remove(String key) {
+		Jedis jedis = null;
 		try {
-			Jedis jedis = getJedis();
+			jedis = getJedis();
 			jedis.del(key);
-			jedis.close();
+			
 		} catch (Exception e) {
-			_log.error("Remove keyex error : " + e);
+			_log.error("Remove string key error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}
 
@@ -215,12 +262,17 @@ public class RedisUtil {
 	 * @param key
 	 */
 	public synchronized static void remove(byte[] key) {
+		Jedis jedis = null;
 		try {
-			Jedis jedis = getJedis();
+			jedis = getJedis();
 			jedis.del(key);
-			jedis.close();
+			
 		} catch (Exception e) {
-			_log.error("Remove keyex error : " + e);
+			_log.error("Remove byte[] key error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}
 
@@ -230,12 +282,17 @@ public class RedisUtil {
 	 * @param key
 	 */
 	public synchronized static void lpush(String key, String... strings) {
+		Jedis jedis = null;
 		try {
-			Jedis jedis = RedisUtil.getJedis();
+			jedis = RedisUtil.getJedis();
 			jedis.lpush(key, strings);
-			jedis.close();
+			
 		} catch (Exception e) {
 			_log.error("lpush error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}
 
@@ -246,12 +303,17 @@ public class RedisUtil {
 	 * @param value
 	 */
 	public synchronized static void lrem(String key, long count, String value) {
+		Jedis jedis = null;
 		try {
-			Jedis jedis = RedisUtil.getJedis();
+			jedis = RedisUtil.getJedis();
 			jedis.lrem(key, count, value);
-			jedis.close();
+			
 		} catch (Exception e) {
-			_log.error("lpush error : " + e);
+			_log.error("lrem error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}
 
@@ -262,13 +324,18 @@ public class RedisUtil {
 	 * @param seconds
 	 */
 	public synchronized static void sadd(String key, String value, int seconds) {
+		Jedis jedis = null;
 		try {
-			Jedis jedis = RedisUtil.getJedis();
+			jedis = RedisUtil.getJedis();
 			jedis.sadd(key, value);
 			jedis.expire(key, seconds);
 			jedis.close();
 		} catch (Exception e) {
 			_log.error("sadd error : " + e);
+		} finally {
+			if(jedis != null) {
+				jedis.close();
+			}
 		}
 	}