PHPackages                             frankeyfeng/hyperf-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. [Caching](/categories/caching)
4. /
5. frankeyfeng/hyperf-redis-lock

ActiveLibrary[Caching](/categories/caching)

frankeyfeng/hyperf-redis-lock
=============================

v2.1.7(2y ago)07.3kMITPHPPHP &gt;=7.3

Since Jan 31Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Frankeyfeng/hyperf-redis-lock)[ Packagist](https://packagist.org/packages/frankeyfeng/hyperf-redis-lock)[ RSS](/packages/frankeyfeng-hyperf-redis-lock/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (7)Versions (8)Used By (0)

hyperf-redis-lock
=================

[](#hyperf-redis-lock)

English | [中文](./README-zh.md)

an easy redis-based distributed-lock implementation for hyperf 2.\*。 This extension features distributed-lock includes block and unblock distributed-lock.

Principle
---------

[](#principle)

The command in `Redis` is atomic.so we use `set` to make sure that your application executes sequentially. The Redis version before 2.8 does't support the option `ex` in `set` command. so we just use `setnx+expire` instead.&gt;&gt; Version 1.\* The Redis version after 2.8 supports the option `ex` in `set` command. so we use `set nx ex`&gt;&gt; Version 2.\*

so version 2.\* supports the version of redis after 2.8. version 1.\* supports all the versions of redis.

Redis version
-------------

[](#redis-version)

`redis-server --version`

Install
-------

[](#install)

Run `composer require frankeyfeng/hyperf-redis-lock`

Usage
-----

[](#usage)

init the `redis` object

```
    /**
     * @var RedisProxy
     */
    protected $redis;

    public function __construct(RedisFactory $redisFactory)
    {
        $this->redis = $redisFactory->get('default');
    }

```

- non-block lock we try to acquire the lock then return the result directly.If acquire the lock, execute the Closure and return the Closure's result. or return false;

```
public function lock(ResponseInterface $response)
    {
        $lock = new RedisLock($this->redis, 'lock', 20);
        $res = $lock->get(function () {
            sleep(10);
            return [123];
        });
        return $response->json($res);
    }

```

- blocking lock we try acquire the lock first. 1.if failed we try to acquire the lock every 250 ms util timeout(the time we waiting for is bigger than the expire time you set in your application) 2.if success then execute the closure and return the result of closure.
    Tips: if timeout occurs in the process of application you need to catch the exception `LockTimeoutException` to handle this situation. for example:

```
/**
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function lockA(ResponseInterface $response)
    {
        try {
            $lock = new RedisLock($this->redis, 'lock', 4);
            $res = $lock->block(4, function () {
                return [456];
            });
            return $response->json(['res' => $res]);
        // catch the exception
        } catch (LockTimeoutException $exception) {
            var_dump('lockA lock check timeout');
            return $response->json(['res' => false, 'message' => 'timeout']);
        }
    }

```

Finally
-------

[](#finally)

#### Contributing

[](#contributing)

Feel free to create a fork and submit a pull request if you would like to contribute.

#### Bug reports

[](#bug-reports)

Raise an issue on GitHub if you notice something broken.

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85.7% 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 ~0 days

Total

6

Last Release

875d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6b173120e5bac0b6f43933e283da6d991c21676ce93af1367eeb2a49340d7050?d=identicon)[Frankeyfeng](/maintainers/Frankeyfeng)

---

Top Contributors

[![Frankeyfeng](https://avatars.githubusercontent.com/u/9375692?v=4)](https://github.com/Frankeyfeng "Frankeyfeng (6 commits)")[![fengxunxi](https://avatars.githubusercontent.com/u/150881384?v=4)](https://github.com/fengxunxi "fengxunxi (1 commits)")

---

Tags

phphyperf

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[lysice/hyperf-redis-lock

20104.8k7](/packages/lysice-hyperf-redis-lock)

PHPackages © 2026

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