Interface for lock engines.
Lock engines provide distributed locking mechanisms to prevent race conditions when multiple processes access shared resources.
Represents an acquired lock.
Lock provides a consistent interface to distributed locking.
You can configure Lock engines in your application's bootstrap:
Lock::setConfig('default', [
'className' => \Cake\Lock\Engine\RedisLockEngine::class,
'host' => '127.0.0.1',
'port' => 6379,
]);
Prefer synchronized() when you can, as it guarantees prompt release:
$result = Lock::synchronized('my-resource', function () {
// Critical section
return $computedValue;
});
Acquiring and releasing a lock:
$lock = Lock::acquire('my-resource');
if ($lock !== null) {
try {
// Critical section
} finally {
$lock->release();
}
}
Abstract base class for lock engines.
Provides common functionality for all lock engine implementations, including configuration management and key generation.
Registry for lock engines.
Used by {@link \Cake\Lock\Lock} to manage lock engine instances.