1. 程式人生 > >整理redis的工具類jedis - 二

整理redis的工具類jedis - 二

程式碼如下:

package cn.cslp.ilea.distribute.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Transaction;

public class RedisUtil {

	private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
	

	private static final String SUCCESS_OK = "OK";
	private static final Long SUCCESS_STATUS_LONG = 1L;

	// SET IF NOT EXIST,即當key不存在時,我們進行set操作;若key已經存在,則不做任何操作
	private static final String SET_IF_NOT_EXIST = "NX";
	// 給key加一個過期的設定,具體時間由第五個引數決定
	private static final String SET_WITH_EXPIRE_TIME = "PX";

	// jedis server url
	public static final String JEDIS_HOST_URL = PropertyUtil.getPropertyByName("JEDIS_HOST_URL");
	// jedis server port
	public static final int JEDIS_HOST_PORT = Integer.parseInt(PropertyUtil.getPropertyByName("JEDIS_HOST_PORT"));
	// jedis server paw
	public static final String JEDIS_HOST_PASSWD = PropertyUtil.getPropertyByName("JEDIS_HOST_PASSWD");
	// jedis max active connection
	public static final int JEDIS_MAX_ACTIVE = Integer.parseInt(PropertyUtil.getPropertyByName("JEDIS_MAX_ACTIVE"));
	// jedis max idle comnnection
	public static final int JEDIS_MAX_IDLE = Integer.parseInt(PropertyUtil.getPropertyByName("JEDIS_MAX_IDLE"));
	// jedis pool
	public static JedisPool pool = null;

	public static JedisPool getJedisPool() {
		if (pool == null) {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxIdle(JEDIS_MAX_IDLE);
			config.setTestOnBorrow(true);
			pool = new JedisPool(config, JEDIS_HOST_URL, JEDIS_HOST_PORT, 0, JEDIS_HOST_PASSWD);
		}

		return pool;
	}


	
	
	/**
	 * 獲取有序set
	 * @param key
	 * @return
	 */
	public static Set<String> getAllSortSet(String key) {
		Set<String> ret = null;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			ret = jedis.zrange(key, 0, -1);
		} catch (Exception e) {
			LOGGER.error("redis getAllSortSet 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	/**
	 * 有序set
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public static Set<String> getSortSet(String key,Integer start,Integer end) {
		Set<String> ret = null;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			ret = jedis.zrange(key, start, end);
		} catch (Exception e) {
			LOGGER.error("redis getSortSet 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	
	/**
	 * 有序set set
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public static boolean setSortSet(String key,Integer score,String member) {
		boolean ret = false;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			Long status = jedis.zadd(key, score, member);
			if(SUCCESS_STATUS_LONG == status) {
				ret = true;
			}
		} catch (Exception e) {
			LOGGER.error("redis getSortSet 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	/**
	 * 獲取無序set
	 * @param key
	 * @return
	 */
	public static Set<String> getSet(String key) {
		Set<String> ret = null;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			ret = jedis.smembers(key);
		} catch (Exception e) {
			LOGGER.error("redis getSet 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	
	/**
	 * 獲取List
	 * @param key
	 * @return
	 */
	public static List<String> getAllList(String key) {
		List<String> ret = null;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			ret = jedis.lrange(key, 0, -1);
		} catch (Exception e) {
			LOGGER.error("redis getAllList 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	
	/**
	 * 獲取List
	 * @param key
	 * @return
	 */
	public static List<String> getAllList(String key,Integer start,Integer end) {
		List<String> ret = null;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			ret = jedis.lrange(key, start, end);
		} catch (Exception e) {
			LOGGER.error("redis getAllList 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	
	/**
	 * 新增List
	 * @param key
	 * @return
	 */
	public static boolean addInList(String key,String value) {
		boolean ret = false;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			Long status = jedis.rpush(key, value);
			if(SUCCESS_STATUS_LONG == status) {
				ret = true;
			}
		} catch (Exception e) {
			LOGGER.error("redis addInList 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	/**
	 * 判斷是否在set集合裡存在
	 * @param key
	 * @param member
	 * @return
	 */
	public static boolean sismenber(String key,String member) {
		boolean ret = false;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			ret = jedis.sismember(key, member);
		} catch (Exception e) {
			LOGGER.error("redis sismenber 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	/**
	 * set集合的add
	 * @param key
	 * @param member
	 * @return
	 */
	public static boolean sadd(String key,String member) {
		boolean ret = false;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			Long status = jedis.sadd(key, member);
			if(SUCCESS_STATUS_LONG == status) {
				ret = true;
			}
		} catch (Exception e) {
			LOGGER.error("redis sadd 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	
	/**
	 * set集合的size
	 * @param key
	 * @param member
	 * @return
	 */
	public static Long scard(String key) {
		Long ret = 0L;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			ret = jedis.scard(key);
		} catch (Exception e) {
			LOGGER.error("redis scard 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	/**
	 * set(key1,value1)  &&  sadd(key2,value2)
	 * @param key1
	 * @param value1
	 * @param key2
	 * @param value2
	 * @return
	 */
	public static boolean setAndSaddTransaction(String key1,String value1,String key2,String value2) {
		boolean ret = false;
		Jedis jedis = null;
		try {
			jedis = getJedisPool().getResource();
			if (jedis == null) {
				return ret;
			}
			Transaction tr =jedis.multi();
			tr.set(key1, value1);
			tr.sadd(key2,value2);
			List<Object> list = tr.exec();
			if(list.get(0).equals(SUCCESS_OK) && list.get(1) == SUCCESS_STATUS_LONG) {
				ret = true;
			}else{
				jedis.del(key1);
				jedis.srem(key2, value2);	
			}
		} catch (Exception e) {
			LOGGER.error("redis adapterAndTerminal 出錯", e);
			pool.returnBrokenResource(jedis);
		} finally {
			if (null != jedis) {
				pool.returnResource(jedis);
			}
		}

		return ret;
	}
	
	
	

}