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;}}}}