arnapou / lock
Library - Simple lock interface and adapters.
v1.3.0
2024-11-25 08:49 UTC
Requires
- php: ~8.3.0 || ~8.4.0
Requires (Dev)
- ext-redis: *
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
README
This library helps you to manage basic locks for async processes or race condition problems.
Installation
composer require arnapou/lock
packagist 👉️ arnapou/lock
Introduction
This library provides basic lock interfaces and adapters.
It mainly follows the KISS principle.
If you need advanced features, go look at symfony/lock or other equivalent libraries.
Here, the goal is minimalist code for 90% of needs.
Basic usage
$locker = new \Arnapou\Lock\Adapter\RedisLocker(
redis: $redis,
autorelease: true,
defaultTtl: 60,
namespace: 'project:locks:'
);
if ($locker->acquire('my_lock')) {
// do your process
$locker->release('my_lock');
} else {
// delay or report an error
}
Decorators
We can enhance the behaviour for instance with a retry mechanism :
$waitingLocker = new \Arnapou\Lock\Decorator\WaitingLocker(
internal: $redisLocker,
maxTotalWaitSeconds: 1.0,
minLoopWaitMilliseconds: 20,
maxLoopWaitMilliseconds: 150
);
if ($locker->acquire('my_lock')) {
// do your process
$locker->release('my_lock');
} else {
// delay or report an error
}
Php versions
Date | Ref | 8.4 | 8.3 |
---|---|---|---|
25/11/2024 | 1.3.x, main | × | × |
09/01/2024 | 1.0 - 1.2 | × |