https://github.com/roadrunner-php/lock
🔌 PHP integration package for the RoadRunner Lock plugin, which allows for easy management of distributed locks in PHP applications. The plugin provides a fast, lightweight, and reliable way to acquire, release, and manage locks in a distributed environment.
https://github.com/roadrunner-php/lock
locks mutex mutex-lock php roadrunner
Last synced: 6 months ago
JSON representation
🔌 PHP integration package for the RoadRunner Lock plugin, which allows for easy management of distributed locks in PHP applications. The plugin provides a fast, lightweight, and reliable way to acquire, release, and manage locks in a distributed environment.
- Host: GitHub
- URL: https://github.com/roadrunner-php/lock
- Owner: roadrunner-php
- License: mit
- Created: 2023-03-15T09:37:36.000Z (about 3 years ago)
- Default Branch: 1.x
- Last Pushed: 2024-02-10T12:16:30.000Z (about 2 years ago)
- Last Synced: 2025-09-13T16:55:16.475Z (6 months ago)
- Topics: locks, mutex, mutex-lock, php, roadrunner
- Language: PHP
- Homepage: https://roadrunner.dev/
- Size: 25.4 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# RoadRunner locks
[](https://packagist.org/packages/roadrunner-php/lock)
[](https://packagist.org/packages/roadrunner-php/lock)
[](https://github.com/roadrunner-php/lock/actions)
[](https://github.com/roadrunner-php/lock/actions)
[](https://codecov.io/gh/roadrunner-php/lock/)
[](https://packagist.org/packages/roadrunner-php/lock)

This package provides a PHP integration package for the RoadRunner Lock plugin, which allows for easy management of
distributed locks in PHP applications. The plugin provides a fast, lightweight, and reliable way to acquire, release,
and manage locks in a distributed environment, making it ideal for use in high-traffic web applications and
microservices.
## Requirements
Make sure that your server is configured with following PHP version and extensions:
- PHP 8.1+
## Installation
You can install the package via composer:
```bash
composer require roadrunner-php/lock
```
## Usage
```php
use RoadRunner\Lock\Lock;
use Spiral\Goridge\RPC\RPC;
require __DIR__ . '/vendor/autoload.php';
$lock = new Lock(RPC::create('tcp://127.0.0.1:6001'));
```
### Acquire lock
Locks a resource so that it can be accessed by one process at a time. When a resource is locked, other processes that
attempt to lock the same resource will be blocked until the lock is released.
```php
$id = $lock->lock('pdf:create');
// Acquire lock with ttl - 10 seconds
$id = $lock->lock('pdf:create', ttl: 10);
// or
$id = $lock->lock('pdf:create', ttl: new \DateInterval('PT10S'));
// Acquire lock and wait 5 seconds until lock will be released
$id = $lock->lock('pdf:create', wait: 5);
// or
$id = $lock->lock('pdf:create', wait: new \DateInterval('PT5S'));
// Acquire lock with id - 14e1b600-9e97-11d8-9f32-f2801f1b9fd1
$id = $lock->lock('pdf:create', id: '14e1b600-9e97-11d8-9f32-f2801f1b9fd1');
```
### Acquire read lock
Locks a resource for shared access, allowing multiple processes to access the resource simultaneously. When a resource
is locked for shared access, other processes that attempt to lock the resource for exclusive access will be blocked
until all shared locks are released.
```php
$id = $lock->lockRead('pdf:create', ttl: 10);
// or
$id = $lock->lockRead('pdf:create', ttl: new \DateInterval('PT10S'));
// Acquire lock and wait 5 seconds until lock will be released
$id = $lock->lockRead('pdf:create', wait: 5);
// or
$id = $lock->lockRead('pdf:create', wait: new \DateInterval('PT5S'));
// Acquire lock with id - 14e1b600-9e97-11d8-9f32-f2801f1b9fd1
$id = $lock->lockRead('pdf:create', id: '14e1b600-9e97-11d8-9f32-f2801f1b9fd1');
```
### Release lock
Releases an exclusive lock or read lock on a resource that was previously acquired by a call to `lock()`
or `lockRead()`.
```php
// Release lock after task is done.
$lock->release('pdf:create', $id);
// Force release lock
$lock->forceRelease('pdf:create');
```
### Check lock
Checks if a resource is currently locked and returns information about the lock.
```php
$status = $lock->exists('pdf:create');
if($status) {
// Lock exists
} else {
// Lock not exists
}
```
### Update TTL
Updates the time-to-live (TTL) for the locked resource.
```php
// Add 10 seconds to lock ttl
$lock->updateTTL('pdf:create', $id, 10);
// or
$lock->updateTTL('pdf:create', $id, new \DateInterval('PT10S'));
```
## Testing
```bash
composer test
```
## Credits
- [butschster](https://github.com/butschster)
## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.