RedisHelper.java package com.isoftstone.pcis.util;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import org.apache.log4j.Logger;import r
RedisHelper.java
package com.isoftstone.pcis.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
/**
*
* @author qingzhu@isoftstone.com Redis Helper 类处理Redis基本操作
*
*/
public class RedisHelper {
static Logger logger = Logger.getLogger(RedisHelper.class);
public static String getStringByKey(final String key) {
return (String) getObjectByKey(key);
}
public static Object getObjectByKey(final String key) {
Jedis jedis = null;
try {
jedis = RedisPool.getJedis();
final String s = DBPREFIX + key;
byte[] ret = jedis.get(s.getBytes());
if (ret != null) {
return SerializeUtils.deserialize(ret);
} else {
return null;
}
} catch (Exception e) {
RedisPool.brokenResource(jedis);
logger.error(e.getMessage());
} finally {
RedisPool.retResource(jedis);
}
return null;
}
public static boolean setObject2Redis(final String key, final String value) {
return setObject2Redis(key, (Object) value);
}
public static boolean setObject2Redis(final String key, final Object value) {
boolean flag = true;
Jedis jedis = null;
try {
jedis = RedisPool.getJedis();
byte[] val = SerializeUtils.serialize(value);
final String s = DBPREFIX + key;
String ret = jedis.set(s.getBytes(), val);
flag = ret.equals(OP_REDIS_SUCCESS) ? true : false;
} catch (Exception e) {
flag = false;
RedisPool.brokenResource(jedis);
logger.error(e.getMessage());
} finally {
RedisPool.retResource(jedis);
}
return flag;
}
/**
*
* @param key
* @param value
* @param nxxx not use
* NX|XX
* NX-- Only set the key if it does not already exist.
* XX-- Only set the key if it already exist.
* @param expx not use
* EX|PX
* EX = seconds; PX = milliseconds
* @param time
* @return
* @throws BusinessServiceException
*/
public static boolean setObject2Redis(final String key, final Object value,
final String nxxx, final String expx, final long time) {
boolean flag = true;
Jedis jedis = null;
try {
jedis = RedisPool.getJedis();
byte[] val = SerializeUtils.serialize(value);
final String s = DBPREFIX + key;
/**
String ret = jedis.set(s.getBytes(), val, nxxx.getBytes(), expx
.getBytes(), time);
**/
String ret = jedis.set(s.getBytes(),val);
jedis.expire(s.getBytes(), (int) time);
flag = ret.equals(OP_REDIS_SUCCESS) ? true : false;
} catch (Exception e) {
e.printStackTrace();
flag = false;
RedisPool.brokenResource(jedis);
logger.error(e.getMessage());
} finally {
RedisPool.retResource(jedis);
}
return flag;
}
/**
* 异步管道方式
* @param hashMap
* @return
*/
public static boolean setObject4Pipelined(final Map
hashMap){
if(hashMap==null) return false;
boolean flag = true;
Jedis jedis = null;
try {
jedis = RedisPool.getJedis();
Pipeline pipeline=jedis.pipelined();
for(Iterator
iter = hashMap.keySet().iterator();iter.hasNext();){ String key = iter.next(); final String s = DBPREFIX + key; pipeline.set(s.getBytes(), SerializeUtils.serialize(hashMap.get(key))); } List