亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用SETNX的單實例Redis鎖

使用SETNX的單實例Redis鎖

Go
慕尼黑5688855 2023-06-26 15:35:22
我需要從應用程序客戶端連接到單個 Redis 實例。由于客戶端將在 Kubernetes 中進行復制,因此我正在研究有關鎖的 Redis 文檔,以防止客戶端副本之間的競爭。有趣的是,SETNX文檔明確建議不要使用SETNX鎖來實現,并指出它基本上已經過時了:為了支持 Redlock 算法,不鼓勵使用以下模式 [...]無論如何,我們都會記錄舊模式,因為某些現有實現鏈接到此頁面作為參考。然而,Redlock 算法是專門為分布式鎖量身定制的,因此當一個人試圖鎖定多個 Redis 實例時,它們實際上是指多個主實例。更進一步,庫redsync?(golang) 聲明該New函數如下:func New(pools []Pool) *Redsync {? ? return &Redsync{? ? ? ? pools: pools,? ? }}它看起來毫無疑問是為了支持 Redis 集群上的鎖定而設計的。在我的用例中,我將僅連接到一個 Redis 實例。也許我可以只使用 redsync 包并傳遞長度為 1 的切片,但對我來說,該SETNX模式似乎可以在單個 Redis 實例上同樣正常工作。我的看法正確嗎?謝謝
查看完整描述

2 回答

?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

是的,Redlock 算法確實是為分布式 Redis 系統設計的,如果您使用單個實例,那么使用 SET 和 SETNX 文檔中描述的更簡單的鎖定方法應該沒問題。

然而,更重要的一點是:您可能不需要使用鎖來避免多個 Redis 客戶端之間的沖突。Redis 鎖通常用于保護某些外部分布式資源(有關此問題的更多信息,請參閱我的回答。Redis 本身通常不需要鎖;由于 Redis 的單線程特性,許多命令已經是原子的,您可以使用事務或 Lua 腳本來組成任意復雜的原子操作。

因此,我的建議是設計您的客戶端代碼以使用原子性來避免沖突,而不是嘗試使用鎖(分布式鎖或其他鎖)。


查看完整回答
反對 回復 2023-06-26
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

實現 (Go) 如下所示:


// import "github.com/gomodule/redigo/redis"


type redisService struct {

? ? pool? ? ? *redis.Pool

? ? lastLogin *redis.Script // Lua script initialized into this field

}


// Constructing a redis client

func NewRedisService(config *config.RedisClientConfig) RedisService {

? ? return &redisService{

? ? ? ? pool: &redis.Pool{

? ? ? ? ? ? MaxIdle:? ? ?10,

? ? ? ? ? ? IdleTimeout: 120 * time.Second,

? ? ? ? ? ? Dial: func() (redis.Conn, error) {

? ? ? ? ? ? ? ? return redis.Dial("tcp", config.BaseURL)

? ? ? ? ? ? },

? ? ? ? ? ? TestOnBorrow: func(c redis.Conn, t time.Time) error {

? ? ? ? ? ? ? ? if time.Since(t) < time.Minute {

? ? ? ? ? ? ? ? ? ? return nil

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? _, err := c.Do("PING")

? ? ? ? ? ? ? ? return err

? ? ? ? ? ? },

? ? ? ? },

? ? ? ? // initialize Lua script object

? ? ? ? // lastLoginLuaScript is a Go const with the script literal

? ? ? ? lastLogin: redis.NewScript(1, lastLoginLuaScript),

? ? }

}

Lua腳本(注釋解釋了它的作用):


--[[

? ? Check if key exists, if it exists, update the value without changing the remaining TTL.

? ? If it doesn't exist, create it.


? ? Script params

? ? KEYS[1] = the account id used as key

? ? ARGV[1] = the key TTL in seconds

? ? ARGV[2] = the value

]]--

local errorKeyExpired = 'KEXP'

local statusKeyUpdated = 'KUPD'

local statusKeyCreated = 'KCRE'


if redis.call('exists', KEYS[1]) == 1 then

? ? local ttl = redis.call('ttl', KEYS[1])

? ? if ttl < 0 then --[[ no expiry ]]--

? ? ? ? redis.call('setex', KEYS[1], ARGV[1], ARGV[2])

? ? ? ? return redis.status_reply(statusKeyCreated)

? ? end

? ? if ttl == 0 then --[[ expired ]]--

? ? ? ? return redis.error_reply(errorKeyExpired)

? ? end


? ? redis.call('setex', KEYS[1], ttl, ARGV[2])

? ? return redis.status_reply(statusKeyUpdated)


else

? ? redis.call('setex', KEYS[1], ARGV[1], ARGV[2])

? ? return redis.status_reply(statusKeyCreated)

end

用法:


func (rs *redisService) UpsertLastLoginTime(key string, ttl uint, value int64) (bool, error) {

? ? conn := rs.pool.Get()

? ? defer conn.Close()


? ? // call Do on the script object

? ? resp, err := rs.lastLogin.Do(conn, key, ttl, value)


? ? switch resp {

? ? case statusKeyCreated:

? ? ? ? return true, nil


? ? case statusKeyUpdated:

? ? ? ? return false, nil


? ? case errorKeyExpired:

? ? ? ? return false, ErrKeyExpired


? ? default:

? ? ? ? return false, errors.Wrap(err, "script execution error")

? ? }

}


查看完整回答
反對 回復 2023-06-26
  • 2 回答
  • 0 關注
  • 244 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號