PHPackages                             ihongwu/redis-redlock - 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. ihongwu/redis-redlock

ActiveLibrary[Caching](/categories/caching)

ihongwu/redis-redlock
=====================

PHP版本的Redis红锁，支持单机/多机Redis，支持传入Redis配置或者Redis对象等特性

v1.0.0(1y ago)317MITPHPPHP &gt;=5.6

Since Oct 13Pushed 1y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (2)Used By (0)

PHP版本redlock
============

[](#php版本redlock)

### PHP版本的Redis红锁，已支持以下特性：

[](#php版本的redis红锁已支持以下特性)

- 单机Redis
- 多机版Redis
- 传入Redis配置
- 传入Redis对象，方便在常驻内存的框架中使用，如webman
- 可重入
- 锁等待

### 注意事项

[](#注意事项)

- 锁超时时间默认为30秒
- 连接Redis超时时间默认50毫秒
- 重试获取锁次数默认200次，100毫秒重试一次

### 使用方法

[](#使用方法)

#### 安装

[](#安装)

```
composer require ihongwu/redis-redlock
```

or

```
git@github.com:ihongwu/redis-redlock.git
```

#### 常规使用：传入Redis配置信息

[](#常规使用传入redis配置信息)

```
use RedisLock\RedisRedLock;

// 有自动加载则不需要
require_once 'src/RedisRedLock.php';

$servers = [
    ['host' => '127.0.0.1', 'port' => 6391, 'password' => '88888888'],
    ['host' => '127.0.0.1', 'port' => 6392, 'password' => '88888888'],
    ['host' => '127.0.0.1', 'port' => 6393, 'password' => '88888888'],
];

$resourceKey = 'my_distributed_lock';

try {
    $redlock = new RedisRedLock($servers, $resourceKey);
    if ($redlock->lock()) {
        echo "成功获取锁，执行任务...\n";
        if ($redlock->lock()) {
            echo "再次获取锁，继续执行任务...\n";
        }

        if ($redlock->unlock()) {
            echo "锁已释放一次。\n";
        }

        if ($redlock->unlock()) {
            echo "锁已全部释放。\n";
        }
    } else {
        echo "获取锁失败。\n";
    }
} catch (\Exception $e) {
    var_dump($e->getMessage());
}
```

#### 常驻内存框架：传入Redis连接池，以webman为例

[](#常驻内存框架传入redis连接池以webman为例)

```
// 配置信息
return [
    '6391' => [
        'host' => '127.0.0.1',
        'password' => '88888888',
        'port' => 6391,
        'database' => 0,
        'pool' => [
            'min' => 5,
            'max' => 50,
            'timeout' => 5
        ]
    ],
    '6392' => [
        'host' => '127.0.0.1',
        'password' => '88888888',
        'port' => 6392,
        'database' => 0,
        'pool' => [
            'min' => 5,
            'max' => 50,
            'timeout' => 5
        ]
    ],
    '6393' => [
        'host' => '127.0.0.1',
        'password' => '88888888',
        'port' => 6393,
        'database' => 0,
        'pool' => [
            'min' => 5,
            'max' => 50,
            'timeout' => 5
        ]
    ],
];
```

```
use RedisLock\RedisRedLock;

// 不想传入连接池的，也可以直接传入Redis配置信息
// 连接成功的redis实例，才给锁对象使用
$servers = [];
try {
    $servers[] = Redis::connection('6391')->client();
} catch (\Exception $e) {}
try {
    $servers[] = Redis::connection('6392')->client();
} catch(\Exception $e) {}
try {
    $servers[] = Redis::connection('6393')->client();
} catch(\Exception $e) {}

$resourceKey = 'my_distributed_lock';

try {
    $redlock = new RedisRedLock($servers, $resourceKey);
    if ($redlock->lock()) {
        echo "成功获取锁，执行任务...\n";
        if ($redlock->lock()) {
            echo "再次获取锁，继续执行任务...\n";
        }

        if ($redlock->unlock()) {
            echo "锁已释放一次。\n";
        }

        if ($redlock->unlock()) {
            echo "锁已全部释放。\n";
        }
    } else {
        echo "获取锁失败。\n";
    }
} catch (\Exception $e) {
    var_dump($e->getMessage());
}
```

### 声明

[](#声明)

1、该库已通过库存200，并发1000的多轮测试并正常工作，但并不意味这它100%没问题
2、传入的redis配置/实例数理论上为奇数，但考虑到常驻内存框架自行判断连接后，最后可能达不到奇数的要求，所以只要可用连接数能达到N / 2 + 1即可
3、如发现问题，请随时提issues或提交更正代码，在生产环境中使用它之前，请务必了解它的工作原理

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

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

573d ago

### Community

Maintainers

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

---

Top Contributors

[![ihongwu](https://avatars.githubusercontent.com/u/21274377?v=4)](https://github.com/ihongwu "ihongwu (1 commits)")

### Embed Badge

![Health badge](/badges/ihongwu-redis-redlock/health.svg)

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

###  Alternatives

[predis/predis

A flexible and feature-complete Redis/Valkey client for PHP.

7.8k305.7M2.4k](/packages/predis-predis)[snc/redis-bundle

A Redis bundle for Symfony

1.0k39.4M67](/packages/snc-redis-bundle)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[wp-media/wp-rocket

Performance optimization plugin for WordPress

7431.3M3](/packages/wp-media-wp-rocket)[illuminate/cache

The Illuminate Cache package.

12835.6M1.4k](/packages/illuminate-cache)[colinmollenhour/php-redis-session-abstract

A Redis-based session handler with optimistic locking

6325.6M14](/packages/colinmollenhour-php-redis-session-abstract)

PHPackages © 2026

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