{"id":24785,"date":"2024-11-21T10:58:32","date_gmt":"2024-11-21T02:58:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24785\/"},"modified":"2024-11-21T10:58:32","modified_gmt":"2024-11-21T02:58:32","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8c%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24785\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5206\u5e03\u5f0f\u9501\u7ba1\u7406"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169845706965483.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5206\u5e03\u5f0f\u9501\u7ba1\u7406\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5206\u5e03\u5f0f\u9501\u7ba1\u7406\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5206\u5e03\u5f0f\u9501\u7ba1\u7406<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u7531\u4e8e\u591a\u4e2a\u8282\u70b9\u540c\u65f6\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u4f1a\u51fa\u73b0\u591a\u4e2a\u8282\u70b9\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u60c5\u51b5\uff0c\u8fdb\u800c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6027\u3001\u810f\u8bfb\u7b49\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e38\u5e38\u9700\u8981\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u673a\u5236\u6765\u4fdd\u8bc1\u8d44\u6e90\u7684\u72ec\u5360\u6027\u3002Hyperf\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u5206\u5e03\u5f0f\u9501\u3002<\/p>\n<p>\u4e00\u3001Hyperf\u6846\u67b6\u7b80\u4ecb<br \/>Hyperf\u662f\u4e00\u4e2a\u57fa\u4e8ePHP\u534f\u7a0b\u7684\u9ad8\u6027\u80fd\uff0c\u7075\u6d3b\u7684\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u6784\u5efa\u6570\u636e\u9a71\u52a8\u7684\u5e94\u7528\u3002\u5b83\u5177\u6709\u4f4e\u95e8\u69db\u3001\u7075\u6d3b\u7684\u4f9d\u8d56\u6ce8\u5165\u3001\u5f3a\u5927\u7684IoC\u5bb9\u5668\u3001\u9ad8\u6027\u80fd\u3001\u6807\u51c6\u7ec4\u4ef6\u4e30\u5bcc\u7b49\u7279\u70b9\u3002<\/p>\n<p>\u4e8c\u3001\u5206\u5e03\u5f0f\u9501\u539f\u7406<br \/>\u5206\u5e03\u5f0f\u9501\u901a\u5e38\u6709\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a\u57fa\u4e8e\u6570\u636e\u5e93\u548c\u57fa\u4e8e\u7f13\u5b58\u3002\u57fa\u4e8e\u6570\u636e\u5e93\u7684\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\u8f83\u4e3a\u7b80\u5355\uff0c\u4f46\u6027\u80fd\u8f83\u4f4e\u3002\u800c\u57fa\u4e8e\u7f13\u5b58\u7684\u5206\u5e03\u5f0f\u9501\u5219\u901a\u5e38\u4f7f\u7528Redis\u6216\u8005Memcached\u7b49\u9ad8\u6027\u80fd\u7f13\u5b58\u670d\u52a1\u6765\u5b9e\u73b0\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4e09\u3001Hyperf\u6846\u67b6\u96c6\u6210Redis<\/p>\n<ol>\n<li>\u5b89\u88c5Redis\u6269\u5c55<\/li>\n<\/ol>\n<p>\u5728PHP\u73af\u5883\u4e2d\u4f7f\u7528Redis\u6269\u5c55\u9700\u8981\u5148\u5b89\u88c5Redid\u76f8\u5173\u6269\u5c55\u3002<\/p>\n<pre>pecl install redis<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u6dfb\u52a0Redis\u914d\u7f6e<\/li>\n<\/ol>\n<p>\u5728Hyperf\u9879\u76ee\u7684\u914d\u7f6e\u6587\u4ef6config\/autoload\/redis.php\u4e2d\u6dfb\u52a0Redis\u7684\u8fde\u63a5\u53c2\u6570\uff1a<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nreturn [\n    'default' =&gt; [\n        'host' =&amp;gt; env('REDIS_HOST', '127.0.0.1'),\n        'auth' =&amp;gt; env('REDIS_AUTH', null),\n        'port' =&amp;gt; (int) env('REDIS_PORT', 6379),\n        'db' =&amp;gt; (int) env('REDIS_DB', 0),\n        'pool' =&amp;gt; [\n            'max_connections' =&amp;gt; (int) env('REDIS_MAX_CONNECTIONS', 10),\n            'min_connections' =&amp;gt; (int) env('REDIS_MIN_CONNECTIONS', 1),\n            'connect_timeout' =&amp;gt; (float) env('REDIS_CONNECT_TIMEOUT', 1.0),\n            'wait_timeout' =&amp;gt; (float) env('REDIS_WAIT_TIMEOUT', 3.0),\n            'heartbeat' =&amp;gt; (int) env('REDIS_HEARTBEAT', -1),\n            'max_idle_time' =&amp;gt; (float) env('REDIS_MAX_IDLE_TIME', 60),\n        ],\n    ],\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u914d\u7f6eRedis\u8fde\u63a5\u4fe1\u606f<\/li>\n<\/ol>\n<p>\u5728\u6839\u76ee\u5f55\u4e0b\u7684.env\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0bRedis\u8fde\u63a5\u4fe1\u606f\uff0c\u6ce8\u610f\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\u53c2\u6570\uff1a<\/p>\n<pre>REDIS_HOST=127.0.0.1\nREDIS_PORT=6379\nREDIS_DB=0<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56db\u3001\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5206\u5e03\u5f0f\u9501<\/p>\n<ol>\n<li>\u521b\u5efa\u9501\u670d\u52a1\u7c7b<\/li>\n<\/ol>\n<p>\u5728Hyperf\u7684app\/Utils\u76ee\u5f55\u4e0b\u521b\u5efaLockService.php\u6587\u4ef6\uff0c\u7528\u4e8e\u5c01\u88c5\u5206\u5e03\u5f0f\u9501\u76f8\u5173\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nnamespace AppUtils;\n\nuse HyperfRedisRedisFactory;\nuse HyperfUtilsApplicationContext;\nuse RedisException;\n\nclass LockService\n{\n    \/**\n     * \u83b7\u53d6\u9501\n     * @param string $key \u9501\u7684key\n     * @param int $expire \u8fc7\u671f\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\n     * @return bool\n     *\/\n    public function lock(string $key, int $expire): bool\n    {\n        $redis = $this-&gt;getRedis();\n        try {\n            return $redis-&amp;gt;set($key, 1, ['nx', 'ex' =&amp;gt; $expire]) ? true : false;\n        } catch (RedisException $exception) {\n            return false;\n        }\n    }\n\n    \/**\n     * \u89e3\u9501\n     * @param string $key \u9501\u7684key\n     * @return bool\n     *\/\n    public function unlock(string $key): bool\n    {\n        $redis = $this-&amp;gt;getRedis();\n        try {\n            return $redis-&amp;gt;del([$key]) &amp;gt; 0;\n        } catch (RedisException $exception) {\n            return false;\n        }\n    }\n\n    \/**\n     * \u83b7\u53d6Redis\u5b9e\u4f8b\n     * @return mixed\n     *\/\n    private function getRedis()\n    {\n        $container = ApplicationContext::getContainer();\n        return $container-&amp;gt;get(RedisFactory::class)-&amp;gt;get('default');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u4f7f\u7528\u9501\u670d\u52a1\u7c7b<\/li>\n<\/ol>\n<p>\u5728\u9700\u8981\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u7684\u5730\u65b9\uff0c\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f\u63a8\u5165\u9501\u670d\u52a1\u7c7b\u5e76\u4f7f\u7528\uff0c\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u6765\u5b9e\u73b0\u5e42\u7b49\u6027\u7684\u8bf7\u6c42\u5904\u7406\uff1a<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nnamespace AppController;\n\nuse AppUtilsLockService;\nuse HyperfHttpServerAnnotationAutoController;\n\n\/**\n * @AutoController()\n *\/\nclass DemoController\n{\n    public function index(LockService $lockService)\n    {\n        \/\/ \u83b7\u53d6\u9501\n        $lockKey = 'demo_lock';\n        $expire = 10; \/\/ \u8fc7\u671f\u65f6\u95f410\u79d2\n        if ($lockService-&gt;lock($lockKey, $expire)) {\n            \/\/ \u83b7\u5f97\u9501\uff0c\u6267\u884c\u4e1a\u52a1\u903b\u8f91\n            \/\/ TODO: \u5904\u7406\u4e1a\u52a1\u903b\u8f91\n\n            \/\/ \u91ca\u653e\u9501\n            $lockService-&amp;gt;unlock($lockKey);\n        } else {\n            \/\/ \u672a\u83b7\u5f97\u9501\uff0c\u8fd4\u56de\u91cd\u8bd5\u6216\u5931\u8d25\u7684\u54cd\u5e94\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e94\u3001\u603b\u7ed3<br \/>\u901a\u8fc7Hyperf\u6846\u67b6\u7684\u96c6\u6210Redis\u548c\u5c01\u88c5\u5206\u5e03\u5f0f\u9501\u670d\u52a1\u7c7b\uff0c\u6211\u4eec\u80fd\u591f\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u4f7f\u7528\u7b80\u5355\u53ef\u9760\u3001\u9ad8\u6027\u80fd\u7684\u5206\u5e03\u5f0f\u9501\u6765\u7ba1\u7406\u5171\u4eab\u8d44\u6e90\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u53ef\u9760\u6027\u3002\u540c\u65f6\u4e5f\u63d0\u9ad8\u4e86\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u8bf7\u6c42\u7684\u5904\u7406\u6548\u7387\u3002\u5206\u5e03\u5f0f\u9501\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5206\u5e03\u5f0f\u9501\u7ba1\u7406\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5206\u5e03\u5f0f\u9501\u7ba1\u7406 \u5f15\u8a00\uff1a\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u7531\u4e8e\u591a\u4e2a\u8282\u70b9\u540c\u65f6\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u4f1a\u51fa\u73b0\u591a\u4e2a\u8282\u70b9\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u60c5\u51b5\uff0c\u8fdb\u800c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6027\u3001\u810f\u8bfb\u7b49\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e38\u5e38\u9700\u8981\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u673a\u5236\u6765\u4fdd\u8bc1\u8d44\u6e90\u7684\u72ec\u5360\u6027\u3002Hyperf\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u5206\u5e03\u5f0f\u9501\u3002 \u4e00\u3001Hyperf\u6846\u67b6\u7b80\u4ecbHyperf\u662f\u4e00\u4e2a\u57fa\u4e8ePHP\u534f\u7a0b\u7684\u9ad8\u6027\u80fd\uff0c\u7075\u6d3b\u7684\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u6784\u5efa\u6570\u636e\u9a71\u52a8\u7684\u5e94\u7528\u3002\u5b83\u5177\u6709\u4f4e\u95e8\u69db\u3001\u7075\u6d3b\u7684\u4f9d\u8d56\u6ce8\u5165\u3001\u5f3a\u5927\u7684IoC\u5bb9\u5668\u3001\u9ad8\u6027\u80fd\u3001\u6807\u51c6\u7ec4\u4ef6\u4e30\u5bcc\u7b49\u7279\u70b9\u3002 \u4e8c\u3001\u5206\u5e03\u5f0f\u9501\u539f\u7406\u5206\u5e03\u5f0f\u9501\u901a\u5e38\u6709\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a\u57fa\u4e8e\u6570\u636e\u5e93\u548c\u57fa\u4e8e\u7f13\u5b58\u3002\u57fa\u4e8e\u6570\u636e\u5e93\u7684\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\u8f83\u4e3a\u7b80\u5355\uff0c\u4f46\u6027\u80fd\u8f83\u4f4e\u3002\u800c\u57fa\u4e8e\u7f13\u5b58\u7684\u5206\u5e03\u5f0f\u9501\u5219\u901a\u5e38\u4f7f\u7528Redis\u6216\u8005Memcached\u7b49\u9ad8\u6027\u80fd\u7f13\u5b58\u670d\u52a1\u6765\u5b9e\u73b0\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002 \u4e09\u3001Hyperf\u6846\u67b6\u96c6\u6210Redis \u5b89\u88c5Redis\u6269\u5c55 \u5728PHP\u73af\u5883\u4e2d\u4f7f\u7528Redis\u6269\u5c55\u9700\u8981\u5148\u5b89\u88c5Redid\u76f8\u5173\u6269\u5c55\u3002 pecl install redis \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0Redis\u914d\u7f6e \u5728Hyperf\u9879\u76ee\u7684\u914d\u7f6e\u6587\u4ef6config\/autoload\/redis.php\u4e2d\u6dfb\u52a0Redis\u7684\u8fde\u63a5\u53c2\u6570\uff1a &lt;?php declare(strict_types=1); return [ &#8216;default&#8217; =&gt; [ &#8216;host&#8217; =&amp;gt; env(&#8216;REDIS_HOST&#8217;, &#8216;127.0.0.1&#8217;), &#8216;auth&#8217; =&amp;gt; env(&#8216;REDIS_AUTH&#8217;, null), &#8216;port&#8217; =&amp;gt; (int) env(&#8216;REDIS_PORT&#8217;, 6379), &#8216;db&#8217; =&amp;gt; (int) env(&#8216;REDIS_DB&#8217;, 0), &#8216;pool&#8217; =&amp;gt; [ &#8216;max_connections&#8217; =&amp;gt; (int) env(&#8216;REDIS_MAX_CONNECTIONS&#8217;, 10), &#8216;min_connections&#8217; =&amp;gt; (int) env(&#8216;REDIS_MIN_CONNECTIONS&#8217;, 1), &#8216;connect_timeout&#8217; =&amp;gt; (float) env(&#8216;REDIS_CONNECT_TIMEOUT&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24785","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24785"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24785\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}