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 = NULL; static {} protected redisclinet_base () {Jedis = creatjedis ();} protected abstract Jedis creatjedis ();/** add object */Public Boolean set (string key, object) {synchronized (Jedis) {try {return Jedis. set (key. getbytes (), serializeutil. serialize (object )). equals ("OK");} catch (exception ex) {ex. printstacktrace (); reconnction (); Return false ;}}/ ** get 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 ;}}/** delete a key **/Public Boolean del (string key) {synchronized (Jedis) {try {return Jedis. del (key. getbytes ()> 0;} catch (exception ex) {ex. printstacktrace (); reconnction (); Return false ;}}/** whether a key value exists **/Public Boolean exists (string key) {synchronized (Jedis) {try {return Jedis. exists (key. getbytes ();} catch (exception ex) {ex. printstacktrace (); reconnction (); Return false ;}}/ ** set the expiration time. Unit: seconds **/public long expire (string key, int seconds) {synchronized (Jedis) {try {return Jedis. expire (key. getbytes (), seconds);} catch (exception ex) {ex. printstacktrace (); reconnction (); Return 0l ;}}} /// // for strings /////////////////////////// ///// // ** add */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 ;}}/ ** get */Public String get_string (string key) {synchronized (Jedis) {try {return Jedis. get (key);} catch (exception ex) {ex. printstacktrace (); reconnction (); return NULL ;}}/ ** delete a key for a string **/Public Boolean del_string (string key) {synchronized (Jedis) {try {return Jedis. del (key)> 0;} catch (exception ex) {ex. printstacktrace (); reconnction (); Return false ;}}/ ** indicates whether a key value of a string exists **/Public Boolean exists_string (string key) {synchronized (Jedis) {try {return Jedis. exists (key);} catch (exception ex) {ex. printstacktrace (); reconnction (); Return false ;}}/** set the expiration time of a string value, in seconds **/public long expire_string (string key, int seconds) {synchronized (Jedis) {try {return Jedis. expire (Key, seconds);} catch (exception ex) {ex. printstacktrace (); reconnction (); Return 0l ;}}/** if Jedis has an exception, reconnect **/private void reconnction () {synchronized (Jedis) {try {Jedis. disconnect (); creatjedis ();} catch (exception e) {e. printstacktrace (); system. out. println ("-- An error occurred while reconnecting to the shared memory ---"); E. printstacktrace () ;}}/ ** get Jedis */Public Jedis get_jedis () {return Jedis ;} // ========================== set operation ============================== =====================================/** Add a string value to the 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 ;}}/ ** delete a string value from 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 ;}}/ ** get all set values **/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 operation ========================== ===================================/** Add a value to 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 ;}}/ ** get a value */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 ;}}/ ** all filed */Public set of a key <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 ;}}/** whether a filed of a key exists **/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 ;}}/ ** delete a filed of a key **/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 ;}}/ ** return all values of a 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 ;}}}}