Redis plus Lock
Customerm = Basememcached.setmlock (customerId);
/** * Personal Account table plus lock **/public static customerm Setmlock (Integer userId) {customerm customerm = (customerm) setlock (User_m_userid +userid, Customerm.class); Customerm.setaddpiggy (0); Customerm.setaddpayoff (0); Customerm.setaddamountbase (0); CUSTOMERM.SETADDAMOUNTCFT (0); return customerm;}
public static Object Setlock (String key,class classes) { logger.info ("plus lock" +key+_lock); Long result = Jedispoolclient.getinstance (). SETNX (Key+_lock,system.currenttimemillis ()); try { long time = System.currenttimemillis (); while (result = 1) { thread.sleep; time = System.currenttimemillis (); result = Jedispoolclient.getins Tance (). SETNX (Key+_lock,time); } Logger.info ("Add lock \ t" +key+_lock+ "\ t complete"); Jedispoolclient.getinstance (). Set (key+_lock,time,time_out); Return Jedispoolclient.getinstance (). get (key,classes); } catch (Exception e) { e.printstacktrace (); } return null;}
Public long setnx (String key,object obj) { Jedis Jedis = Jedispool.getresource (); try { long L = jedis.setnx (Key,stringutil.bean2json (obj)); return l; } catch (Jedisconnectionexception e) { jedispool.returnbrokenresource (Jedis); Jedis=null; Logger.error ("Redis exception:" +e.getmessage ()); E.printstacktrace (); } finally{ Jedispool.returnresource (Jedis); } return-1; }
public boolean set (String key,object obj,int seconds) { Jedis Jedis = Jedispool.getresource (); try { Jedis.setex (Key,seconds,stringutil.bean2json (obj)); return true; } catch (Jedisconnectionexception e) { jedispool.returnbrokenresource (Jedis); Jedis=null; Logger.error ("Redis exception:" + E.getmessage ()); E.printstacktrace (); } catch (Exception e) { logger.error ("JSON encapsulation failed:" + e.getstacktrace ()); } finally{ Jedispool.returnresource (Jedis); } return false; }
Redis unlock
Basememcached.updateandunlock (Customerm);
/** * Personal account modified and unlocked * */public static void Updateandunlock (Customerm customerm) { logger.info ("Unlock" +user_ M_userid+customerm.getcustomerid () + _lock); Setmem (User_m_userid+customerm.getcustomerid (), customerm); Delete (User_m_userid+customerm.getcustomerid () +_lock); }
Protected static Boolean Setmem (String key,object obj) { return jedispoolclient.getinstance (). Set (Key,obj); }
protected static void Delete (String key) { jedispoolclient.getinstance (). Delete (key); }
public Boolean set (String key,object obj) { Jedis Jedis = Jedispool.getresource (); try { logger.info (key+ ":" +stringutil.bean2json (obj)); Jedis.set (Key, Stringutil.bean2json (obj)); return true; } catch (Jedisconnectionexception e) { jedispool.returnbrokenresource (Jedis); Jedis=null; Logger.error ("Redis exception:" + E.getmessage ()); E.printstacktrace (); } finally{ Jedispool.returnresource (Jedis); } return false; }
public void Delete (String key) { Jedis Jedis = Jedispool.getresource (); try { Jedis.del (key); } catch (Jedisconnectionexception e) { jedispool.returnbrokenresource (Jedis); Jedis=null; Logger.error ("Redis exception:" +e.getmessage ()); E.printstacktrace (); } finally{ Jedispool.returnresource (Jedis); } }
Redis Locking and unlocking