PHPackages                             libratechie/think-distributed-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. libratechie/think-distributed-lock

ActiveLibrary

libratechie/think-distributed-lock
==================================

一个专为 ThinkPHP 框架设计的轻量级分布式锁组件，帮助开发者轻松实现高并发场景下的资源互斥访问

v1.0.0(1y ago)11Apache-2.0PHPPHP &gt;=8.0.0

Since Feb 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/libratechie/think-distributed-lock)[ Packagist](https://packagist.org/packages/libratechie/think-distributed-lock)[ RSS](/packages/libratechie-think-distributed-lock/feed)WikiDiscussions master Synced 1mo ago

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

think-distributed-lock
======================

[](#think-distributed-lock)

专为 ThinkPHP 框架设计的轻量级分布式锁组件，帮助开发者轻松实现高并发场景下的资源互斥访问。

特性
--

[](#特性)

- 自动锁释放与防死锁机制
- 简单直观的 API 设计
- 完美适配 ThinkPHP 8.x
- 开箱即用的配置方案

安装
--

[](#安装)

```
composer require libratechie/think-distributed-lock
```

配置
--

[](#配置)

在 config/distributed\_lock.php 中配置（文件不存在时会自动创建）：

```
return [
    // 默认使用的数据库连接配置
    'default'         => env('LOCK_DRIVER', 'redis'),
    'connections'     => [
        'redis' => [
            // 驱动方式
            'type' => 'redis',
            // 服务器地址
            'host' => env('LOCK_REDIS_HOST', '127.0.0.1'),
            // 端口
            'port' => env('LOCK_REDIS_PORT', 6379),
            // 锁前缀
            'prefix' => env('LOCK_REDIS_PREFIX', 'lock_'),

        ],
    ]
];
```

使用指南
----

[](#使用指南)

```
use DistributedLock\DisLockFactory;

// 初始化锁
$dis_lock = DisLockFactory::getInstance();

public function orderCreate($buyer_id)
{
    $lock_key = "user_submit_order:{$buyer_id}";

    // 尝试获取锁（等待10秒）
    if (!$lock = $dis_lock->lock($lock_key, 10)) {
        return json(['error' => '操作过于频繁，请稍后重试']);
    }

    try {
        // 你的业务逻辑...
        // 例如：订单创建、库存扣减等

        return json(['status' => 'success']);
    } catch (\Exception $e) {
        // 异常处理...
        return json(['error' => $e->getMessage()]);
    } finally {
        // 确保最终释放锁
        $dis_lock->unlock($lock_key);
    }
}
```

注意事项
----

[](#注意事项)

1. **异常处理**
    确保在 try-catch 块中进行业务操作，避免异常导致锁无法释放；
2. **不可重入锁**
    本项目当前不支持可重入锁机制。即同一线程在持有锁的情况下，无法再次获得该锁。请确保锁的使用符合这一约束；
3. **默认超时时间**
    锁资源的默认超时时间为60秒。如果在超时时间内没有释放锁，锁将自动过期并被释放；
4. **节点故障与锁资源释放**
    如果锁持有的节点发生故障或宕机，锁资源的释放可能会延迟。这意味着，在锁资源未释放之前，其他节点无法获得该锁。此期间的分布式锁将无法被任何节点使用，直到故障节点恢复或锁被手动释放；
5. **PHP客户端的限制**
    目前，PHP客户端不具备类似于Java `Redisson`的自动监控机制，因此，PHP实现的分布式锁可能会面临锁超时后无法自动延长有效期的问题。我们也欢迎大家通过Pull Request参与贡献，帮助改进这一特性。

免责声明
----

[](#免责声明)

在您的生产环境中使用本项目之前，请确保了解其工作原理，并在充分测试后再进行部署。我们不对因使用本项目造成的任何损失或故障负责。

贡献
--

[](#贡献)

欢迎通过 Issue 提交问题或 Pull Request 参与贡献

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance42

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 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

468d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3fa85c5a3c02014f51ff2f57c9f5f1b9e230a6400422a231e200271726fa0584?d=identicon)[libratechie](/maintainers/libratechie)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/libratechie-think-distributed-lock/health.svg)

```
[![Health](https://phpackages.com/badges/libratechie-think-distributed-lock/health.svg)](https://phpackages.com/packages/libratechie-think-distributed-lock)
```

###  Alternatives

[topthink/think

the new thinkphp framework

8.0k1.2M13](/packages/topthink-think)[topthink/think-queue

The ThinkPHP6 Queue Package

640675.0k75](/packages/topthink-think-queue)[topthink/think-swoole

Swoole extend for thinkphp

477174.4k19](/packages/topthink-think-swoole)[topthink/think-captcha

captcha package for thinkphp

132934.4k68](/packages/topthink-think-captcha)[topthink/think-worker

workerman extend for thinkphp

202227.2k9](/packages/topthink-think-worker)[topthink/think-migration

96460.8k121](/packages/topthink-think-migration)

PHPackages © 2026

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