PHPackages                             latrell/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. latrell/lock

ActiveLibrary

latrell/lock
============

支持Redis与Memcached的并发锁。

1.6(7y ago)3222.8k↑542.9%9[1 issues](https://github.com/Latrell/Lock/issues)MITPHPPHP &gt;=5.5.9

Since Mar 8Pushed 7y ago2 watchersCompare

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

READMEChangelogDependencies (1)Versions (7)Used By (0)

Lock
====

[](#lock)

这是一个支持 Laravel 5 的并发锁拓展包。

该模块在 Redis 与 Memcache 上实现了锁机制。

注意：集群环境下，必须使用 Redis 驱动，否则由于 Memcache 的特性，锁可能出现上锁不准确的情况。

安装
--

[](#安装)

```
composer require latrell/lock dev-master

```

使用 `composer update` 更新包列表，或使用 `composer install` 安装。

找到 `config/app.php` 配置文件中的 `providers` 键，注册服务提供者。

（Laravel 5.5 以上版本可跳过该步骤）

```
    'providers' => [
        // ...
        Latrell\Lock\LockServiceProvider::class,
    ]
```

找到 `config/app.php` 配置文件中的 `aliases` 键，注册别名。

```
    'aliases' => [
        // ...
        'Lock' => Latrell\Lock\Facades\Lock::class,
    ]
```

运行 `php artisan vendor:publish` 命令，发布配置文件到你的项目中。

使用
--

[](#使用)

### 闭包方式

[](#闭包方式)

使用闭包的方式，可由方法内自动处理异常，并自动解锁，防止死锁。

但需注意，外部变量需要使用 `use` 引入才可在闭包中使用。

```
// 防止商品超卖。
$key = 'Goods:' . $goods_id;
Lock::granule($key, function() use($goods_id) {
	$goods = Goods::find($goods_id);
	if ( $goods->stock > 0 ) {
		// ...
	}
});
```

### 普通方式

[](#普通方式)

提供手动上锁与解锁方式，方便应用在复杂环境。

```
// 锁名称。
$key = 'Goods:' . $goods_id;

// **注意：除非特别自信，否则一定要记得捕获异常，保证解锁动作。**
try {

	// 上锁。
	Lock::acquire($key);

	// 逻辑单元。
	$goods = Goods::find($goods_id);
	if ( $goods->stock > 0 ) {
		// ...
	}
} finally {
	// 解锁。
	Lock::release($key);
}
```

### 中间件

[](#中间件)

使用中间件的方式，让两个相同指纹的请求同步执行。

找到 `app/Http/Kernel.php` 中的 `$routeMiddleware` 配置，添加中间件配置。

```
	protected $routeMiddleware = [
		// ...
		'synchronized' => \Latrell\Lock\Middleware\SynchronizationRequests::class,
	];

```

###  Health Score

36

—

LowBetter than 81% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 81.8% 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 ~183 days

Recently: every ~210 days

Total

6

Last Release

2794d ago

### Community

Maintainers

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

---

Top Contributors

[![latrell](https://avatars.githubusercontent.com/u/6267962?v=4)](https://github.com/latrell "latrell (18 commits)")[![motian](https://avatars.githubusercontent.com/u/2382215?v=4)](https://github.com/motian "motian (4 commits)")

---

Tags

laravellockredis-memcachedlaravellaravel 5l5lock

### Embed Badge

![Health badge](/badges/latrell-lock/health.svg)

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

###  Alternatives

[latrell/alipay

支付宝SDK在Laravel5封装包。

40349.2k](/packages/latrell-alipay)[addons/elasticsearch

Laravel's elasticsearch Paginator/Collection/Logger Helper.

472.5k](/packages/addons-elasticsearch)[latrell/rongcloud

融云服务端SDK在Laravel的封装包。

2111.7k](/packages/latrell-rongcloud)[latrell/geohash

This package geohash for Laravel 5 support.

1128.3k](/packages/latrell-geohash)[latrell/qqwry

纯真 IP 库 Laravel 版 。

274.7k](/packages/latrell-qqwry)

PHPackages © 2026

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