6 Mart 2023 Pazartesi

Redis - Key-Value Veri Yapısı

SET
Şöyle yaparız
SET lock_name arbitrary_lock_value NX EX 10 # acquire the lock
# ... do something to the shared resource DEL lock_name # release the lock
SET NX
Atomic olarak çalışır. Eski hali SETNX. Sonradan SET komutuna NX parametresi haline geldi. Açıklaması şöyle
Because Redis has atomic writes, we can use a SET NX operation to only set the key if it’s not there. This means that Redis guarantees that only one worker will the race for a particular key. The official documentation says that the Redlock algorithm is preferred over SET NX because it “but offers better guarantees and is fault tolerant.” We are not looking to use a lock with Redis, however, just a one-way gate that prevents multiple execution. As long as Redis can prevent the race condition where two workers read the same value for the gate and both pass through it, that is all we need.
Örnek
Şöyle yaparız
SETNX lock_name true

DEL lock_name
EXPIRE
Örnek
Şöyle yaparız
SETNX lock_name arbitrary_lock_value
EXPIRE lock_name 10

Hiç yorum yok:

Yorum Gönder