danil-kashin/file-lock
Composer 安装命令:
composer require danil-kashin/file-lock
包简介
File-based exclusive locking via flock().
README 文档
README
A lightweight PHP library for file-based exclusive locking using flock().
Requirements
- PHP 8.1 or higher
Installation
composer require danil-kashin/file-lock
Usage
Basic acquire and release
use DanilKashin\FileLock\FileLock; $lock = new FileLock('/tmp/my-process.lock'); $lock->acquire(); // blocks until the lock is obtained // ... critical section ... $lock->release();
Clean up the lock file after use
$lock = new FileLock('/tmp/my-process.lock'); $lock->acquire(); // ... critical section ... $lock->release(); $lock->deleteFile();
Error handling
use DanilKashin\FileLock\Exceptions\FileLockException; $lock = new FileLock('/tmp/my-process.lock'); try { $lock->acquire(); // ... critical section ... } catch (FileLockException $e) { // lock file could not be opened or the lock could not be acquired echo $e->getMessage(); } finally { $lock->release(); }
API
FileLock::__construct(string $lockFile)
Creates a new FileLock instance. The $lockFile path is the file used as the lock.
The file is created automatically on the first call to acquire() if it does not exist.
acquire(): void
Acquires an exclusive lock (LOCK_EX). Blocks until the lock becomes available.
Calling acquire() multiple times on the same instance is safe — the file handle is reused and flock is re-entrant on the same process.
Throws FileLockException if the lock file cannot be opened.
release(): void
Releases the exclusive lock (LOCK_UN). The lock file itself is kept on disk.
deleteFile(): void
Deletes the lock file from disk. Call this after release() when the file is no longer needed.
How it works
FileLock opens the target file in cb mode (create if missing, binary) and delegates all locking to the operating system via PHP's flock(). This makes it safe to use across multiple processes on the same machine.
Running tests
composer install vendor/bin/phpunit
danil-kashin/file-lock 适用场景与选型建议
danil-kashin/file-lock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 danil-kashin/file-lock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 danil-kashin/file-lock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 52
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 43
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-03-26