PHPackages                             warmans/dlock - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. warmans/dlock

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

warmans/dlock
=============

Distributed locking library

0.0.3(12y ago)324PHPPHP &gt;=5.3.0

Since May 11Pushed 12y ago1 watchersCompare

[ Source](https://github.com/warmans/dlock)[ Packagist](https://packagist.org/packages/warmans/dlock)[ Docs](https://github.com/warmans/dlock)[ RSS](/packages/warmans-dlock/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (4)Used By (0)

Dlock
=====

[](#dlock)

[![Build Status](https://camo.githubusercontent.com/913611772fd25ba4b6e7daa9685b37c75171a1681da8e1a591f959799328e916/68747470733a2f2f7472617669732d63692e6f72672f7761726d616e732f646c6f636b2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/warmans/dlock) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/f3f115f342f3f867bc41031755fce54ea350273419c23cd6fb0c035effd20736/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7761726d616e732f646c6f636b2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/warmans/dlock/?branch=master) [![Code Coverage](https://camo.githubusercontent.com/0d36ac9a63fdac474410c0034b89714c3d20dc5f6558dad122be464bac4aea2c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7761726d616e732f646c6f636b2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/warmans/dlock/?branch=master)

Distributed locking library for PHP. This library allows a process to be locked across multiple servers using a cache server (Redis or Memcache).

The example use case is where you have a process that must happen e.g. hourly but cannot be duplicated such as an import. In this case all of your application servers can be setup with the import job but each hour only one (the one with the fastest system clock) will be able to acquire the lock so all the others will fail. If any server goes offline another will acquire the lock and imports will continue as normal.

Like most distributed systems care should be taken to maintain consistent system clocks across severs. If servers have clocks that are more inconsistent than the time it takes to run a job you may still duplicate jobs. To avid this you could store a globally unique identifier for a job somewhere (e.g. in a redis set) and simply check that a job hasn't already been done after locking (note: not before or you have a race condition).

Locking is achieved using atomic functions to avoid race conditions. To discourage the user creating their own race conditions there is no "isLocked" functionality. You must always attempt to create a lock to deterine the locked status.

- [Usage](#usage)
- [Options](#options)
- [Adapters](#adapters)
    - [Memcache](#memcache)
    - [Redis](#redis)
- [Running the tests](#running-the-tests)

Usage
-----

[](#usage)

Allow lock to handle locking/unlocking automatically using locked() method:

```
//raw memcache connection
$memcache = new \Memcache();
$memcache->connect('localhost');

//datastore adapter
$adapter = new \Dlock\Datastore\Memcache($memcache);

$lock = new \Dlock\Lock($adapter);
$lock->locked(function(){
    //do something
});
```

Alternatively manage it yourself:

```
//raw memcache connection
$memcache = new \Memcache();
$memcache->connect('localhost');

//datastore adapter
$adapter = new \Dlock\Datastore\Memcache($memcache);

$lock = new \Dlock\Lock($adapter);
if ($lock->acquire()) {
    //do something
    $lock->release();
} else {
   //handle lock failure
}
```

Finally if you want to wait for a lock to become available you can use a blocking lock:

```
//raw memcache connection
$memcache = new \Memcache();
$memcache->connect('localhost');

//datastore adapter
$adapter = new \Dlock\Datastore\Memcache($memcache);

$lock = new \Dlock\Lock($adapter);

//wait for up to 30 seconds for lock then return false
if ($lock->acquire(30)) {
    //do something
    $lock->release();
} else {
   //handle lock failure
}

//or use closure with 30 second block
$lock->locked(function(){
    //do something
}, 30);
```

Options
-------

[](#options)

```
$lock = new Lock([...], [...], $options);
```

The third optional argument of the Lock's constructor is an options array. The options are as follows:

OptionDefaultDescriptionds\_key\_prefixdlockPrefix used by cache key in datastoreAdapters
--------

[](#adapters)

Any class that implements the DatastoreInterface can be used as an adapter:

```
interface DatastoreInterface
{
    public function acquireLock($lockId);
    public function releaseLock($lockId);
}
```

Included implementations are as follows:

#### Memcache

[](#memcache)

```
//raw memcache connection
$memcache = new \Memcache();
$memcache->connect('localhost');

//configure adapter with localhost connction and an hour TTL on locks
$adapter = new \Dlock\Datastore\Memcache($memcache, 3600);
```

#### Redis

[](#redis)

```
//raw redis connection
$redis = new \Redis();
$redis->connect('localhost');

//configure adapter with localhost connction and an hour TTL on locks
$adapter = new \Dlock\Datastore\Redis($memcache, 3600);
```

Running the tests
-----------------

[](#running-the-tests)

Tests are split into two testcases; unit and integration. Integration tests require a running Redis and Memcache server on your localhost.

To run all from root directory:

```
phpunit

```

To run just unit tests:

```
phpunit --testsuite=unit

```

To run just integration tests:

```
phpunit --testsuite=integration

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

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 ~0 days

Total

3

Last Release

4390d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1299401?v=4)[SW](/maintainers/warmans)[@warmans](https://github.com/warmans)

---

Tags

lockinglockdistributed

### Embed Badge

![Health badge](/badges/warmans-dlock/health.svg)

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

###  Alternatives

[symfony/lock

Creates and manages locks, a mechanism to provide exclusive access to a shared resource

514127.6M459](/packages/symfony-lock)[pudongping/hyperf-wise-locksmith

A mutex library provider for the Hyperf framework, designed to enable serialized execution of PHP code in high-concurrency scenarios.

106.3k2](/packages/pudongping-hyperf-wise-locksmith)[workerman/gateway-worker-for-win

1711.1k2](/packages/workerman-gateway-worker-for-win)[zerkalica/semaphore

This library provides an api for semaphore acquire and release

1119.0k1](/packages/zerkalica-semaphore)[nabao/laravel-lock

高性能, 分布式, 并发抢占锁, 队列锁

271.5k](/packages/nabao-laravel-lock)

PHPackages © 2026

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