Update RedisUtil.java

This commit is contained in:
shuzheng 2017-05-18 23:19:24 +08:00 committed by GitHub
parent 7637b9502f
commit 07365ec105
1 changed files with 97 additions and 30 deletions

View File

@ -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();
Jedis jedis = null;
String value = null;
try {
jedis = getJedis();
if (null == jedis) {
return null;
}
String value = jedis.get(key);
value = jedis.get(key);
} catch (Exception e) {
_log.error("Get value error : " + e);
} finally {
if(jedis != null) {
jedis.close();
}
}
return value;
}
@ -187,12 +217,24 @@ public class RedisUtil {
* @return value
*/
public synchronized static byte[] get(byte[] key) {
Jedis jedis = getJedis();
Jedis jedis = null;
byte[] value = null;
try {
jedis = getJedis();
if (null == jedis) {
return null;
}
byte[] value = jedis.get(key);
value = jedis.get(key);
} catch (Exception e) {
_log.error("Get byte value error : " + e);
} finally {
if(jedis != null) {
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();
}
}
}