M
M
Mykola Ivashchuk2019-08-01 21:59:05
Redis
Mykola Ivashchuk, 2019-08-01 21:59:05

How to work with atomic locks in Laravel?

It is necessary to use a cache in order for the work on one entity to be performed by the user who received it first, and so that it is tied to this user for a certain time.
Judging by the dock, Atomic Locks
are suitable for this, but I don’t understand the logic of their work, can someone explain more specifically?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-08-01
@mykolaim

Well, xs is much easier than in the dock:

$product = Product::first();

$lock = Cache::lock('product_edit.' . $product->id, 10);

if ($lock->get()) {
    // ту-ту-ту, я делаю что хочу
    $product->name = 'flower pot';
    $product->save();

    // ла-ла-ла, я сделал что хотел
    // дам возможность другим редактировать эту сущность заранее, зачем ждать еще 9 секунд
    $lock->release();
} else {
    // сорри чел, кто-то другой редагирует эту сущность
}

The part with "tu-tu-tu" and "lock->release()" can, for example, be moved to where this entity is stored in order to reset the lock if the user has finished editing the entity.
In general, the concept is simple, apply

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question