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.3(2mo ago)012MITPHPPHP &gt;=7.4

Since Oct 15Pushed 2mo 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 today

READMEChangelog (2)Dependencies (20)Versions (3)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

36

—

LowBetter than 79% of packages

Maintenance85

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 53.6% 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 ~547 days

Total

2

Last Release

79d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/14801415?v=4)[青莲](/maintainers/kangkang123)[@kangkang123](https://github.com/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 (13 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

[hyperf/database

A flexible database library.

192.9M321](/packages/hyperf-database)[hyperf/http-server

A HTTP Server for Hyperf.

103.0M371](/packages/hyperf-http-server)[hyperf/database-pgsql

A pgsql handler for hyperf/database.

12321.2k21](/packages/hyperf-database-pgsql)[hyperf/watcher

Hot reload watcher for Hyperf

17904.9k31](/packages/hyperf-watcher)

PHPackages © 2026

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