자바 제 디 스 작업 도구 클래스

32364 단어
package com.conb.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

/**
 * @description redis   
 * @author haixia.shi
 * @date 2017 12 1 
 *
 */
@Repository
public class RedisUtils {

	private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);

	@Autowired
	private JedisPool jedisPool;

	/************************************* String ***********************************/

	/**
	 *           (     ,      )
	 * 
	 * @param key
	 * @param value
	 * @return        ok ,    null     
	 */
	public String setString(String key, String value) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.set(key, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *           (     ,      )
	 * 
	 * @param key
	 * @param value
	 * @return 1:  ,0:  
	 */
	public Long setnxString(String key, String value) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.setnx(key, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *                (     ,      )
	 * 
	 * @param keysvalues
	 *            key value 
	 * @return        ok ,    null     
	 */
	public String mset(String... keysvalues) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.mset(keysvalues);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *                (        ,       )
	 * 
	 * @param keysvalues
	 *            keysvalues key value 
	 * @return 1:  ,0:  
	 */
	public Long msetnx(String... keysvalues) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.msetnx(keysvalues);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *               
	 * 
	 * @param key
	 * @param value
	 * @param time
	 *                   : 
	 * @return        ok ,    null     
	 */
	public String setexString(String key, String value, int time) {
		//  redis          
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.setex(key, time, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *           
	 * 
	 * @param key
	 * @return
	 */
	public String getString(String key) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.get(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *   String     
	 * 
	 * @param key
	 * @return     
	 */
	public Long delString(String key) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.del(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *   String  (   )   
	 * 
	 * @param key
	 */
	public Long delSerializeString(String key) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.del(SerializeUtils.serialize(key));
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *   String   key    
	 * 
	 * @param key
	 * @return   :true ,    :false
	 */
	public Boolean existStringKey(String key) {
		Jedis jedis = null;
		Boolean result = false;
		try {
			jedis = jedisPool.getResource();
			result = jedis.exists(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *   String   key value    
	 * 
	 * @param key
	 * @param value
	 * @return   :true ,    :false
	 */
	public Boolean existStringValue(String key, String value) {
		Jedis jedis = null;
		boolean result = true;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sismember(key, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * String  key value   1
	 * 
	 * @param key
	 * @return    value
	 */
	public Long incr(String key) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.incr(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * String  key value   n(n     ,n       )
	 * 
	 * @param key
	 * @param integer
	 * @return    value
	 */
	public Long incrBy(String key, int integer) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.incrBy(key, integer);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * String  key value 1  1
	 * 
	 * @param key
	 * @return    value
	 */
	public long decrString(String key) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.decr(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * String  key value 1  n(n     ,n       )
	 * 
	 * @param key
	 * @param integer
	 * @return    value
	 */
	public Long decrBy(String key, long integer) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.decrBy(key, integer);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *  String  key value     
	 * 
	 * @param key
	 * @param value
	 * @return    value   
	 */
	public Long appendString(String key, String value) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.append(key, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *      
	 * 
	 * @param key
	 * @param start
	 *                
	 * @param end
	 *                
	 * @return          
	 */
	public String substr(String key, int start, int end) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.substr(key, start, end);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * String   key   (   key  ,   )
	 * 
	 * @param oldKey
	 *             key
	 * @param newKey
	 *             key
	 * @return        ok ,    null     
	 */
	public String renameKey(String oldKey, String newKey) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.rename(oldKey, newKey);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * String   key   (   key  ,     )
	 * 
	 * @param oldKey
	 *             key
	 * @param newKey
	 *             key
	 * @return 1:   , 0:  
	 */
	public long renamenxKey(String oldKey, String newKey) {
		Jedis jedis = null;
		long result = 1;
		try {
			jedis = jedisPool.getResource();
			result = jedis.renamenx(oldKey, newKey);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *      value(      value)
	 * 
	 * @param key
	 * @param value
	 * @return    value
	 */
	public String getSetKey(String key, String value) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.getSet(key, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/************************************* Object ***********************************/

	/**
	 *   Object     (    key value)
	 * 
	 * @param key
	 * @param value
	 */
	public void setSerializeObject(Object key, Object value) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 *       (    )Object     
	 * 
	 * @param key
	 * @param time
	 * @param value
	 */
	public void setexSerializeTime(Object key, int time, Object value) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.setex(SerializeUtils.serialize(key), time, SerializeUtils.serialize(value));
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 *   Object  (    )      
	 * 
	 * @param key
	 * @return
	 */
	public Object getSerializeObject(Object key) {
		Jedis jedis = null;
		Object obj = null;
		try {
			jedis = jedisPool.getResource();
			obj = SerializeUtils.unserialize(jedis.get(SerializeUtils.serialize(key)));
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return obj;
	}
	
	/**
	 *   Object     2
	 * @param key
	 * @return
	 */
	public Object getObject(Object key) {
		Jedis jedis = null;
		Object obj = null;
		try {
			jedis = jedisPool.getResource();
			obj = SerializeUtils.unserialize(jedis.get(key.toString().getBytes()));
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		}finally{
			if (jedis != null) {
				jedis.close();
			}
		}
		return obj;
	}

	/**
	 *       Object   key
	 * 
	 * @param key
	 */
	public void deSerializelObject(Object key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.del(SerializeUtils.serialize(key));
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 *   (   )Object   key    
	 * 
	 * @param key
	 * @return   :true ,    :false
	 */
	public Boolean existObjectKey(Object key) {
		Jedis jedis = null;
		Boolean result = false;
		try {
			jedis = jedisPool.getResource();
			result = jedis.exists(SerializeUtils.serialize(key));
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/************************************* Byte ***********************************/

	/**
	 *   byte  (    ) key
	 * 
	 * @param key
	 * @return
	 */
	public void setByte(byte[] key, byte[] value) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 *   byte  (    ) key
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 */
	public void setexByteTime(byte[] key, byte[] value, int seconds) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.setex(key, seconds, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 *   byte  (    ) key
	 * 
	 * @param key
	 * @return
	 */
	public byte[] getByte(byte[] key) {
		Jedis jedis = null;
		byte[] bs = null;
		try {
			jedis = jedisPool.getResource();
			bs = jedis.get(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return bs;
	}

	/**
	 *   byte  (    ) key
	 * 
	 * @param key
	 */
	public void delByte(byte[] key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.del(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/************************************** List **********************************/

	/**
	 *         (  )
	 * 
	 * @param key
	 * @param strings
	 *            list  
	 * @return     
	 */
	public Long rpush(String key, String... strings) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.rpush(key, strings);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *         (  )
	 * 
	 * @param key
	 * @param strings
	 *            list  
	 * @return     
	 */
	public Long lpush(String key, String... strings) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lpush(key, strings);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *       
	 * 
	 * @param key
	 * @return
	 */
	public Long llen(String key) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.llen(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *      key       (0:   ,-1:    )
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public List lrange(String key, long start, long end) {
		List list = new ArrayList();
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			list = jedis.lrange(key, start, end);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return list;
	}

	/**
	 *       ,                 
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return        ok ,    null     
	 */
	public String ltrim(String key, long start, long end) {
		String result = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.ltrim(key, start, end);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *    n     
	 * 
	 * @param key
	 * @param index
	 * @return          "null"
	 */
	public String lindex(String key, long index) {
		String result = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lindex(key, index);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *            
	 * 
	 * @param key
	 * @param index
	 * @param value
	 * @return        ok ,    null     
	 */
	public String lset(String key, long index, String value) {
		String result = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lset(key, index, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *      COUNT   ,         VALUE      (  count      value)
	 * 
	 * @param key
	 * @param count
	 *                (count > 0:       ,count < 0:       )
	 * @param value
	 * @return
	 */
	public Long lrem(String key, long count, String value) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lrem(key, count, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *                 
	 * 
	 * @param key
	 * @return
	 */
	public String lpop(String key) {
		String result = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lpop(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *                  
	 * 
	 * @param key
	 * @return
	 */
	public String rpop(String key) {
		String result = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.rpop(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *                       ,      
	 * 
	 * @param srckey
	 * @param dstkey
	 * @return
	 */
	public String rpoplpush(String srckey, String dstkey) {
		String result = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.rpoplpush(srckey, dstkey);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/************************************ Hash ************************************/

	/**
	 *   key          (field       ,      )
	 * 
	 * @param key
	 *               key
	 * @param field
	 *              
	 * @param value
	 *             
	 * @return 1:  ,0:  ,       Hash      
	 */
	public Long hset(String key, String field, String value) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hset(key, field, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *   key          (field      ,      )
	 * 
	 * @param key
	 *               key
	 * @param field
	 *              
	 * @param value
	 *             
	 * @return 1:  ,0:  ,       Hash      
	 */
	public Long hsetnx(String key, String field, String value) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hsetnx(key, field, value);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *     
	 * 
	 * @param key
	 *               key
	 * @param map
	 * @param return
	 *                   Hash      
	 */
	public String hmset(String key, Map map) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hmset(key, map);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *   key           
	 * 
	 * @param key
	 *               key
	 * @param field
	 *              
	 * @return        Hash      
	 */
	public String hget(String key, String field) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hget(key, field);

		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *   key         
	 * 
	 * @param key
	 *               key
	 * @return        Hash      
	 */
	public Map hgetAll(String key) {
		Jedis jedis = null;
		Map hgetAll = new LinkedHashMap();
		try {
			jedis = jedisPool.getResource();
			hgetAll = jedis.hgetAll(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return hgetAll;
	}

	/**
	 *            n(n     ,n       )
	 * 
	 * @param key
	 *               key
	 * @param field
	 *              
	 * @param value
	 *             
	 * @return      ,       Hash                 
	 */
	public Long hincrBy(String key, String field, long value) {
		Jedis jedis = null;
		long result = 0;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hincrBy(key, field, value);

		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;

	}

	/**
	 *            
	 * 
	 * @param key
	 *               key
	 * @param field
	 *              
	 * @return true   ,false    ,       Hash      
	 */
	public Boolean hexists(String key, String field) {
		boolean result = false;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hexists(key, field);

		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *               (          )
	 * 
	 * @param key
	 *               key
	 * @param fields
	 *              
	 * @return           ,       Hash      
	 */
	public Long hdel(String key, String... fields) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hdel(key, fields);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}

		return result;
	}

	/**
	 *            
	 * 
	 * @param key
	 *               key
	 * @return
	 */
	public Long hlen(String key) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hlen(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}

		return result;
	}

	/**
	 *           
	 * 
	 * @param key
	 *               key
	 * @return
	 */
	public Set hkeys(String key) {
		Set set = new HashSet();
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			set = jedis.hkeys(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return set;
	}

	/**
	 *          
	 * 
	 * @param key
	 *               key
	 * @return
	 */
	public List hvals(String key) {
		List list = new ArrayList();
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			list = jedis.hvals(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return list;
	}

	/************************************ Set ************************************/

	/**
	 *   set  (        ,      )
	 * 
	 * @param key
	 * @param members
	 *              
	 * @return               ,       set      
	 */
	public Long sadd(String key, String... members) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sadd(key, members);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *   set         
	 * 
	 * @param key
	 * @return
	 */
	public Set sgetAll(String key) {
		Jedis jedis = null;
		Set set = new HashSet();
		try {
			jedis = jedisPool.getResource();
			set = jedis.smembers(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return set;
	}

	/**
	 *                (      )
	 * 
	 * @param key
	 * @param members
	 *              
	 * @return        ,       set      
	 */
	public Long srem(String key, String... members) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.srem(key, members);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *                
	 * 
	 * @param key
	 * @return          ,       set      
	 */
	public String spop(String key) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.spop(key);

		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *                 
	 * 
	 * @param srckey
	 *                 
	 * @param dstkey
	 *                 
	 * @param member
	 *              
	 * @return 1:  ,0:  ,       set      
	 */
	public Long smove(String srckey, String dstkey, String member) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.smove(srckey, dstkey, member);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *           
	 * 
	 * @param key
	 * @return        set      
	 */
	public Long scard(String key) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.scard(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *             
	 * 
	 * @param key
	 * @param member
	 *                 
	 * @return  :true ,  :false
	 */
	public Boolean sismember(String key, String member) {
		Jedis jedis = null;
		Boolean result = false;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sismember(key, member);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *              (        ,      )
	 * 
	 * @param keys
	 * @return        set      
	 */
	public Set sinter(String... keys) {
		Set set = new HashSet();
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			set = jedis.sinter(keys);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return set;
	}

	/**
	 *                    (                ,     ,        )
	 * 
	 * @param dstkey
	 *                
	 * @param keys
	 * @return          ,       set      
	 */
	public Long sinterstore(String dstkey, String... keys) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sinterstore(dstkey, keys);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *          
	 * 
	 * @param keys
	 * @return
	 */
	public Set sunion(String... keys) {
		Set set = new HashSet();
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			set = jedis.sunion(keys);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return set;
	}

	/**
	 *                    (                ,     ,        )
	 * 
	 * @param dstkey
	 * @param keys
	 * @return          ,       set      
	 */
	public Long sunionstore(String dstkey, String... keys) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sunionstore(dstkey, keys);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *            
	 * 
	 * @param keys
	 * @return        set      
	 */
	public Set sdiff(String... keys) {
		Set set = new HashSet();
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			set = jedis.sdiff(keys);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return set;
	}

	/**
	 *                     (                ,     ,        )
	 * 
	 * @param dstkey
	 * @param keys
	 * @return          ,       set      
	 */
	public Long sdiffstore(String dstkey, String... keys) {
		long result = 0;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sdiffstore(dstkey, keys);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *             (       null)
	 * 
	 * @param key
	 * @return        set      
	 */
	public String srandmember(String key) {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.srandmember(key);

		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/*************************************    ***********************************/

	/**
	 *          key
	 * 
	 * @param key
	 */
	public Set getLikeKeys(byte[] key) {
		Jedis jedis = null;
		Set list = new HashSet();
		try {
			jedis = jedisPool.getResource();
			list = jedis.keys(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return list;
	}

	/**
	 *   key   
	 * 
	 * @param key
	 * @return
	 */
	public String typeOfString(String key) {
		Jedis jedis = null;
		String type = "";
		try {
			jedis = jedisPool.getResource();
			type = jedis.type(key);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return type;
	}

	/**
	 *      key
	 * 
	 * @return ok:   , err:  
	 */
	public String flushAllKey() {
		Jedis jedis = null;
		String result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.flushAll();
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *             
	 * 
	 * @param keys
	 * @return
	 */
	public List mgetKeys(String... keys) {
		Jedis jedis = null;
		List result = new ArrayList();
		try {
			jedis = jedisPool.getResource();
			result = jedis.mget(keys);
		} catch (Exception e) {
			logger.info("redis   :" + e.getMessage());
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

}

좋은 웹페이지 즐겨찾기