The lock component for Hyperf.

Maintainers

πŸ‘ huangdijia

Package info

github.com/friendsofhyperf/lock

Issues

Documentation

pkg:composer/friendsofhyperf/lock

Fund package maintenance!

huangdijia

hdj.me/sponsors

Statistics

Installs: 53 336

Dependents: 0

Suggesters: 1

Stars: 14

v3.2.1 2026-06-09 14:51 UTC

Requires

Requires (Dev)

None

Suggests

Provides

None

Conflicts

None

Replaces

None

MIT d694699787fd13e72ccf5326c4a606193c2dddf0

  • huangdijia <huangdijia.woop@gmail.com>

hyperfv3.2

This package is auto-updated.

Last update: 2026-06-17 01:08:34 UTC


README

πŸ‘ Latest Stable Version
πŸ‘ Total Downloads
πŸ‘ License

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 {
 $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...
 });
 }
}

Contact

License

MIT