### Install Hyperf Lock Component via Composer Source: https://github.com/friendsofhyperf/lock/blob/main/README.md This command adds the `friendsofhyperf/lock` package as a dependency to your Hyperf project using Composer, making the lock component available for use. ```shell composer require friendsofhyperf/lock ``` -------------------------------- ### Acquire Lock with Auto-Release Closure in Hyperf Source: https://github.com/friendsofhyperf/lock/blob/main/README.md This example shows how to acquire a lock by passing a closure to the `get()` method. The lock is held indefinitely while the closure executes and is automatically released upon its completion, simplifying lock management. ```php lock('foo')->get(function () { // Lock acquired indefinitely and automatically released... }); ``` -------------------------------- ### Manage Lock Using Annotation in Hyperf Source: https://github.com/friendsofhyperf/lock/blob/main/README.md This example shows how to declare a lock using the `#[Lock]` annotation on a class property. The lock instance is then automatically injected, allowing the `get()` method with a closure to be used for lock management within the class method. ```php use FriendsOfHyperf\Lock\Annotation\Lock; use FriendsOfHyperf\Lock\Driver\LockInterface; class Foo { #[Lock(name:"foo", seconds:10)] protected LockInterface $lock; public function bar() { $this->lock->get(function () { // Lock acquired indefinitely and automatically released... }); } } ``` -------------------------------- ### Block for Lock with Timeout and Auto-Release Closure in Hyperf Source: https://github.com/friendsofhyperf/lock/blob/main/README.md Similar to the previous example, this demonstrates blocking for a lock with a timeout, but it uses a closure for automatic release. The lock is acquired if available within the specified time and released automatically after the closure's execution. ```php lock('foo', 10)->block(5, function () { // Lock acquired after waiting maximum of 5 seconds... }); ``` -------------------------------- ### Publish Hyperf Lock Configuration Source: https://github.com/friendsofhyperf/lock/blob/main/README.md This command publishes the default configuration file for the `friendsofhyperf/lock` component to your Hyperf project's config directory, allowing for customization of lock settings. ```shell php bin/hyperf.php vendor:publish friendsofhyperf/lock -i config ``` -------------------------------- ### Acquire and Release a Lock in Hyperf Source: https://github.com/friendsofhyperf/lock/blob/main/README.md This snippet demonstrates how to acquire a named lock with a specified expiration time using the `lock()` helper function and then explicitly release it. The lock is acquired if available, and the `release()` method ensures it's freed. ```php $lock = lock($name = 'foo', $seconds = 10, $owner = null); if ($lock->get()) { // Lock acquired for 10 seconds... $lock->release(); } ``` -------------------------------- ### Block and Wait for Lock Acquisition with Timeout in Hyperf Source: https://github.com/friendsofhyperf/lock/blob/main/README.md This snippet illustrates how to attempt to acquire a lock by blocking for a maximum specified duration. If the lock cannot be acquired within the timeout, a `LockTimeoutException` is thrown, which can be caught to handle failure. The lock is released in a `finally` block. ```php use FriendsOfHyperf\Lock\Exception\LockTimeoutException; $lock = lock('foo', 10); try { $lock->block(5); // Lock acquired after waiting maximum of 5 seconds... } catch (LockTimeoutException $e) { // Unable to acquire lock... } finally { optional($lock)->release(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.