Search by

michel / lock

F.Michel

A flexible PHP locking library designed to prevent race conditions in CLI and web environments.

Package info

github.com/michelphp/lock

pkg:composer/michel/lock

Statistics

Installs: 11

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.1 2026-10-28 09:24 UTC

This package is auto-updated.

Last update: 2026-10-28 12:47:15 UTC


README

One process at a time.

Two cron runs, two workers, two requests: only one does the job. Lock a key, do the work, unlock. Plain PHP file locks by default, your own backend when you need it.

PHP 7.4+ 路 No dependency 路 CLI and web

馃摉 Documentation 路 Website

Install

composer require michel/lock

Quick start

use Michel\Lock\Locker;
use Michel\Lock\Handler\FlockHandler;

$locker = new Locker(new FlockHandler());

if (!$locker->lock('nightly_import')) {
    echo "Already running.\n";
    exit(0);
}

runImport(); // only one process at a time runs this

$locker->unlock('nightly_import');

Wait for the lock instead of giving up:

$locker->lock('counter', true); // blocks until the lock is free

Why Lock

No dependency Pure PHP 7.4+. No server, no extension: the default handler uses flock().
Never stuck Locks are released by unlock(), when the Locker is destroyed, and at shutdown with unlockIfKill(). If the process is killed, the system releases the file lock.
Any backend Implement LockHandlerInterface (two methods) to share a lock between servers, on your database or Redis.
Safe keys Keys accept letters, digits, _ and . only: a key can never become a path.

What Lock will never do

add a dependency 路 require a server 路 retry on a timer 路 expire a lock behind your back 路 accept a key that is a path

Documentation

The documentation covers keys, blocking and non-blocking locks, releasing, FlockHandler, writing your own handler and errors.

License

MPL-2.0