Download the PHP package warmans/dlock without Composer
On this page you can find all versions of the php package warmans/dlock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download warmans/dlock
More information about warmans/dlock
Files in warmans/dlock
Package dlock
Short Description Distributed locking library
License
Homepage https://github.com/warmans/dlock
Informations about the package dlock
Dlock
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
- Options
- Adapters
- Memcache
- Redis
- Running the tests
Usage
Allow lock to handle locking/unlocking automatically using locked() method:
Alternatively manage it yourself:
Finally if you want to wait for a lock to become available you can use a blocking lock:
Options
The third optional argument of the Lock's constructor is an options array. The options are as follows:
Option | Default | Description |
---|---|---|
ds_key_prefix | dlock | Prefix used by cache key in datastore |
Adapters
Any class that implements the DatastoreInterface can be used as an adapter:
Included implementations are as follows:
Memcache
Redis
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