{"id":21571153,"url":"https://github.com/cheprasov/php-memcached-lock","last_synced_at":"2025-08-08T02:40:47.705Z","repository":{"id":60774281,"uuid":"45070975","full_name":"cheprasov/php-memcached-lock","owner":"cheprasov","description":"MemcachedLock for PHP is a synchronization mechanism based on Memcached for enforcing limits on access to a resource in an environment where there are many threads of execution.","archived":false,"fork":false,"pushed_at":"2021-06-10T19:44:20.000Z","size":33,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-17T19:32:08.828Z","etag":null,"topics":["locks","memcached","php"],"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/cheprasov.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":"cheprasov","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2015-10-27T21:23:55.000Z","updated_at":"2021-06-10T19:44:22.000Z","dependencies_parsed_at":"2022-10-04T15:32:05.488Z","dependency_job_id":null,"html_url":"https://github.com/cheprasov/php-memcached-lock","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/cheprasov/php-memcached-lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cheprasov","download_url":"https://codeload.github.com/cheprasov/php-memcached-lock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-memcached-lock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269355393,"owners_count":24403432,"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","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["locks","memcached","php"],"created_at":"2024-11-24T11:15:02.278Z","updated_at":"2025-08-08T02:40:47.645Z","avatar_url":"https://github.com/cheprasov.png","language":"PHP","funding_links":["https://github.com/sponsors/cheprasov"],"categories":[],"sub_categories":[],"readme":"[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n[![Latest Stable Version](https://poser.pugx.org/cheprasov/php-memcached-lock/v/stable)](https://packagist.org/packages/cheprasov/php-memcached-lock)\n[![Total Downloads](https://poser.pugx.org/cheprasov/php-memcached-lock/downloads)](https://packagist.org/packages/cheprasov/php-memcached-lock)\n\n# MemcachedLock v1.0.5 for PHP \u003e= 5.5\n\n## About\nMemcachedLock for PHP is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many threads of execution. A lock is designed to enforce a mutual exclusion concurrency control policy. Based on [Memcached](http://php.net/manual/en/book.memcached.php).\n\n## Usage\n\n### Create a new instance of MemcachedLock\n\n```php\n\u003c?php\nrequire 'vendor/autoload.php';\n\nuse MemcachedLock\\MemcachedLock;\n\n$Memcached = new \\Memcached();\n$Memcached-\u003eaddServer('127.0.0.1', '11211');\n\n$Lock = new MemcachedLock(\n    $Memcached, // Instance of Memcached,\n    'key', // Key in storage,\n);\n```\n\n### Usage for lock a process\n\n```php\n\u003c?php\nrequire 'vendor/autoload.php';\n\nuse MemcachedLock\\MemcachedLock;\n\n// Create a new Memcached instance\n$Memcached = new \\Memcached();\n$Memcached-\u003eaddServer('127.0.0.1', '11211');\n\n//...\n\n/**\n * Safe update json in Memcached storage\n * @param Memcached $Memcached\n * @param string $key\n * @param array $array\n * @throws Exception\n */\nfunction updateJsonInMemcached(\\Memcached $Memcached, $key, array $array) {\n    // Create new Lock instance\n    $Lock = new MemcachedLock($Memcached, 'Lock_'.$key, MemcachedLock::FLAG_CATCH_EXCEPTIONS);\n\n    // Acquire lock for 2 sec.\n    // If lock has acquired in another thread then we will wait 3 second,\n    // until another thread release the lock. Otherwise it throws a exception.\n    if (!$Lock-\u003eacquire(2, 3)) {\n        throw new Exception('Can\\'t get a Lock');\n    }\n\n    // Get value from storage\n    $json = $Memcached-\u003eget($key);\n    if (!$json) {\n        $jsonArray = [];\n    } else {\n        $jsonArray = json_decode($json, true);\n    }\n\n    // Some operations with json\n    $jsonArray = array_merge($jsonArray, $array);\n\n    $json = json_encode($jsonArray);\n    // Update key in storage\n    $Memcached-\u003eset($key, $json);\n\n    // Release the lock\n    // After $lock-\u003erelease() another waiting thread (Lock) will be able to update json in storage\n    $Lock-\u003erelease();\n}\n\n```\n\n## Methods\n\n#### MemcachedLock :: __construct ( `\\Memcached` **$Memcached** , `string` **$key** [, `int` **$flags** = 0 ] )\n---\nCreate a new instance of MemcachedLock.\n\n##### Method Pameters\n\n1. \\Memcached **$Memcached** - Instanse of [Memcached](http://php.net/manual/en/book.memcached.php) \n2. string **$key** - name of key in Memcached storage. Only locks with the same name will compete with each other for lock.\n3. int **$flags**, default = 0\n   * `MemcachedLock::FLAG_CATCH_EXCEPTIONS` - use this flag, if you don't want catch exceptions by yourself. Do not use this flag, if you want have a full control on situation with locks. Default behavior without this flag - all Exceptions will be thrown.\n   * `MemcachedLock::FLAG_USE_SELF_EXPIRE_SYNC` - without this flag, MemcachedLock uses Memcached for sync of expire time of locks. It is very useful if you have multiple servers that are not synchronized in time. It is recommended to use this flag when lock works on single server. Can be used for multiple servers on a strict time synchronisation of multiple servers.\n\n##### Example\n\n```php\n$Lock = new MemcachedLock($Memcached, 'lockName');\n// or\n$Lock = new MemcachedLock($Memcached, 'lockName', MemcachedLock::FLAG_USE_SELF_EXPIRE_SYNC);\n// or\n$Lock = new MemcachedLock($Memcached, 'lockName',\n    MemcachedLock::FLAG_CATCH_EXCEPTIONS | MemcachedLock::FLAG_USE_SELF_EXPIRE_SYNC\n);\n\n```\n\n#### `bool` MemcachedLock :: acquire ( `int|float` **$lockTime** , [ `float` **$waitTime** = 0 [, `float` **$sleep** = 0.005 ] ] )\n---\nTry to acquire lock for `$lockTime` seconds.\nIf lock has acquired in another thread then we will wait `$waitTime` seconds, until another thread release the lock.\nOtherwise method throws a exception (if `FLAG_CATCH_EXCEPTIONS` is not set) or result.\nReturns `true` on success or `false` on failure.\n\n##### Method Pameters\n\n1. int|float **$lockTime** - The time for lock in seconds. By default, the value is cast to a integer with round fractions up (`(int) ceil($lockTime)`). The value must be `\u003e= 1`. **Note**, because of the behavior of the Memcached, the lock can be acquired less than a specified time about from 0.1 to 0.8 seconds. Otherwise, if  flag `FLAG_USE_SELF_EXPIRE_SYNC` is set, the value must be `\u003e= 0.01`, in this case we have not any inaccuracies with time. \n2. float **$waitTime**, default = 0 - The time for waiting lock in seconds. Use `0` if you don't wait until lock release.\n3. float **$sleep**, default = 0.005 - The wait time between iterations to check the availability of the lock.\n\n##### Example\n\n```php\n$Lock = new MemcachedLock($Memcached, 'lockName');\n$Lock-\u003eacquire(3, 4);\n// ... do something\n$Lock-\u003erelease();\n```\n\n#### `bool` MemcachedLock :: update ( `int|float` **$lockTime** )\n---\nSet a new time for lock if it is acquired already. Returns `true` on success or `false` on failure. Method can throw Exceptions.\n\n##### Method Pameters\n1. int|float **$lockTime** - Please, see description for method `MemcachedLock :: acquire`\n\n##### Example\n\n```php\n$Lock = new MemcachedLock($Memcached, 'lockName');\n$Lock-\u003eacquire(3, 4);\n// ... do something\n$Lock-\u003eupdate(3);\n// ... do something\n$Lock-\u003erelease();\n```\n\n#### `bool` MemcachedLock :: isAcquired ( )\n---\nCheck this lock for acquired. Returns `true` on success or `false` on failure.\n\n#### `bool` MemcachedLock :: isLocked ( )\n---\nCheck this lock for acquired and not expired, and active yet. Returns `true` on success or `false` on failure. Method can throw Exceptions.\n\n#### `bool` MemcachedLock :: isExists ()\n---\nDoes lock exists or acquired anywhere? Returns `true` if lock is exists or `false` if is not.\n\n## Installation\n\n### Composer\n\nDownload composer:\n\n    wget -nc http://getcomposer.org/composer.phar\n\nand add dependency to your project:\n\n    php composer.phar require cheprasov/php-memcached-lock\n\n## Running tests\n\nTo run tests type in console:\n\n    ./vendor/bin/phpunit ./test/\n\n## Something doesn't work\n\nFeel free to fork project, fix bugs and finally request for pull\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fphp-memcached-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcheprasov%2Fphp-memcached-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fphp-memcached-lock/lists"}