michel/lock
Composer 安装命令:
composer require michel/lock
包简介
A flexible PHP locking library designed to prevent race conditions in CLI and web environments.
README 文档
README
A flexible PHP locking library designed to prevent race conditions in CLI and web environments. It provides a simple Locker class that delegates the actual locking mechanism to a LockHandlerInterface implementation.
Features
- Simple API: Easy to use
lock()andunlock()methods. - Handler Abstraction: Easily swap between different locking backends (e.g.,
FlockHandlerfor file-based locking). - Automatic Cleanup: Locks are automatically released when the
Lockerinstance is destroyed or (optionally) on script shutdown. - Blocking & Non-Blocking: Supports both blocking (wait for lock) and non-blocking (fail immediately) modes.
Installation
composer require michel/lock
Usage
Basic Usage (Non-Blocking)
By default or when passing false as the second argument, the lock attempt is non-blocking. It returns false immediately if the lock is already held by another process.
<?php
use Michel\Lock\Locker;
use Michel\Lock\Handler\FlockHandler;
// 1. Create a handler (e.g., file-based locking)
$handler = new FlockHandler('/tmp/locks');
// 2. Instantiate the Locker
$locker = new Locker($handler);
// 3. Acquire a lock
// 'my_process' is the key.
// false = non-blocking mode (return false immediately if locked)
if ($locker->lock('my_process', false)) {
echo "Lock acquired!\n";
// Do some critical work...
sleep(5);
// 4. Release the lock
$locker->unlock('my_process');
} else {
echo "Could not acquire lock. Another process is running.\n";
}
Blocking Mode (Wait for Lock)
If you want the script to pause and wait until the lock becomes available (e.g., waiting for another process to finish), set the second argument to true.
// Try to acquire lock, waiting indefinitely until it is available
// true = blocking mode
if ($locker->lock('heavy_task', true)) {
echo "Lock acquired after waiting (if necessary).\n";
// Perform task
// ...
$locker->unlock('heavy_task');
}
Automatic Unlock on Shutdown
You can ensure locks are released even if the script is killed or ends unexpectedly by using unlockIfKill().
$locker = new Locker($handler);
$locker->unlockIfKill(); // Registers a shutdown function
if ($locker->lock('critical_job')) {
// ... work ...
}
// Lock will be released automatically when script ends
Key Validation
Keys must be non-empty and contain only alphanumeric characters, underscores, and dots (/^[a-zA-Z0-9_.]+$/).
License
MPL-2.0 License.
michel/lock 适用场景与选型建议
michel/lock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「lock」 「mutex」 「flock」 「native-php」 「process-synchronization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 michel/lock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 michel/lock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 michel/lock 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Redis distributed locks for laravel
CacheMutex for Yii2
Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking
A Symfony bundle for Mutex implementation for PHP
Run mutually exclusive migrations from more than one server at a time
Simple php library providing OOP-interface to file locks.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MPL-2.0
- 更新时间: 2026-01-02