{"id":1250,"date":"2026-01-06T15:57:08","date_gmt":"2026-01-06T07:57:08","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1250"},"modified":"2026-01-06T15:57:08","modified_gmt":"2026-01-06T07:57:08","slug":"php%e4%bd%bf%e7%94%a8%e4%ba%92%e6%96%a5%e9%94%81%e7%a1%ae%e4%bf%9d%e4%bb%a3%e7%a0%81%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/php%e4%bd%bf%e7%94%a8%e4%ba%92%e6%96%a5%e9%94%81%e7%a1%ae%e4%bf%9d%e4%bb%a3%e7%a0%81%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b\/","title":{"rendered":"PHP\u4f7f\u7528\u4e92\u65a5\u9501\u786e\u4fdd\u4ee3\u7801\u7684\u7ebf\u7a0b\u5b89\u5168\u7684\u64cd\u4f5c\u793a\u4f8b"},"content":{"rendered":"\n<p>\u4e0b\u9762\u6211\u7528**\u300c\u6982\u5ff5 \u2192 \u4e09\u79cd\u9501\u5b9e\u73b0 \u2192 \u5b8c\u6574\u793a\u4f8b \u2192 \u4f7f\u7528\u5efa\u8bae\u300d<strong>\uff0c\u7ed9\u4f60\u4e00\u4efd<\/strong>\u771f\u6b63\u53ef\u843d\u5730\u7684 PHP \u4e92\u65a5\u9501\u7ebf\u7a0b\u5b89\u5168\u793a\u4f8b\u5408\u96c6**\ud83d\udc47<br>\uff08\u8986\u76d6\uff1a<strong>\u6587\u4ef6\u9501 \/ Redis \u9501 \/ \u6570\u636e\u5e93\u9501<\/strong>\uff09<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u26a0\ufe0f \u8bf4\u660e\uff1a<br><strong>PHP \u672c\u8eab\u662f\u591a\u8fdb\u7a0b\u800c\u975e\u591a\u7ebf\u7a0b<\/strong>\uff0c\u8fd9\u91cc\u7684\u201c\u7ebf\u7a0b\u5b89\u5168\u201d\u6307\u7684\u662f<strong>\u5e76\u53d1\u8bf7\u6c42\u4e0b\u7684\u4e34\u754c\u533a\u5b89\u5168<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4e3a\u4ec0\u4e48 PHP \u9700\u8981\u4e92\u65a5\u9501\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ca1\u52a0\u9501\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$balance = file_get_contents(&#039;money.txt&#039;);\n$balance -= 100;\nfile_put_contents(&#039;money.txt&#039;, $balance);\n\n<\/pre><\/div>\n\n\n<p>\u274c \u5e76\u53d1 2 \u4e2a\u8bf7\u6c42 \u2192 <strong>\u4f59\u989d\u53ea\u6263\u4e00\u6b21<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u65b9\u6848\u4e00\uff1a\u6587\u4ef6\u9501\uff08flock\uff09\u3010\u6700\u7b80\u5355\u3011<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u9002\u7528\u573a\u666f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5355\u673a<\/li>\n\n\n\n<li>\u5c0f\u5e76\u53d1<\/li>\n\n\n\n<li>CLI \/ cron<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u6587\u4ef6\u5199\u5165\u4e92\u65a5<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$fp = fopen(&#039;lock.txt&#039;, &#039;c&#039;);\n\nif (flock($fp, LOCK_EX)) {   \/\/ \u83b7\u53d6\u6392\u4ed6\u9501\n    $balance = (int) file_get_contents(&#039;money.txt&#039;);\n    sleep(1); \/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n    $balance -= 100;\n    file_put_contents(&#039;money.txt&#039;, $balance);\n    flock($fp, LOCK_UN);     \/\/ \u91ca\u653e\u9501\n}\n\nfclose($fp);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u7279\u70b9<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td>\u7b80\u5355<\/td><td>\u53ea\u80fd\u5355\u673a<\/td><\/tr><tr><td>\u65e0\u4f9d\u8d56<\/td><td>NFS \u4e0d\u53ef\u9760<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u65b9\u6848\u4e8c\uff1aRedis \u5206\u5e03\u5f0f\u9501\uff08\u63a8\u8350 \u2b50\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u9002\u7528\u573a\u666f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Web \u9ad8\u5e76\u53d1<\/li>\n\n\n\n<li>\u591a\u670d\u52a1\u5668<\/li>\n\n\n\n<li>\u79d2\u6740 \/ \u62a2\u8d2d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u539f\u7406\uff08\u4e00\u5b9a\u8981\u61c2\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSET key value NX EX ttl\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>NX\uff1a\u4e0d\u5b58\u5728\u624d\u8bbe\u7f6e<\/li>\n\n\n\n<li>EX\uff1a\u8fc7\u671f\u65f6\u95f4\uff08\u9632\u6b7b\u9501\uff09<\/li>\n\n\n\n<li>value\uff1a\u552f\u4e00\u6807\u8bc6\uff08\u9632\u8bef\u5220\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u5b89\u5168\u7248 Redis \u9501<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$redis = new Redis();\n$redis-&gt;connect(&#039;127.0.0.1&#039;, 6379);\n\n$lockKey = &#039;lock:order:123&#039;;\n$token   = uniqid();\n\n$isLock = $redis-&gt;set($lockKey, $token, &#x5B;&#039;nx&#039;, &#039;ex&#039; =&gt; 5]);\n\nif ($isLock) {\n    try {\n        \/\/ \u4e34\u754c\u533a\u4ee3\u7801\n        processOrder();\n    } finally {\n        \/\/ Lua \u811a\u672c\u4fdd\u8bc1\u539f\u5b50\u91ca\u653e\n        $lua = &#039;\n        if redis.call(&quot;get&quot;, KEYS&#x5B;1]) == ARGV&#x5B;1] then\n            return redis.call(&quot;del&quot;, KEYS&#x5B;1])\n        else\n            return 0\n        end&#039;;\n        $redis-&gt;eval($lua, &#x5B;$lockKey, $token], 1);\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u4e0d\u7528 <code>DEL key<\/code>\uff1f<\/h3>\n\n\n\n<p>\u274c \u9501\u8fc7\u671f \u2192 \u88ab\u522b\u4eba\u62ff\u5230 \u2192 \u4f60\u5220\u4e86\u522b\u4eba\u7684\u9501<br>\u2714 \u7528 token \u6821\u9a8c\u624d\u80fd\u5b89\u5168<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u65b9\u6848\u4e09\uff1aMySQL \u9501\uff08\u6570\u636e\u5e93\u7ea7\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u5f0f 1\uff1a\u60b2\u89c2\u9501\uff08<code>SELECT FOR UPDATE<\/code>\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$pdo-&gt;beginTransaction();\n\n$stmt = $pdo-&gt;query(&quot;SELECT balance FROM account WHERE id=1 FOR UPDATE&quot;);\n$balance = $stmt-&gt;fetchColumn();\n\nsleep(1);\n$pdo-&gt;exec(&quot;UPDATE account SET balance = balance - 100 WHERE id=1&quot;);\n\n$pdo-&gt;commit();\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>\u5fc5\u987b\u5728\u4e8b\u52a1\u4e2d<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u5f0f 2\uff1aMySQL \u547d\u540d\u9501\uff08GET_LOCK\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$stmt = $pdo-&gt;query(&quot;SELECT GET_LOCK(&#039;order_123&#039;, 5)&quot;);\nif ($stmt-&gt;fetchColumn()) {\n    try {\n        processOrder();\n    } finally {\n        $pdo-&gt;query(&quot;SELECT RELEASE_LOCK(&#039;order_123&#039;)&quot;);\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL \u9501\u5bf9\u6bd4<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u7279\u70b9<\/th><\/tr><\/thead><tbody><tr><td>FOR UPDATE<\/td><td>\u884c\u7ea7\u9501<\/td><\/tr><tr><td>GET_LOCK<\/td><td>\u8fdb\u7a0b\u7ea7\u4e92\u65a5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u771f\u5b9e\u4e1a\u52a1\u5b8c\u6574\u793a\u4f8b\uff08\u9632\u6b62\u8ba2\u5355\u91cd\u590d\u652f\u4ed8\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Redis \u9501 + \u5e42\u7b49<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction payOrder($orderId) {\n    global $redis;\n\n    $lockKey = &quot;pay:$orderId&quot;;\n    $token = uniqid();\n\n    if (! $redis-&gt;set($lockKey, $token, &#x5B;&#039;nx&#039;, &#039;ex&#039; =&gt; 10])) {\n        throw new Exception(&#039;\u8ba2\u5355\u6b63\u5728\u5904\u7406\u4e2d&#039;);\n    }\n\n    try {\n        \/\/ \u652f\u4ed8\u903b\u8f91\n        doPay();\n        markPaid();\n    } finally {\n        $redis-&gt;eval(\n            &#039;if redis.call(&quot;get&quot;, KEYS&#x5B;1]) == ARGV&#x5B;1] then return redis.call(&quot;del&quot;, KEYS&#x5B;1]) end&#039;,\n            &#x5B;$lockKey, $token],\n            1\n        );\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u4e09\u79cd\u65b9\u6848\u8be5\u600e\u4e48\u9009\uff1f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u63a8\u8350<\/th><\/tr><\/thead><tbody><tr><td>\u5355\u673a\u811a\u672c<\/td><td>flock<\/td><\/tr><tr><td>Web \u5e76\u53d1<\/td><td>Redis<\/td><\/tr><tr><td>\u5f3a\u4e00\u81f4\u4e8b\u52a1<\/td><td>MySQL<\/td><\/tr><tr><td>\u5206\u5e03\u5f0f\u7cfb\u7edf<\/td><td>Redis<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u4e92\u65a5\u9501\u4f7f\u7528\u9ec4\u91d1\u6cd5\u5219\uff08\u5fc5\u770b\uff09<\/h2>\n\n\n\n<p>\u2714 \u9501\u7c92\u5ea6\u8981\u5c0f\uff08\u7cbe\u786e\u5230 ID\uff09<br>\u2714 \u5fc5\u987b\u8bbe\u7f6e\u8d85\u65f6<br>\u2714 \u4e00\u5b9a try-finally<br>\u2714 \u4e0d\u8981\u9501\u91cc\u5199\u6162 IO<br>\u2714 \u4e0d\u8981\u5d4c\u5957\u9501\uff08\u6b7b\u9501\uff09<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u4e00\u53e5\u8bdd\u603b\u7ed3<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>PHP \u7684\u201c\u7ebf\u7a0b\u5b89\u5168\u201d\u4e0d\u662f\u9760\u8bed\u8a00\uff0c\u800c\u662f\u9760\u201c\u6b63\u786e\u5730\u4f7f\u7528\u4e92\u65a5\u9501\u201d<\/strong><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u6211\u7528**\u300c\u6982\u5ff5 \u2192 \u4e09\u79cd\u9501\u5b9e\u73b0 \u2192 \u5b8c\u6574\u793a\u4f8b \u2192 \u4f7f\u7528\u5efa\u8bae\u300d\uff0c\u7ed9\u4f60\u4e00\u4efd\u771f\u6b63\u53ef&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/php%e4%bd%bf%e7%94%a8%e4%ba%92%e6%96%a5%e9%94%81%e7%a1%ae%e4%bf%9d%e4%bb%a3%e7%a0%81%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-1250","post","type-post","status-publish","format-standard","hentry","category-php"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1250"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1250\/revisions"}],"predecessor-version":[{"id":1251,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1250\/revisions\/1251"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}