arnapou / lock
Library - Simple lock interface and adapters.
v1.2
2024-04-05 00:03 UTC
Requires
- php: ~8.3.0
Requires (Dev)
- ext-redis: *
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- 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.3 |
---|---|---|
09/01/2024 | 1.x, main | × |