Lock component for hyperf.

1.0.0 2021-08-05 00:50 UTC

This package is auto-updated.

Last update: 2021-11-18 09:50:38 UTC


Latest Stable Version Total Downloads GitHub license

Lock component for hyperf. 中文说明


  • Request
composer require "friendsofhyperf/lock"
  • Publish
php bin/hyperf.php vendor:publish friendsofhyperf/lock -i config


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...


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 acquired after waiting maximum of 5 seconds...
} catch (LockTimeoutException $e) {
    // Unable to acquire lock...
} finally {

lock('foo', 10)->block(5, function () {
    // Lock acquired after waiting maximum of 5 seconds...