Redis通用类

package redis.clients;

import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;

/**
 * @author WQL 2012-8-8
 */
public abstract class RedisClinet_Base {
	protected Jedis jedis = null;
	static {
	}
	protected RedisClinet_Base() {
		jedis = creatJedis();
	}

	protected abstract Jedis creatJedis();

	/** 添加 Object */
	public boolean set(String key, Object object) {
		synchronized (jedis) {
			try {
				return jedis.set(key.getBytes(), SerializeUtil.serialize(object)).equals("OK");
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return false;
			}
		}

	}

	/** 获取 Object */
	public Object get(String key) {
		synchronized (jedis) {
			try {
				byte[] value = jedis.get(key.getBytes());
				return SerializeUtil.unserialize(value);
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return null;
			}
		}
	}

	/** 删除一个Key **/
	public boolean del(String key) {
		synchronized (jedis) {
			try {
				return jedis.del(key.getBytes()) > 0;
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return false;
			}
		}
	}

	/** 某个键值是否存在 **/
	public boolean exists(String key) {
		synchronized (jedis) {
			try {
				return jedis.exists(key.getBytes());
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return false;
			}
		}
	}

	/** 设置过期时间,单位:秒 **/
	public long expire(String key, int seconds) {
		synchronized (jedis) {
			try {
				return jedis.expire(key.getBytes(), seconds);
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return 0L;
			}
		}
	}

	// /针对字符串

	/** 针对字符串 添加 */
	public boolean set_String(String key, String string) {
		synchronized (jedis) {
			try {
				return jedis.set(key, string).equals("OK");
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return false;
			}
		}

	}

	/** 针对字符串 获取 */
	public String get_String(String key) {
		synchronized (jedis) {
			try {
				return jedis.get(key);
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return null;
			}
		}
	}

	/** 针对字符串 删除一个Key **/
	public boolean del_String(String key) {
		synchronized (jedis) {
			try {
				return jedis.del(key) > 0;
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return false;
			}
		}
	}

	/** 针对字符串 某个键值是否存在 **/
	public boolean exists_String(String key) {
		synchronized (jedis) {
			try {
				return jedis.exists(key);
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return false;
			}
		}
	}

	/** 设置字符串类型的值过期时间,单位:秒 **/
	public long expire_String(String key, int seconds) {
		synchronized (jedis) {
			try {
				return jedis.expire(key, seconds);
			} catch (Exception ex) {
				ex.printStackTrace();
				reConnction();
				return 0L;
			}
		}
	}

	/** 若jedis有异常,重新连接 **/
	private void reConnction() {
		synchronized (jedis) {
			try {
				jedis.disconnect();
				creatJedis();
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(" --重新连接共享内存异常---");
				e.printStackTrace();
			}
		}
	}

	/** 获取 Jedis */
	public Jedis get_jedis() {
		return jedis;
	}
	//=================Set操作================================================
	/**往Set添加一个字符串值*/
	public boolean sadd_String(String key,String value)
	{
		if(value==null||value.equals(""))return false;
		synchronized (jedis) {
			try {
				return jedis.sadd(key, value) > 0;
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}
	public boolean sadd(byte[] key,byte[] value)
	{
		if(value==null||value.equals(""))return false;
		synchronized (jedis) {
			try {
				return jedis.sadd(key, value)>0;
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}

	/** 从Set中删除一个字符串值 **/
	public boolean srem_String(String key, String value) {
		if (value == null || value.equals(""))
			return false;
		synchronized (jedis) {
			try {
				return jedis.srem(key, value) > 0;
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}
	
	public boolean srem(byte[] key, byte[] value) {
		if (value == null || value.equals(""))
			return false;
		synchronized (jedis) {
			try {
				return jedis.srem(key, value) > 0;
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}

	/** 获取Set所有值 **/
	public Set<String> smembers(String key) {
		synchronized (jedis) {
			try {
				return jedis.smembers(key);
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return null;
			}
		}
	}
	public Set<byte[]> smembers(byte[] key) {
		synchronized (jedis) {
			try {
				return jedis.smembers(key);
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return null;
			}
		}
	}
	//=================Hash操作================================================
	/**往Hashi添加一个值*/
	public boolean hset(String hashKey,String filed,String value){
		synchronized (jedis) {
			try {
				return jedis.hset(hashKey, filed, value) > 0;
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}
	public boolean hset(byte[] hashKey,byte[] filed,Object value){
		synchronized (jedis) {
			try {
				return jedis.hset(hashKey, filed, SerializeUtil.serialize(value)) > 0;
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}

	/** 获取某一值 */
	public String hget(String hashKey, String filed) {
		synchronized (jedis) {
			try {
				return jedis.hget(hashKey, filed);
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return null;
			}
		}
	}
	public Object hget(byte[] hashKey, byte[] filed) {
		synchronized (jedis) {
			try {
				return jedis.hget(hashKey, filed);
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return null;
			}
		}
	}

	/** 某获某个Key的所有filed */
	public Set<String> hkeys(String hashKey) {
		synchronized (jedis) {
			try {
				return jedis.hkeys(hashKey);
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return null;
			}
		}
	}
	
	public Set<byte[]> hkeys(byte[] hashKey) {
		synchronized (jedis) {
			try {
				return jedis.hkeys(hashKey);
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return null;
			}
		}
	}
	

	/** 某个Key的某个filed是否存在 **/
	public boolean hexists(String hashKey, String filed) {
		synchronized (jedis) {
			try {
				return jedis.hexists(hashKey, filed);
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}
	public boolean hexists(byte[] hashKey, byte[] filed) {
		synchronized (jedis) {
			try {
				return jedis.hexists(hashKey, filed);
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}

	/** 删除某个Key的某个filed **/
	public boolean hdel(String hashKey, String filed) {
		synchronized (jedis) {
			try {
				return jedis.hdel(hashKey, filed) > 0;
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}
	public boolean hdel(byte[] hashKey, byte[] filed) {
		synchronized (jedis) {
			try {
				return jedis.hdel(hashKey, filed) > 0;
			} catch (Exception ex) {
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}
	
	/**返回Hash某个Key所有值**/
	public Map<String,String> hgetAll(String hashKey){
		synchronized (jedis) {
			try{
				return jedis.hgetAll(hashKey);
			}catch(Exception ex){
				reConnction();
				ex.printStackTrace();
				return null;
			}
		}
	}
	public Map<byte[],byte[]> hgetAll(byte[] hashKey){
		synchronized (jedis) {
			try{
				return jedis.hgetAll(hashKey);
			}catch(Exception ex){
				reConnction();
				ex.printStackTrace();
				return null;
			}
		}
	}
	
	public boolean delKey(String key){
		synchronized (jedis) {
			try{
				return jedis.del(key)>0;
			}catch(Exception ex){
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}
	
	public boolean delKey(byte[] key){
		synchronized (jedis) {
			try{
				return jedis.del(key)>0;
			}catch(Exception ex){
				reConnction();
				ex.printStackTrace();
				return false;
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值