Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize distributed Lock by SETNX Operation based on Redis

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to achieve distributed locks in Redis-based SETNX operations". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve distributed locks in Redis-based SETNX operations" can help you solve your doubts.

Import com.jd.jim.cli.Cluster

Import java.io.IOException

Import java.util.concurrent.TimeUnit

/ * *

*

* distributed lock based on SETNX operation of Redis

*

* @ author lzc.java@icloud.com

*

, /

Public class RedisDistributedLock {

Private Cluster redis

/ / name of the lock

Private String lockKey

/ / value of the lock

Private String lockVal = ""

/ / the valid duration of the default lock (milliseconds)

Private long lockExpires

Private boolean locked

/ / the thread that holds the lock in the current jvm (if have one)

Private Thread exclusiveOwnerThread

/ * *

*

* @ param redis

* @ param lockKey lockKey

* @ param lockExpires lockKey expiration time (in milliseconds)

* @ throws IOException

, /

Public RedisDistributedLock (Cluster redis, String lockKey, long lockExpires) {

This.redis = redis

This.lockKey = lockKey

This.lockExpires = lockExpires

}

/ * *

* Block lock is acquired, but there is a timeout. If the lock is not acquired by tryGetLockTime, it will be returned to false directly.

* @ param tryGetLockTime

* @ param tryGetLockUnit

* @ return

* @ throws InterruptedException

, /

Protected boolean lock (long tryGetLockTime, TimeUnit tryGetLockUnit) {

Try {

/ / the time of timeout control can be obtained locally, because it has nothing to do with lock timeout, but only the control of a period of time

Long start = System.currentTimeMillis ()

Long timeout = tryGetLockUnit.toMillis (tryGetLockTime)

/ / int tryTimes=0

While (System.currentTimeMillis ()-start < timeout) {

/ / tryTimes++

/ / Lock timeout

Long lockExpireTime = System.currentTimeMillis () + lockExpires + 1

String stringOfLockExpireTime = String.valueOf (lockExpireTime)

If (setnx (lockKey, stringOfLockExpireTime)) {/ / acquire the lock

/ / successfully acquire the lock and set the relevant identity

Locked = true

ExclusiveOwnerThread = Thread.currentThread ()

/ / System.out.println ("got the lock, :" + tryTimes)

Return true

}

/ / indicates that the lock has not been acquired. Further check whether the lock has timed out.

String lockVal=redis.get (lockKey)

/ / in the case of lockVal=null, the C1 client acquires the lock, and after processing, the DEL drops the lock, before the DEL lock.

/ / C2 sets the timestamp T0 to the lockKey through SETNX and finds that the client has acquired the lock and entered the GET operation.

/ / at this time, the C1 client DEL is unlocked successfully.

/ / C2 sends a GET command to lockKey to get the return value T1 (null).

If (lockValualValidships nullable Long.parseLong (lockVal))

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report