package com.feng;
import com.common.utils.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class JedisUtil {
@Autowired
protected static JedisPool jedisPool;
private JedisUtil() {}
/**
* 简单的Get
* @param
* @param key
* @param requiredType
* @return
*/
public static T get(String key , Class...requiredType){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] skey = SerializeUtil.serialize(key);
return SerializeUtil.deserialize(jds.get(skey),requiredType);
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return null;
}
/**
* 简单的set
* @param key
* @param value
*/
public static void set(Object key ,Object value){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] skey = SerializeUtil.serialize(key);
byte[] svalue = SerializeUtil.serialize(value);
jds.set(skey, svalue);
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
}
/**
* 过期时间的
* @param key
* @param value
* @param timer (秒)
*/
public static void setex(Object key, Object value, int timer) {
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] skey = SerializeUtil.serialize(key);
byte[] svalue = SerializeUtil.serialize(value);
jds.setex(skey, timer, svalue);
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
}
/**
*
* @param
* @param mapkey map
* @param key map里的key
* @param requiredType value的泛型类型
* @return
*/
public static T getVByMap(String mapkey,String key , Class requiredType){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] mkey = SerializeUtil.serialize(mapkey);
byte[] skey = SerializeUtil.serialize(key);
List result = jds.hmget(mkey, skey);
if(null != result && result.size() > 0 ){
byte[] x = result.get(0);
T resultObj = SerializeUtil.deserialize(x, requiredType);
return resultObj;
}
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return null;
}
/**
*
* @param mapkey map
* @param key map里的key
* @param value map里的value
*/
public static void setVByMap(String mapkey,String key ,Object value){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] mkey = SerializeUtil.serialize(mapkey);
byte[] skey = SerializeUtil.serialize(key);
byte[] svalue = SerializeUtil.serialize(value);
jds.hset(mkey, skey,svalue);
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
}
/**
* 删除Map里的值
* @param mapKey
* @param dkey
* @return
*/
public static Object delByMapKey(String mapKey ,String...dkey){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[][] dx = new byte[dkey.length][];
for (int i = 0; i < dkey.length; i++) {
dx[i] = SerializeUtil.serialize(dkey[i]);
}
byte[] mkey = SerializeUtil.serialize(mapKey);
Long result = jds.hdel(mkey, dx);
return result;
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return new Long(0);
}
/**
* 往redis里取set整个集合
*
* @param
* @param setKey
* @param requiredType
* @return
*/
public static Set getVByList(String setKey,Class requiredType){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] lkey = SerializeUtil.serialize(setKey);
Set set = new TreeSet();
Set xx = jds.smembers(lkey);
for (byte[] bs : xx) {
T t = SerializeUtil.deserialize(bs, requiredType);
set.add(t);
}
return set;
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return null;
}
/**
* 获取Set长度
* @param setKey
* @return
*/
public static Long getLenBySet(String setKey){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
Long result = jds.scard(setKey);
return result;
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return null;
}
/**
* 删除Set
* @param dkey
* @return
*/
public static Long delSetByKey(String key,String...dkey){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
Long result = 0L;
if(null == dkey){
result = jds.srem(key);
}else{
result = jds.del(key);
}
return result;
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return new Long(0);
}
/**
* 随机 Set 中的一个值
* @param key
* @return
*/
public static String srandmember(String key){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
String result = jds.srandmember(key);
return result;
} catch (Exception e){
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return null;
}
/**
* 往redis里存Set
* @param setKey
* @param value
*/
public static void setVBySet(String setKey,String value){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
jds.sadd(setKey, value);
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
}
/**
* 取set
* @param key
* @return
*/
public static Set getSetByKey(String key){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
Set result = jds.smembers(key);
return result;
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return null;
}
/**
* 往redis里存List
* @param listKey
* @param value
*/
public static void setVByList(String listKey,Object value){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] lkey = SerializeUtil.serialize(listKey);
byte[] svalue = SerializeUtil.serialize(value);
jds.rpush(lkey, svalue);
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
}
/**
* 往redis里取list
*
* @param
* @param listKey
* @param start
* @param end
* @param requiredType
* @return
*/
public static List getVByList(String listKey,int start,int end,Class requiredType){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] lkey = SerializeUtil.serialize(listKey);
List list = new ArrayList();
List xx = jds.lrange(lkey,start,end);
for (byte[] bs : xx) {
T t = SerializeUtil.deserialize(bs, requiredType);
list.add(t);
}
return list;
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return null;
}
/**
* 获取list长度
* @param listKey
* @return
*/
public static Long getLenByList(String listKey){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] lkey = SerializeUtil.serialize(listKey);
Long result = jds.llen(lkey);
return result;
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return null;
}
/**
* 删除
* @param dkey
* @return
*/
public static Long delByKey(String...dkey){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[][] dx = new byte[dkey.length][];
for (int i = 0; i < dkey.length; i++) {
dx[i] = SerializeUtil.serialize(dkey[i]);
}
Long result = jds.del(dx);
return result;
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return new Long(0);
}
/**
* 判断是否存在
* @param existskey
* @return
*/
public static boolean exists(String existskey){
Jedis jds = null;
boolean isBroken = false;
try {
jds = jedisPool.getResource();
jds.select(0);
byte[] lkey = SerializeUtil.serialize(existskey);
return jds.exists(lkey);
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
returnResource(jds, isBroken);
}
return false;
}
/**
* 释放
* @param jedis
* @param isBroken
*/
public static void returnResource(Jedis jedis, boolean isBroken) {
if (jedis == null)
return;
jedis.close();
}
}
package com.common.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import net.sf.json.JSONObject;
public class SerializeUtil {
static final Class> CLAZZ = SerializeUtil.class;
public static byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] rv = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(value);
os.close();
bos.close();
rv = bos.toByteArray();
} catch (Exception e) {
LoggerUtils.fmtError(CLAZZ,e, "serialize error %s", JSONObject.fromObject(value));
} finally {
close(os);
close(bos);
}
return rv;
}
public static Object deserialize(byte[] in) {
return deserialize(in, Object.class);
}
public static T deserialize(byte[] in, Class...requiredType) {
Object rv = null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
rv = is.readObject();
}
} catch (Exception e) {
LoggerUtils.fmtError(CLAZZ,e, "serialize error %s", in);
} finally {
close(is);
close(bis);
}
return (T) rv;
}
private static void close(Closeable closeable) {
if (closeable != null)
try {
closeable.close();
} catch (IOException e) {
LoggerUtils.fmtError(CLAZZ, "close stream error");
}
}
}