PHPackages                             weijiajia/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. weijiajia/hyperf-redis-lock

ActiveLibrary[Caching](/categories/caching)

weijiajia/hyperf-redis-lock
===========================

v2.2.2(2y ago)071MITPHPPHP &gt;=7.3

Since Oct 18Pushed 2y ago1 watchersCompare

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

READMEChangelog (2)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 lysice/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

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.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 ~149 days

Recently: every ~223 days

Total

7

Last Release

771d ago

Major Versions

v1.0 → v2.02021-10-18

v1.1 → v2.1.12022-12-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/636557533fde1107bb301eca908de1c17c9ea7073d5c9c0fbad725ccd0800bde?d=identicon)[jackchang1025](/maintainers/jackchang1025)

---

Top Contributors

[![Lysice](https://avatars.githubusercontent.com/u/17869820?v=4)](https://github.com/Lysice "Lysice (15 commits)")[![jackchang1025](https://avatars.githubusercontent.com/u/71494064?v=4)](https://github.com/jackchang1025 "jackchang1025 (3 commits)")

---

Tags

phphyperf

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[lysice/hyperf-redis-lock

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

PHPackages © 2026

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