{"id":26422593,"url":"https://github.com/arvenil/mutex","last_synced_at":"2025-04-09T05:08:10.312Z","repository":{"id":4311915,"uuid":"5444876","full_name":"arvenil/mutex","owner":"arvenil","description":"Mutex implementation for PHP","archived":false,"fork":false,"pushed_at":"2022-08-09T22:09:00.000Z","size":271,"stargazers_count":189,"open_issues_count":6,"forks_count":31,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-02T03:17:45.631Z","etag":null,"topics":["flock","lock","memcache","memcached","mutex","mysql","php","redis"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arvenil.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"arvenil"}},"created_at":"2012-08-16T21:37:40.000Z","updated_at":"2024-05-15T10:47:27.000Z","dependencies_parsed_at":"2022-09-02T16:28:39.158Z","dependency_job_id":null,"html_url":"https://github.com/arvenil/mutex","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvenil%2Fmutex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvenil%2Fmutex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvenil%2Fmutex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arvenil%2Fmutex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arvenil","download_url":"https://codeload.github.com/arvenil/mutex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247980837,"owners_count":21027808,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flock","lock","memcache","memcached","mutex","mysql","php","redis"],"created_at":"2025-03-18T02:26:02.403Z","updated_at":"2025-04-09T05:08:10.292Z","avatar_url":"https://github.com/arvenil.png","language":"PHP","readme":"[![License](https://img.shields.io/github/license/arvenil/ninja-mutex?color=informational)](http://opensource.org/licenses/MIT)\n[![PHP](https://img.shields.io/packagist/php-v/arvenil/ninja-mutex)](https://packagist.org/packages/arvenil/ninja-mutex)\n[![Version](https://img.shields.io/github/v/release/arvenil/ninja-mutex)](https://github.com/arvenil/ninja-mutex/releases/latest)\n\n[![Build](https://github.com/arvenil/ninja-mutex/workflows/PHP/badge.svg)](https://github.com/arvenil/ninja-mutex/actions?query=workflow%3APHP)\n\n[![Coverage](https://img.shields.io/scrutinizer/coverage/g/arvenil/ninja-mutex?color=success)](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master)\n[![Quality](https://img.shields.io/scrutinizer/quality/g/arvenil/ninja-mutex?color=success\u0026label=quality)](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master)\n[![Maintainability](https://img.shields.io/codeclimate/maintainability-percentage/arvenil/ninja-mutex?color=success)](https://codeclimate.com/github/arvenil/ninja-mutex)\n[![Grade](https://img.shields.io/symfony/i/grade/15c5c748-f8d8-4b56-b536-a29a151aac6c?color=success)](https://insight.symfony.com/projects/15c5c748-f8d8-4b56-b536-a29a151aac6c)\n[![Total Downloads](https://img.shields.io/packagist/dt/arvenil/ninja-mutex.svg)](https://packagist.org/packages/arvenil/ninja-mutex)\n\n## About\n\nninja-mutex is a simple to use mutex implementation for php. It supports different adapters (flock, memcache, mysql, redis, ...) so you can set it up as you wish. All adapters (if set up properly) can be used in multi server environment - in other words lock is shared between web servers.\n\n## Usage\n\n### Mutex\n\nFirst you need to choose an adapter and setup it properly. For example if you choose flock implementation first you need to set up NFS filesystem and mount it on web servers. In this example we will choose memcache adapter:\n\n```php\n\u003c?php\nrequire 'vendor/autoload.php';\n\nuse NinjaMutex\\Lock\\MemcacheLock;\nuse NinjaMutex\\Mutex;\n\n$memcache = new Memcache();\n$memcache-\u003econnect('127.0.0.1', 11211);\n$lock = new MemcacheLock($memcache);\n$mutex = new Mutex('very-critical-stuff', $lock);\nif ($mutex-\u003eacquireLock(1000)) {\n    // Do some very critical stuff\n\n    // and release lock after you finish\n    $mutex-\u003ereleaseLock();\n} else {\n    throw new Exception('Unable to gain lock!');\n}\n```\n\n### Mutex Fabric\n\nIf you want to use multiple mutexes in your project then MutexFabric is the right solution. Set up lock implementor once, and you can use as many mutexes as you want!\n\n```php\n\u003c?php\nrequire 'vendor/autoload.php';\n\nuse NinjaMutex\\Lock\\MemcacheLock;\nuse NinjaMutex\\MutexFabric;\n\n$memcache = new Memcache();\n$memcache-\u003econnect('127.0.0.1', 11211);\n$lock = new MemcacheLock($memcache);\n$mutexFabric = new MutexFabric('memcache', $lock);\nif ($mutexFabric-\u003eget('very-critical-stuff')-\u003eacquireLock(1000)) {\n    // Do some very critical stuff\n\n    // and release lock after you finish\n    $mutexFabric-\u003eget('very-critical-stuff')-\u003ereleaseLock();\n} else {\n    throw new Exception('Unable to gain lock for very critical stuff!');\n}\n\nif ($mutexFabric-\u003eget('also-very-critical-stuff')-\u003eacquireLock(0)) {\n    // Do some also very critical stuff\n\n    // and release lock after you finish\n    $mutexFabric-\u003eget('also-very-critical-stuff')-\u003ereleaseLock();\n} else {\n    throw new Exception('Unable to gain lock for also very critical stuff!');\n}\n```\n\n## Installation\n\n### Composer\n\nDownload composer:\n\n    wget -nc http://getcomposer.org/composer.phar\n\nAdd dependency to your project:\n\n    php composer.phar require arvenil/ninja-mutex:*\n\n## Running tests\n\nTests require vfsStream to work. To install it, simply run in project dir:\n\n    wget -nc http://getcomposer.org/composer.phar \u0026\u0026 php composer.phar install --dev\n\nTo run tests type in a console:\n\n    vendor/bin/phpunit\n\n## Something doesn't work\n\nFeel free to fork project, fix bugs and finally request for pull\n","funding_links":["https://github.com/sponsors/arvenil"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farvenil%2Fmutex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farvenil%2Fmutex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farvenil%2Fmutex/lists"}