1. Go to this page and download the library: Download mvonline/locker library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Mvonline\Locker\Traits\HasLocks;
class OrderProcessor
{
use HasLocks;
public function processOrder()
{
$this->lockResource('order-'.$this->id)
->type('reentrant')
->ttl(30)
->run(fn() => {
// critical section
});
}
// Or use the simpler helper
public function updateOrder()
{
$this->withLock('order-'.$this->id, fn() => {
// protected code
}, type: 'reentrant', ttl: 60);
}
}
$lock = Locker::lock('resource')
->type('safe')
->ttl(30)
->acquire();
try {
// Do work
} finally {
$lock->release();
}
Locker::lock('resource')
->type('safe')
->ttl(30)
->block(5) // Wait up to 5 seconds
->run(fn() => {});
use Mvonline\Locker\Events\LockAcquired;
Event::listen(LockAcquired::class, function ($event) {
Log::info("Lock acquired: {$event->key} by {$event->owner}");
});
// Check if locked
Locker::isLocked('key');
Locker::isLocked('key', 'simple');
// Force release (use with caution)
Locker::forceRelease('key');