PHPackages                             lixiaokai/redis-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. lixiaokai/redis-lock

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lixiaokai/redis-lock
====================

Redis 分布式锁

10PHP

Since May 5Pushed 3y ago1 watchersCompare

[ Source](https://github.com/lixiaokai/redis-lock)[ Packagist](https://packagist.org/packages/lixiaokai/redis-lock)[ RSS](/packages/lixiaokai-redis-lock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

介绍
==

[](#介绍)

Redis 分布式锁 For [Hyperf 3.0](https://hyperf.wiki/3.0/#/)，该组件衍生于 [Laravel](https://laravelacademy.org/post/22000#toc-12) 缓存系统的原子锁并做了 Hyperf 适配。

安装
==

[](#安装)

注意：Redis 服务端的版本需 &gt;= 2.6.12 ( 该版本开始 SET 命令才支持设置 `EX` `PX` 过期选项 )

```
composer require lixiaokai/redis-lock

```

使用
==

[](#使用)

原理
--

[](#原理)

- `NX`：建不存在才设置
- `EX`: 自动过期秒数

```
SET lockKey uniqueValue NX EX 10

```

例子
--

[](#例子)

原子锁允许对分布式锁进行操作而不必担心竞争条件。你可以使用如下方法来创建和管理锁：

```
use Hyperf\Context\ApplicationContext;
use Lixiaokai\RedisLock\RedisLock;

$redis = ApplicationContext::getContainer()->get(Redis::class);
$lock = new RedisLock($redis, 'lock:test', 10);

if ($lock->get()) {
    // 锁定时间为 10 秒...

    $lock->release();
}
```

`get` 方法可以接受一个闭包。在闭包执行之后，将会自动释放锁

```
$lock->get(function () {
    // 锁定时间为 10 秒...
});
```

如果你在请求时锁无法使用，你可以控制等待指定的秒数。

如果在指定的时间限制内无法获取锁，则会抛出 `Lixiaokai\RedisLock\Exception\LockTimeoutException`

```
use Hyperf\Context\ApplicationContext;
use Lixiaokai\RedisLock\RedisLock;
use Lixiaokai\RedisLock\Exception\LockTimeoutException;

$redis = ApplicationContext::getContainer()->get(Redis::class);
$lock = new RedisLock($redis, 'lock:test', 10);

try {
    $lock->block(5);
    // 为获得锁等待最多 5 秒...
} catch (LockTimeoutException $e) {
    // 无法获取锁...
} finally {
    $lock->release();
}
```

通过向 `block` 方法传递闭包，可以简化上述示例。当闭包传递给此方法时，将尝试在指定的秒数内获取锁，并在执行闭包后自动释放锁：

```
use Hyperf\Context\ApplicationContext;
use Lixiaokai\RedisLock\RedisLock;

$redis = ApplicationContext::getContainer()->get(Redis::class);
$lock = new RedisLock($redis, 'lock:test', 10);

$lock->block(5, function () {
    // 为获得锁等待最多 5 秒...
});
```

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity22

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/57b9f517bb089278c3a6ba6ec12db3dfc37f2eace43cbe9ab90f2a8bfa056230?d=identicon)[lixiaokai](/maintainers/lixiaokai)

---

Top Contributors

[![lixiaokai](https://avatars.githubusercontent.com/u/2142656?v=4)](https://github.com/lixiaokai "lixiaokai (1 commits)")

### Embed Badge

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

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

###  Alternatives

[dazet/data-map

Library for mapping data structures.

2123.7k](/packages/dazet-data-map)

PHPackages © 2026

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