sacred/prevent-race-condition

此包允许您锁定代码的一部分以防止竞争条件。

1.0.2 2022-11-19 17:30 UTC

This package is auto-updated.

Last update: 2024-09-21 10:27:34 UTC


README

codecov

此包允许您锁定代码的一部分以防止竞争条件。

安装

composer require sacred/prevent-race-condition

基本用法

您需要PSR-6或PSR-16缓存才能使用此包

use PreventRaceCondition\RaceConditionFactory;

// $somePSR16Cache is PSR16 any cache instance 
$raceCondition = RaceConditionFactory::fromCacheItemPool($somePSR16Cache);

// or

// $somePSR6Cache is PSR6 any cache instance 
$raceCondition = RaceConditionFactory::fromSimpleCache($somePSR6Cache);


while($raceCondition->isBusy('lock-name')) {
    // wait or do something else
}

$raceCondition->lock('lock-name');

// do something

$raceCondition->release('lock-name');