{"id":21309960,"url":"https://github.com/lysice/hyperf-redis-lock","last_synced_at":"2025-08-31T11:50:26.076Z","repository":{"id":41816485,"uuid":"418325925","full_name":"Lysice/hyperf-redis-lock","owner":"Lysice","description":"a distributed-redis-lock implementation for hyperf2.*","archived":false,"fork":false,"pushed_at":"2025-08-15T02:26:34.000Z","size":20,"stargazers_count":20,"open_issues_count":5,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-15T04:19:45.652Z","etag":null,"topics":["distributed-lock","hyperf","lock","redis","redis-lock"],"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/Lysice.png","metadata":{"files":{"readme":"README-zh.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-18T03:07:54.000Z","updated_at":"2025-08-15T02:26:38.000Z","dependencies_parsed_at":"2024-06-19T04:01:45.003Z","dependency_job_id":"0dbed88e-1898-4561-ab1d-9ef660c2da3f","html_url":"https://github.com/Lysice/hyperf-redis-lock","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"41f12b44f7e7881aadf8ddd337a56f6fa12a5504"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/Lysice/hyperf-redis-lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysice%2Fhyperf-redis-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysice%2Fhyperf-redis-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysice%2Fhyperf-redis-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysice%2Fhyperf-redis-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lysice","download_url":"https://codeload.github.com/Lysice/hyperf-redis-lock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysice%2Fhyperf-redis-lock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272976983,"owners_count":25025043,"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-31T02:00:09.071Z","response_time":79,"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":["distributed-lock","hyperf","lock","redis","redis-lock"],"created_at":"2024-11-21T17:11:39.539Z","updated_at":"2025-08-31T11:50:26.013Z","avatar_url":"https://github.com/Lysice.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hyperf-redis-lock\n[![Latest Stable Version](https://poser.pugx.org/Lysice/hyperf-redis-lock/v/stable)](https://packagist.org/packages/Lysice/hyperf-redis-lock)\n[![Total Downloads](https://poser.pugx.org/Lysice/hyperf-redis-lock/downloads)](https://packagist.org/packages/Lysice/hyperf-redis-lock)\n[![Latest Unstable Version](https://poser.pugx.org/Lysice/hyperf-redis-lock/v/unstable)](https://packagist.org/packages/Lysice/hyperf-redis-lock)\n[![License](https://poser.pugx.org/Lysice/hyperf-redis-lock/license)](https://packagist.org/packages/Lysice/hyperf-redis-lock)\n[English](./README.md) | 中文\n\n一个简单的Redis分布式锁的实现 基于Hyperf框架。本扩展实现了基本的分布式锁，支持阻塞式分布式锁和非阻塞式分布式锁。\n\n## 原理\n`Redis`的命令为原子性 使用`Redis`的`set`即可保证业务的串行执行。\n`2.8`之前版本的`Redis`不支持`set` 的`ex`选项 因此只能使用 `setnx+expire`的方式。 对应扩展1.0版本.\n`2.8`之后版本的`Redis`可以直接使用 `set` 的`nx+ex`选项。对应扩展2.*版本\n\n因此 2.*版本只支持2.8版本以后的`Redis` 1.*版本支持所有版本的`Redis`.\n\n## 确认你的Redis版本 如果你的Redis低于 2.8版本 则set 命令不支持 ex选项 因此你需要安装1.0版本。\n`redis-server --version`\n\n## 安装 \n注意:请根据你的`Redis`版本\n执行 `composer require lysice/hyperf-redis-lock`\n\n## 使用\n首先需要在程序内初始化你需要的`redis`\n```\n/**\n     * @var RedisLock\n     */\n    protected $redis;\n\n    public function __construct(RedisFactory $redisFactory)\n    {\n        $this-\u003eredis = $redisFactory-\u003eget('default');\n    }\n```\n\n- 非阻塞式锁 该方法在尝试获取锁之后直接返回结果。若获取到锁则执行闭包后返回结果。否则返回false\n```\npublic function lock(ResponseInterface $response)\n    {\n        // 初始化RedisLock 参数:redis实例 锁名称 超时时间\n        $lock = new RedisLock($this-\u003eredis, 'lock', 20);\n        // 非阻塞式获取锁\n        $res = $lock-\u003eget(function () {\n            sleep(10);\n            return [123];\n        });\n        return $response-\u003ejson($res);\n    }\n```\n- 阻塞式锁 该方法首先尝试获取锁，若获取失败 则每隔250毫秒获取一次 直到超时(等待时间超出本程序内锁的过期时间 则判定为超时)。如果锁获取成功 则执行闭包函数返回结果。\n注意 若超时 则程序会抛出`LockTimeoutException`超时异常。应用程序内需要自己捕获该异常以便处理超时情况的返回结果。\n例子:\n```\n/**\n     * @return \\Psr\\Http\\Message\\ResponseInterface\n     */\n    public function lockA(ResponseInterface $response)\n    {\n        try {\n            // 初始化RedisLock 参数:redis实例 锁名称 超时时间\n            $lock = new RedisLock($this-\u003eredis, 'lock', 4);\n            // 阻塞式\n            $res = $lock-\u003eblock(4, function () {\n                return [456];\n            });\n            return $response-\u003ejson(['res' =\u003e $res]);\n        // 捕获超时异常 超时处理\n        } catch (LockTimeoutException $exception) {\n            var_dump('lockA lock check timeout');\n            return $response-\u003ejson(['res' =\u003e false, 'message' =\u003e '超时']);\n        }\n    }\n```\n\n\n## 最后\n\n#### 代码贡献\n如果存在任何好的想法请提交pull request\nFeel free to create a fork and submit a pull request if you would like to contribute.\n\n#### 代码问题\n如果存在任何问题请提交issue.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flysice%2Fhyperf-redis-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flysice%2Fhyperf-redis-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flysice%2Fhyperf-redis-lock/lists"}