shuzheng 9 years ago
parent
commit
99eb0a690b
1 changed files with 31 additions and 98 deletions
  1. 31 98
      zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java

+ 31 - 98
zheng-common/src/main/java/com/zheng/common/util/RedisUtil.java

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