Redis Distributed Lock

Source: Internet
Author: User
1 package COM. ZAD. jedis; 2 3 Import redis. clients. jedis. jedis; 4 5 import Java. util. collections; 6 7/** 8 * Description: 9 * Distributed Lock 10*11 * @ author zad12 * @ create 2018-09-14 13: 5813 */14 public class distributed {15 Private Static final string lock_success = "OK"; 16 Private Static final string set_if_not_exist = "NX "; 17 Private Static final string set_with_expire_time = "PX"; 18 Private Static final integer release_success = 1; 19 Private Static final integer default_time = 5; 20 21/** 22 * try to obtain the Distributed Lock 23*24 * @ Param Jedis redis client 25 * @ Param lockkey lock 26 * @ Param requestid Request ID 27 * @ return whether the query is successful 28 */29 public static Boolean trygetdistributedlock (Jedis, string lockkey, string requestid) {30 31 string result = Jedis. set (lockkey, requestid, set_if_not_exist, set_with_expire_time, default_time); 32 33 If (lock_success.equals (result) {34 return true; 35} 36 return false; 37 38} 39 40 41/** 42 * try to obtain the Distributed Lock 43*44 * @ Param Jedis redis client 45 * @ Param lockkey lock 46 * @ Param requestid Request ID 47 *@ param expiretime expiration time 48 * @ return whether the query is successful 49 */50 public static Boolean trygetdistributedlock (Jedis, string lockkey, string requestid, int expiretime) {51 52 string result = Jedis. set (lockkey, requestid, set_if_not_exist, set_with_expire_time, expiretime); 53 54 if (lock_success.equals (result) {55 return true; 56} 57 Return false; 58 59} 60 61/** 62 * release Distributed Lock 63*64 * @ Param Jedis redis client 65 * @ Param lockkey 66 * @ Param requestid Request ID 67 * @ return whether 68 */69 public static Boolean releasedistributedlock (Jedis, string lockkey, string requestid) {70 71 string script = "If redis. call ('get', keys [1]) = argv [1] Then return redis. call ('del ', keys [1]) else return 0 end "; 72 object result = Jedis. eval (script, collections. singletonlist (lockkey), collections. singletonlist (requestid); 73 74 if (release_success.equals (result) {75 return true; 76} 77 return false; 78 79} 80}

 

Redis Distributed Lock

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.