Private FinalString Redislockkey = "Redlock"; Private Final LongAlttimeout = 1 * * 1000L;//1hour /*** Get Redis Distributed lock * *@authorAdministrator *@version1.0 *@returntrue: Get lock permission false: Lock permission not acquired*/ Private synchronized BooleanGetredlock () {Long flag= Jedis.setnx (Redislockkey, String.valueof (System.currenttimemillis () + alttimeout + 1L)); if(Flag = = 0L) {String value=Jedis.get (Redislockkey); LongLongval =Long.parselong (value); if(Longval > System.currenttimemillis ()) {//Not expired returnBoolean.false; } Else{//ExpiredString _value =Jedis.getset (Redislockkey, String.valueof (System.currenttimemillis ()+ Alttimeout + 1L)); Long_longval =Long.parselong (_value); if(_longval >System.currenttimemillis ()) { returnBoolean.false; } Else { returnboolean.true; } } } Else { returnboolean.true; } }
Redis Setnx Distributed lock