PHPackages                             zonghay/redlock-hyperf - 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. zonghay/redlock-hyperf

ActiveLibrary

zonghay/redlock-hyperf
======================

redis redlock for hyperf

117.3k↓100%1[1 issues](https://github.com/zonghay/redlock-hyperf/issues)PHP

Since Mar 18Pushed 5y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

redlock-hyperf
==============

[](#redlock-hyperf)

```
composer require zonghay/redlock-hyperf

```

Based on [redlock-php](https://github.com/ronnylt/redlock-php) transform to [Hyperf 2.1.\*](https://github.com/hyperf/hyperf)

本sdk基于redlock-php向hyperf ～2.1版本改造。

使用前建议先了解一下Redlock算法的原理，[Redis作者Antirez文章解释Redlock算法（英文）](http://antirez.com/news/77)

我自己也写了一篇文章对RedLock进行了翻译 [基于Redis的分布式锁算法RedLock及RedLock-Hyperf实现](https://segmentfault.com/a/1190000039663994)

### 使用

[](#使用)

#### 简单使用

[](#简单使用)

```
    try {
        $lock = $this->container->get(RedLock::class)->setRedisPoolName()->setRetryCount(1)->lock('redlock-hyperf-test', 60000);
        if ($lock) {
            //do your code
            $this->container->get(RedLock::class)->unlock($lock);
        }
    } catch (\Throwable $throwable) {
        var_dump($throwable->getMessage());
    }
```

- setRedisPoolName方法用于指定Redlock使用哪些Redis实例作为分布式独立节点，这里需要传入索引数组，默认\['default'\]，数组的值应该是/config/autoload/redis.php下的连接池name 关于为什么要使用独立的Redis节点：

    [![img.png](img.png)](img.png)
- setRetryCount方法用于设置获取锁的重试次数，默认2次
- setRetryDelay 用于一次获取锁失败后延迟时间后重试，默认200，单位毫秒
- lock方法，获取锁

    - resource：锁的key
    - ttl：锁过期时间，单位毫秒。
    - return：array|false
- unlock方法，释放锁

    - 参数：lock方法成功后的return
- 如果担心请求保持锁阶段进程出现重启或退出情况，建议增加以下代码

```
//参考 RedlockHyperf\Aspect\RedLockAspect
if ($lock) {
  //to release lock when server receive exit sign
  Coroutine::create(function () use ($lock) {
  $exited = CoordinatorManager::until(Constants::WORKER_EXIT)->yield($lock['validity']);
  $exited && $this->redlock->unlock($lock);
  });
  //do your code
  $this->redlock->unlock($lock);
  return $result;
}
```

#### 注解使用

[](#注解使用)

```
class IndexController extends AbstractController
{
    /**
     * @RedLockAnnotation(resource="redlock-hyperf-test", poolName={"default"})
     */
    public function index() {}
}
```

SDK提供 RedlockHyperf\\Annotation\\RedLockAnnotation 注解，作用类于方法上，可以配置resource（必填），poolName，poolName，poolName，clockDriftFactor，ttl等参数

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

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/24c2cce85c60606e4668ce8f0fa9b211c64ba5a3347fb32531f303bacc88917a?d=identicon)[zonghay](/maintainers/zonghay)

### Embed Badge

![Health badge](/badges/zonghay-redlock-hyperf/health.svg)

```
[![Health](https://phpackages.com/badges/zonghay-redlock-hyperf/health.svg)](https://phpackages.com/packages/zonghay-redlock-hyperf)
```

PHPackages © 2026

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