friendsofhyperf / lock
The lock component for Hyperf.
Fund package maintenance!
huangdijia
hdj.me/sponsors
Requires
- hyperf/config: ~3.1.0
- hyperf/context: ~3.1.0
- hyperf/stringable: ~3.1.0
- hyperf/support: ~3.1.0
- nesbot/carbon: ^2.0 || ^3.0
Suggests
- hyperf/cache: Require this component for driver 'file'.
- hyperf/db-connection: Require this component for driver 'database'.
- hyperf/redis: Require this component for driver 'redis'.
- dev-main / 3.1.x-dev
- v3.1.48
- v3.1.41
- v3.1.35
- v3.1.32
- v3.1.31
- v3.1.28.1
- v3.1.27
- v3.1.20
- v3.1.19
- v3.1.18
- v3.1.17
- v3.1.15
- v3.1.1
- v3.1.0
- v3.1.0-rc.22
- v3.1.0-rc.7
- v3.1.0-rc.5
- v3.1.0-rc.4
- v3.1.0-beta.20
- v3.1.0-beta.15
- v3.1.0-beta.9
- v3.1.0-beta.1
- 3.0.x-dev
- v3.0.121
- v3.0.120
- v3.0.90
- v3.0.89
- v3.0.85
- v3.0.80
- v3.0.70
- v3.0.55
- v3.0.52
- v3.0.51
- v3.0.46
- v3.0.45
- v3.0.44
- v3.0.42
- v3.0.40
- v3.0.35
- v3.0.14
- v3.0.8
- v3.0.2
- v3.0.0
- v3.0.0-rc.30
- v3.0.0-rc.19
- v3.0.0-rc.16
- v3.0.0-rc.9
- v3.0.0-rc.7
- v3.0.0-rc.6
- v3.0.0-rc.4
- v3.0.0-rc.2
- v3.0.0-rc.1
- v3.0.0-beta35
- v3.0.0-beta34
- v3.0.0-beta33
- v3.0.0-beta32
- v3.0.0-beta29
- v3.0.0-beta28
- v3.0.0-beta27
- v3.0.0-beta26
- v3.0.0-beta25
- v3.0.0-beta24
- v3.0.0-beta23
- v3.0.0-beta22
- v3.0.0-beta21
- v3.0.0-beta20
- v3.0.0-beta19
- v3.0.0-beta18
- v3.0.0-beta17
- v3.0.0-beta16
- v3.0.0-beta15
- v3.0.0-beta14
- v3.0.0-beta13
- v3.0.0-beta12
- v3.0.0-beta11
- v3.0.0-beta10
- v3.0.0-beta9
- v3.0.0-beta8
- v3.0.0-beta7
- v3.0.0-beta6
- v3.0.0-beta5
- v3.0.0-beta4
- v3.0.0-beta3
- v3.0.0-beta2
- v3.0.0-beta1
- 2.0.x-dev
- v2.0.28
- v2.0.24
- v2.0.22
- v2.0.19
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0.0-beta3
- v2.0.0-beta2
- v2.0.0-beta1
This package is auto-updated.
Last update: 2024-12-04 02:41:56 UTC
README
The lock component for Hyperf. 中文说明
Installation
- Require
composer require friendsofhyperf/lock
- Publish
php bin/hyperf.php vendor:publish friendsofhyperf/lock -i config
Usage
You may create and manage locks using the lock()
method:
$lock = lock($name = 'foo', $seconds = 10, $owner = null); if ($lock->get()) { // Lock acquired for 10 seconds... $lock->release(); }
The get
method also accepts a closure. After the closure is executed, Will automatically release the lock:
lock('foo')->get(function () { // Lock acquired indefinitely and automatically released... });
If the lock is not available at the moment you request it, you may instruct the lock to wait for a specified number of seconds. If the lock can not be acquired within the specified time limit, an FriendsOfHyperf\Lock\Exception\LockTimeoutException
will be thrown:
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(); } lock('foo', 10)->block(5, function () { // Lock acquired after waiting maximum of 5 seconds... });
Using by annotation
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... }); } }