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

ActiveLibrary[Caching](/categories/caching)

str-kang/hyperf-redis-lock
==========================

2.2(1y ago)06MITPHPPHP &gt;=7.4

Since Oct 15Pushed 1y agoCompare

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

READMEChangelog (1)Dependencies (10)Versions (2)Used By (0)

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

[](#hyperf-redis-lock)

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

an easy redis-based distributed-lock implementation for hyperf 3.1.\*。 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 str-kang/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

23

—

LowBetter than 27% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

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

Unknown

Total

1

Last Release

571d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a3055304155139831ad3399da498c5edf9ce76d5eec94539d0c170982ae3828e?d=identicon)[kangkang123](/maintainers/kangkang123)

---

Top Contributors

[![Lysice](https://avatars.githubusercontent.com/u/17869820?v=4)](https://github.com/Lysice "Lysice (15 commits)")[![StrKang](https://avatars.githubusercontent.com/u/44571572?v=4)](https://github.com/StrKang "StrKang (6 commits)")

---

Tags

phphyperf

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[lysice/hyperf-redis-lock

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

A pgsql handler for hyperf/database.

12282.0k13](/packages/hyperf-database-pgsql)

PHPackages © 2026

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