symfony/lock
Symfony Lock component provides a unified API to create and manage locks, ensuring exclusive access to shared resources. Supports multiple backends (e.g., filesystem, Redis, PDO) to prevent race conditions in concurrent PHP apps.
Lock facade, Jobs, and Service Container, reducing boilerplate and leveraging Laravel’s ecosystem (e.g., dispatch() with locks, handle() in jobs).Lock facade and dependency injection simplify integration (e.g., LockFactory for custom stores).LockKeyNormalizer in v8.1+) to avoid edge cases in distributed systems.symfony/lock v6.x).dispatch()), resource-level (e.g., user:inventory:123), or workflow-level (e.g., payment:transaction:abc)?Lock facade in handle() or dispatch() with Lock middleware.Horizon workers or dispatch() calls with LockStore.Artisan commands with Lock (e.g., php artisan migrate --lock=database).Event listeners across workers with Lock.LockFactory for custom stores (e.g., RedisStore, PdoStore).LockKeyNormalizer (v8.1+) for consistent key generation across distributed systems.flock()).Lock.config(['lock.store' => env('LOCK_STORE', 'redis')])).Lock facade calls (e.g., Lock::acquire('resource:123')).symfony/lock v6.x.Lock facade support (minimal integration needed).composer require symfony/lock.config/lock.php (e.g., Redis DSN, PDO connection).php artisan vendor:publish --tag=lock-config.LockFactory in AppServiceProvider if using custom stores.Lock to job handle():
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\RedisStore;
public function handle(LockFactory $lockFactory)
{
$lock = $lockFactory->createLock('inventory:update:123', new RedisStore());
$acquired = $lock->acquire(true); // Block until acquired
if ($acquired) {
// Critical section
$lock->release();
}
}
Lock middleware in Horizon or dispatch():
$job
How can I help you explore Laravel packages today?