{"id":15296341,"url":"https://github.com/parsadp/laravel-redlock","last_synced_at":"2025-10-07T08:31:08.252Z","repository":{"id":56947091,"uuid":"146263685","full_name":"parsadp/laravel-redlock","owner":"parsadp","description":"Redis distributed locks for Laravel 5.6","archived":false,"fork":true,"pushed_at":"2020-10-10T16:26:06.000Z","size":80,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-16T18:01:10.918Z","etag":null,"topics":["laravel","laravel56","php","php71","redlock","redlock-redis"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"thatsus/laravel-redlock","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/parsadp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-27T07:41:17.000Z","updated_at":"2020-10-10T16:25:26.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/parsadp/laravel-redlock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parsadp%2Flaravel-redlock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parsadp%2Flaravel-redlock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parsadp%2Flaravel-redlock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parsadp%2Flaravel-redlock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parsadp","download_url":"https://codeload.github.com/parsadp/laravel-redlock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235607123,"owners_count":19017298,"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":["laravel","laravel56","php","php71","redlock","redlock-redis"],"created_at":"2024-09-30T18:10:10.263Z","updated_at":"2025-10-07T08:31:02.931Z","avatar_url":"https://github.com/parsadp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel RedLock\n\nProvides a generic locking mechanism using Redis. Implements the locking standard proposed by Redis.\n\n\n\n### Acknowledgements\n\nThis library was originally built by LibiChai based on the Redlock algorithm developed by antirez. The library was reworked by the team at That's Us, Inc.\n\n### Installation\n\n1. `\"parsadp/laravel-redlock\": \"^4.0.0\"`\n2. `\"composer update\"`\n3. Add `Amiralii\\RedLock\\RedLockServiceProvider::class,` to the `providers` array in config/app.php\n4. Enjoy!\n\n\n### It's Simple!\n\nSet a lock on any scalar. If the `lock()` method returns false, you did not acquire the lock.\n\nStore results of the `lock()` method. Use this value to release the lock with `unlock()`.\n\n### Example\n\nThis example sets a lock on the key \"1\" with a 3 second expiration time.\n\nIf it acquired the lock, it does some work and releases the lock.\n\n```php \n use Amiralii\\RedLock\\Facades\\RedLock;\n\n $product_id = 1;\n\n $lock_token = RedLock::lock($product_id, 3000);\n \n if ($lock_token) {\n\n     $order-\u003esubmit($product_id);\n\n     RedLock::unlock($lock_token);\n }\n```\n\n### Refresh\n\nUse `refreshLock()` to reacquire and extend the time of your lock.\n\n```php \n use Amiralii\\RedLock\\Facades\\RedLock;\n\n $product_ids = [1, 2, 3, 5, 7];\n\n $lock_token = RedLock::lock('order-submitter', 3000);\n \n while ($product_ids \u0026\u0026 $lock_token) {\n\n     $order-\u003esubmit(array_shift($product_ids));\n\n     $lock_token = RedLock::refreshLock($lock_token);\n }\n\n RedLock::unlock($lock_token);\n```\n\n### Even Easier with Closures\n\nUse `runLocked()` for nicer syntax. The method returns the results of the closure, or else false if the lock could not be acquired.\n\n```php\n use Amiralii\\RedLock\\Facades\\RedLock;\n\n $product_id = 1;\n\n $result = RedLock::runLocked($product_id, 3000, function () use ($order, $product_id) {\n     $order-\u003esubmit($product_id);\n     return true;\n });\n\n echo $result ? 'Worked!' : 'Lock not acquired.';\n```\n\n### Refresh with Closures\n\nYou can easily refresh your tokens when using closures. The first parameter to your closure is `$refresh`. Simply call it when you want to refresh. If the lock cannot be refreshed, `$refresh()` will break out of the closure.\n\n```php\n use Amiralii\\RedLock\\Facades\\RedLock;\n\n $product_ids = [1, 2, 3, 5, 7];\n\n $result = RedLock::runLocked($product_id, 3000, function ($refresh) use ($order, $product_ids) {\n     foreach ($product_ids as $product_id) {\n         $refresh();\n         $order-\u003esubmit($product_id);\n     }\n     return true;\n });\n\n echo $result ? 'Worked!' : 'Lock lost or never acquired.';\n```\n\n### Lock Queue Jobs Easily\n\nIf you're running jobs on a Laravel queue, you may want to avoid queuing up the same job more than once at a time.\n\nThe `Amiralii\\RedLock\\Traits\\QueueWithoutOverlap` trait provides this functionality with very few changes to your job. Usually only two changes are necessary.\n\n1. `use Amiralii\\RedLock\\Traits\\QueueWithoutOverlap` as a trait\n2. Change the `handle()` method to `handleSync()`\n\n```php\nuse Amiralii\\RedLock\\Traits\\QueueWithoutOverlap;\n\nclass OrderProductJob\n{\n    use QueueWithoutOverlap;\n\n    public function __construct($order, $product_id)\n    {\n        $this-\u003eorder = $order;\n        $this-\u003eproduct_id = $product_id;\n    }\n\n    public function handleSync()\n    {\n        $this-\u003eorder-\u003esubmit($this-\u003eproduct_id);\n    }\n\n}\n```\n\nSometimes it's also necessary to specify a `getLockKey()` method. This method must return the string that needs to be locked.\n\nThis is typically unnecessary because the lock key can be generated automatically. But if the job's data is not easy to stringify, you must define the `getLockKey()` method.\n\nThis trait also provides a refresh method called `refreshLock()`. If `refreshLock()` is unable to refresh the lock, an exception is thrown and the job fails.\n\nFinally, you can change the lock time-to-live from the default 300 seconds to another\nvalue using the `$lock_time` property.\n\n```php\nuse Amiralii\\RedLock\\Traits\\QueueWithoutOverlap;\n\nclass OrderProductsJob\n{\n    use QueueWithoutOverlap;\n\n    protected $lock_time = 600; // 10 minutes in seconds\n\n    public function __construct($order, array $product_ids)\n    {\n        $this-\u003eorder = $order;\n        $this-\u003eproduct_ids = $product_ids;\n    }\n\n    // We need to define getLockKey() because $product_ids is an array and the\n    // automatic key generator can't deal with arrays.\n    protected function getLockKey()\n    {\n        $product_ids = implode(',', $this-\u003eproduct_ids);\n        return \"OrderProductsJob:{$this-\u003eorder-\u003eid}:{$product_ids}\";\n    }\n\n    public function handleSync()\n    {\n        foreach ($this-\u003eproduct_ids as $product_id) {\n            $this-\u003erefreshLock();\n            $this-\u003eorder-\u003esubmit($product_id);\n        }\n    }\n\n}\n```\n\n### Contribution\n\nIf you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/amiralii/laravel-redlock/issues) or a [pull request](https://github.com/amiralii/laravel-redlock/pulls).\n\n### License\n\n[MIT](http://opensource.org/licenses/MIT)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparsadp%2Flaravel-redlock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparsadp%2Flaravel-redlock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparsadp%2Flaravel-redlock/lists"}