Redis Locking and unlocking

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.