If SETNX returns 1 the client acquired the lock, setting the lock.foo key to the Unix time at which the lock should no longer be considered valid. The client will later use DEL lock.foo in order to release the lock.
If SETNX returns 0 the key is already locked by some other client. We can either return to the caller if it's a non blocking lock, or enter a loop retrying to hold the lock until we succeed or some kind of timeout expires.
redis.io 關于setnx 有這么一段 描述, 其中他說 If SETNX returns 1 the client acquired the lock, setting the lock.foo key to the Unix time at which the lock should no longer be considered valid. 有木有大佬解釋一下這句話到底是什么意思,是說當客戶端獲得了鎖,這個命令的過期時間就不再有效了
uj5u.com熱心網友回復:
If SETNX returns 1 the client acquired the lock, setting the lock.foo key to the Unix time at which the lock should no longer be considered valid. The client will later use DEL lock.foo in order to release the lock.如果SETNX回傳1(代表)客戶端已經獲得該鎖,設定 lock.foo鍵為某個代表之后鎖失效的unix時間。客戶之后可以用 DEL lock.foo釋放該鎖。
意思應該是獲得鎖的客戶端可以設定一個鎖失效時間,在此時間之前鎖一直有效,但是客戶端也可以用 DEL lock.foo提前釋放鎖。
unix時間是1970-01-01 0點開始的秒計數。
uj5u.com熱心網友回復:
釋放鎖有兩種方式,如果使用鎖正確結束,則通過del釋放鎖;還有一種情況是連接斷開了,執行緒掛了,沒有辦法釋放鎖,此時另一執行緒來請求該鎖就會無法獲取,進入死鎖狀態。為了避免上面問題,時間用來說明鎖的有效時間,過了時間,原本擁有執行緒的鎖即使沒有釋放,別的執行緒會釋放并爭搶該鎖。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/31044.html
標籤:數據庫相關
下一篇:攔截第三方程式的按鈕單擊事件
