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;
}
}
}
}
Redis通用类
最新推荐文章于 2020-06-11 09:24:20 发布