PHPackages                             friendsofhyperf/lock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. friendsofhyperf/lock

ActiveLibrary

friendsofhyperf/lock
====================

The lock component for Hyperf.

v3.1.77(5mo ago)1447.4k↓15.8%1MITPHP

Since May 21Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/friendsofhyperf/lock)[ Packagist](https://packagist.org/packages/friendsofhyperf/lock)[ Fund](https://hdj.me/sponsors/)[ GitHub Sponsors](https://github.com/huangdijia)[ RSS](/packages/friendsofhyperf-lock/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (118)Used By (0)

Lock
====

[](#lock)

[![Latest Stable Version](https://camo.githubusercontent.com/dc0091604ca321e75e6e174684be4449f4a1cbf8160abe81f7f095a0f723d834/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f667269656e64736f666879706572662f6c6f636b)](https://packagist.org/packages/friendsofhyperf/lock)[![Total Downloads](https://camo.githubusercontent.com/4c543c1b109063383b3a9d809cbd2f4fe5a2c7c183b00bdcc7b459eae4c2a9a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f667269656e64736f666879706572662f6c6f636b)](https://packagist.org/packages/friendsofhyperf/lock)[![License](https://camo.githubusercontent.com/a43691130aaeba962b34ff3db247b0848026b5d55a5dea8c46feeaf36c468b41/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f667269656e64736f666879706572662f6c6f636b)](https://github.com/friendsofhyperf/lock)

The lock component for Hyperf. [中文说明](README_CN.md)

Installation
------------

[](#installation)

- Require

```
composer require friendsofhyperf/lock
```

- Publish

```
php bin/hyperf.php vendor:publish friendsofhyperf/lock -i config
```

Usage
-----

[](#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
-------

[](#contact)

- [Twitter](https://twitter.com/huangdijia)
- [Gmail](mailto:huangdijia@gmail.com)

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance73

Regular maintenance activity

Popularity37

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 94.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Recently: every ~4 days

Total

117

Last Release

154d ago

Major Versions

v2.0.9 → v3.0.0-beta212022-06-24

v2.0.13 → v3.0.0-beta282022-06-24

v2.0.19 → v3.0.0-rc.162022-09-21

v2.0.24 → v3.0.02022-12-29

2.0.x-dev → v3.0.82023-02-02

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8337659?v=4)[Deeka Wong](/maintainers/huangdijia)[@huangdijia](https://github.com/huangdijia)

---

Top Contributors

[![huangdijia](https://avatars.githubusercontent.com/u/8337659?v=4)](https://github.com/huangdijia "huangdijia (82 commits)")[![tw2066](https://avatars.githubusercontent.com/u/24579418?v=4)](https://github.com/tw2066 "tw2066 (2 commits)")[![xuanyanwow](https://avatars.githubusercontent.com/u/28777109?v=4)](https://github.com/xuanyanwow "xuanyanwow (2 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (1 commits)")

---

Tags

hyperfv3.1

### Embed Badge

![Health badge](/badges/friendsofhyperf-lock/health.svg)

```
[![Health](https://phpackages.com/badges/friendsofhyperf-lock/health.svg)](https://phpackages.com/packages/friendsofhyperf-lock)
```

###  Alternatives

[hyperf/database

A flexible database library.

202.8M257](/packages/hyperf-database)[hyperf/validation

hyperf validation

122.1M166](/packages/hyperf-validation)[hyperf/http-server

A HTTP Server for Hyperf.

102.8M305](/packages/hyperf-http-server)[hyperf/crontab

A crontab component for Hyperf.

131.6M62](/packages/hyperf-crontab)[limingxinleo/hyperf-utils

Utils for Hyperf.

29133.9k3](/packages/limingxinleo-hyperf-utils)[friendsofhyperf/sentry

The sentry component for Hyperf.

1864.6k](/packages/friendsofhyperf-sentry)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
