https://github.com/phlib/mutex
PHP mutex handling in different ways
https://github.com/phlib/mutex
Last synced: 5 months ago
JSON representation
PHP mutex handling in different ways
- Host: GitHub
- URL: https://github.com/phlib/mutex
- Owner: phlib
- License: lgpl-3.0
- Created: 2014-07-11T14:30:59.000Z (almost 12 years ago)
- Default Branch: main
- Last Pushed: 2025-02-11T13:51:53.000Z (over 1 year ago)
- Last Synced: 2026-01-13T22:44:35.798Z (6 months ago)
- Language: PHP
- Size: 66.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# phlib/mutex
[](https://github.com/phlib/mutex/actions/workflows/code-checks.yml)
[](https://codecov.io/gh/phlib/mutex)
[](https://packagist.org/packages/phlib/mutex)
[](https://packagist.org/packages/phlib/mutex)

PHP mutex handling in different ways
## Install
Via Composer
``` bash
$ composer require phlib/mutex
```
## Usage
### MySQL
```php
$adapter = new \Phlib\Db\Adapter([
'host' => '127.0.0.1',
'username' => 'my-user',
'password' => 'my-pass'
]);
$mutex = new \Phlib\Mutex\MySQL('my-lock', $adapter);
if ($mutex->lock()) {
// Do some data manipulation while locked
$mutex->unlock();
}
```
### Helpers
**Get-Or-Create** provides a simple way to attempt retrieval of a value,
or create it using a mutex if it doesn't already exist
```php
$getClosure = function() {
// attempt to get a value, eg. from DB, cache, etc.
if (!$value) {
throw new \Phlib\Mutex\NotFoundException();
}
return $value;
};
$createClosure = function() {
// attempt to create a value and write eg. to DB, cache, etc.
return $value;
};
$value = \Phlib\Mutex\Helper::getOrCreate($mutex, $getClosure, $createClosure);
```
## License
This package is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .