{"id":21571158,"url":"https://github.com/cheprasov/php-redis-lock","last_synced_at":"2025-04-06T12:10:51.857Z","repository":{"id":60774280,"uuid":"63552131","full_name":"cheprasov/php-redis-lock","owner":"cheprasov","description":"RedisLock 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.","archived":false,"fork":false,"pushed_at":"2021-06-10T19:36:04.000Z","size":23,"stargazers_count":102,"open_issues_count":0,"forks_count":27,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-24T23:20:46.613Z","etag":null,"topics":["locks","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/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":"2016-07-17T20:56:44.000Z","updated_at":"2024-03-14T08:39:46.000Z","dependencies_parsed_at":"2022-10-04T15:36:57.907Z","dependency_job_id":null,"html_url":"https://github.com/cheprasov/php-redis-lock","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-redis-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-redis-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-redis-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-redis-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cheprasov","download_url":"https://codeload.github.com/cheprasov/php-redis-lock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247478324,"owners_count":20945266,"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":["locks","php","redis"],"created_at":"2024-11-24T11:15:05.002Z","updated_at":"2025-04-06T12:10:51.822Z","avatar_url":"https://github.com/cheprasov.png","language":"PHP","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-redis-lock/v/stable)](https://packagist.org/packages/cheprasov/php-redis-lock)\n[![Total Downloads](https://poser.pugx.org/cheprasov/php-redis-lock/downloads)](https://packagist.org/packages/cheprasov/php-redis-lock)\n\n# RedisLock v1.0.3 for PHP \u003e= 5.5\n\n## About\nRedisLock 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 [redis](http://redis.io/).\n\n\n## Usage\n\n### Create a new instance of RedisLock\n\n```php\n\u003c?php\nrequire 'vendor/autoload.php';\n\nuse RedisLock\\RedisLock;\nuse RedisClient\\ClientFactory;\nuse RedisClient\\RedisClient;\n\n// Create a new Redis instance\n$Redis = ClientFactory::create([\n    'server' =\u003e 'tcp://127.0.0.1:6379'\n]);\n\n$Lock = new RedisLock(\n    $Redis, // Instance of RedisClient,\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 RedisLock\\RedisLock;\nuse RedisClient\\ClientFactory;\nuse RedisClient\\RedisClient;\n\n// Create a new Redis instance\n$Redis = ClientFactory::create([\n    'server' =\u003e 'tcp://127.0.0.1:6379'\n]);\n\n// ...\n\n/**\n * Safe update json in Redis storage\n * @param Redis $Redis\n * @param string $key\n * @param array $array\n * @throws Exception\n */\nfunction updateJsonInRedis(RedisClient $Redis, $key, array $array) {\n    // Create new Lock instance\n    $Lock = new RedisLock($Redis, 'Lock_'.$key, RedisLock::FLAG_DO_NOT_THROW_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 = $Redis-\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    $Redis-\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\nupdateJsonInRedis($Redis, 'json-key', ['for' =\u003e 1, 'bar' =\u003e 2]);\nupdateJsonInRedis($Redis, 'json-key', ['for' =\u003e 42, 'var' =\u003e 2016]);\n\n```\n\n## Methods\n\n#### RedisLock :: __construct ( `RedisClient` **$Redis** , `string` **$key** [, `int` **$flags** = 0 ] )\n---\nCreate a new instance of RedisLock.\n\n##### Method Pameters\n\n1. RedisClient **$Redis** - Instanse of [RedisClient](https://github.com/cheprasov/php-redis-client)\n2. string **$key** - name of key in Redis storage. Only locks with the same name will compete with each other for lock.\n3. int **$flags**, default = 0\n   * `RedisLock::FLAG_DO_NOT_THROW_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\n##### Example\n\n```php\n$Lock = new RedisLock($Redis, 'lockName');\n// or\n$Lock = new RedisLock($Redis, 'lockName', RedisLock::FLAG_DO_NOT_THROW_EXCEPTIONS);\n\n```\n\n#### `bool` RedisLock :: 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_DO_NOT_THROW_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, the value must be `\u003e= 0.01`.\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 RedisLock($Redis, 'lockName');\n$Lock-\u003eacquire(3, 4);\n// ... do something\n$Lock-\u003erelease();\n```\n\n#### `bool` RedisLock :: 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 `RedisLock :: acquire`\n\n##### Example\n\n```php\n$Lock = new RedisLock($Redis, 'lockName');\n$Lock-\u003eacquire(3, 4);\n// ... do something\n$Lock-\u003eupdate(3);\n// ... do something\n$Lock-\u003erelease();\n```\n\n#### `bool` RedisLock :: isAcquired ( )\n---\nCheck this lock for acquired. Returns `true` on success or `false` on failure.\n\n#### `bool` RedisLock :: 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` RedisLock :: 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-redis-lock\n\n## Running tests\n\nTo run tests type in 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/cheprasov"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fphp-redis-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcheprasov%2Fphp-redis-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fphp-redis-lock/lists"}