{"id":981,"date":"2021-06-07T14:54:47","date_gmt":"2021-06-07T06:54:47","guid":{"rendered":"http:\/\/blog.yyhcw.com\/?p=981"},"modified":"2021-06-07T14:59:49","modified_gmt":"2021-06-07T06:59:49","slug":"php%e7%a7%92%e6%9d%80%e7%b3%bb%e7%bb%9f%e8%ae%be%e8%ae%a1%e5%ae%9e%e7%8e%b0%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/blog.yyhcw.com\/post\/981.html","title":{"rendered":"PHP\u79d2\u6740\u7cfb\u7edf\u8bbe\u8ba1\u5b9e\u73b0\u4ee3\u7801"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><h2>\u573a\u666f\u63cf\u8ff0<\/h2>\n<p>\u5047\u8bbe\u516c\u53f8\u73b0\u5728\u8981\u641e\u4e00\u4e2a\u7279\u4ef7\u4fc3\u9500\u6d3b\u52a8\uff0c\u5546\u54c1\u4e3aiPhone11\u624b\u673a\uff0c\u5e93\u5b58\u4e3a2\u53f0\uff0c\u8d2d\u4e70\u65f6\u95f4\u4e3a\u665a\u4e0a8\u70b9\u3002\u9884\u8ba1\u6709100\u540d\u7528\u6237\u53c2\u4e0e\u8fd9\u6b21\u4fc3\u9500\u6d3b\u52a8\u3002\u73b0\u5728\u7528<a href=\"https:\/\/blog.yyhcw.com\/post\/tag\/php\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e PHP \u7684\u6587\u7ae0\" target=\"_blank\">PHP<\/a>\u505a\u4e86\u4e00\u4e2a\u94fe\u63a5\uff0c\u65f6\u95f4\u4e00\u5230\uff0c\u8c01\u5148\u70b9\u51fb\uff0c\u8c01\u5c31\u53ef\u4ee5\u4e0b\u5355\u8d2d\u4e70\u3002<\/p>\n<h2>\u8868\u8bbe\u8ba1<\/h2>\n<h5>\u5546\u54c1\u5e93\u5b58\u8868<\/h5>\n<pre class=\"lang:mysql decode:true\" title=\"\u79d2\u6740\u8868\u8bbe\u8ba1\">CREATE TABLE `goods` (\r\n  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\r\n  `subject` varchar(100) DEFAULT NULL,\r\n  `stock` int(3) DEFAULT '0',\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8\r\nINSERT INTO `goods`(`id`, `subject`, `stock`) VALUES (1, 'iPhone11', 2);<\/pre>\n<h5>\u8ba2\u5355\u8868<\/h5>\n<pre class=\"lang:mysql decode:true\" title=\"\u79d2\u6740\u5546\u54c1\u8ba2\u5355\u8868\u8bbe\u8ba1\">CREATE TABLE `good_orders` (\r\n  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\r\n  `user_id` varchar(36) DEFAULT NULL,\r\n  `good_id` int(11) DEFAULT NULL,\r\n  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8<\/pre>\n<h2>PHP\u4f2a\u4ee3\u7801\u5982\u4e0b\uff1a<\/h2>\n<pre class=\"lang:php decode:true\">$sql = \"SELECT * from goods where id = 1\";\r\n$userid = uniqid();\r\n$row = $db-&gt;query($sql);\r\nif ($row['stock'] &gt; 0) {\r\n    \/\/\u63d2\u5165\u8ba2\u5355\u8868\r\n    $db-&gt;exec(\"insert into good_orders set user_id ='{$userid}',good_id='1'\");\r\n    \/\/\u51cf\u5e93\u5b58\r\n    $stock = $row['stock'] - 1;\r\n    $db-&gt;exec(\"update goods set stock ='{$stock}' where good_id='1'\");\r\n    echo '\u8d2d\u4e70\u6210\u529f';\r\n} else {\r\n    echo '\u5e93\u5b58\u4e0d\u8db3';\r\n}<\/pre>\n<p>\u6b64\u7a0b\u5e8f\u4e0d\u5728\u5e76\u53d1\u6761\u4ef6\u4e0b\uff0c\u5355\u4e2a\u70b9\u51fb\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0cgood_orders\u8868\u91cc\u53ea\u4f1a\u51fa\u73b02\u6761\u8ba2\u5355\u8bb0\u5f55\uff0c\u4f46\u4e00\u65e6\u5904\u4e8e\u5e76\u53d1\u6761\u4ef6\u4e0b\uff0c\u5c31\u4f1a\u5b58\u5728\u591a\u6761\u8ba2\u5355\uff0c\u8fd9\u6837\u5c31\u9020\u6210\u4e86\u8d85\u5356\u7684\u60c5\u51b5\uff0c\u8001\u677f\u4e8f\u4e86\u54b1\u7684\u65e5\u5b50\u4e5f\u4e0d\u597d\u8fc7\u3002<\/p>\n<h5>\u4f7f\u7528\u6587\u4ef6\u9501<\/h5>\n<pre class=\"lang:php decode:true\" title=\"\u79d2\u6740\u7cfb\u7edf\u4f7f\u7528\u6587\u4ef6\u9501\">$fp = fopen(\"lock.txt\", \"w+\");\r\nif (flock($fp, LOCK_EX)) { \/\/ \u8fdb\u884c\u6392\u5b83\u578b\u9501\u5b9a\r\n    $sql = \"SELECT * from goods where id = 1\";\r\n    $userid = uniqid();\r\n    $row = $db-&gt;query($sql);\r\n    if ($row['stock'] &gt; 0) {\r\n        \/\/\u63d2\u5165\u8ba2\u5355\u8868\r\n        $db-&gt;exec(\"insert into good_orders set user_id ='{$userid}',good_id='1'\");\r\n        \/\/\u51cf\u5e93\u5b58\r\n        $stock = $row['stock'] - 1;\r\n        $db-&gt;exec(\"update goods set stock ='{$stock}' where good_id='1'\");\r\n        echo '\u8d2d\u4e70\u6210\u529f';\r\n    } else {\r\n        echo '\u5e93\u5b58\u4e0d\u8db3';\r\n    }\r\n    flock($fp, LOCK_UN); \/\/ \u91ca\u653e\u9501\u5b9a\r\n} else {\r\n    echo '\u524d\u65b9\u62e5\u6324';\r\n}\r\nfclose($fp);<\/pre>\n<h2>redis\u5b9e\u73b0<\/h2>\n<h5>\u5c06\u5e93\u5b58\u653e\u5165redis<\/h5>\n<pre class=\"lang:php decode:true \">$redis = new \\Redis;\r\n$redis-&gt;connect('127.0.0.1', 6379);\r\n$sql = \"SELECT * from goods where id = 1\";\r\n$userid = uniqid();\r\n$row = $db-&gt;query($sql);\r\nif ($row['stock'] &gt; 0) {\r\n    for ($i = 0; $i &lt; $row['stock']; $i++) {\r\n        $redis-&gt;lpush('goods_number', $i);\r\n    }\r\n}\r\necho $redis-&gt;llen('goods_number');<\/pre>\n<h5>\u4eceredis\u4e2d\u6263\u5e93\u5b58<\/h5>\n<pre class=\"lang:php decode:true \">$stock = $this-&gt;redis-&gt;llen('goods_number');\r\n$count = $this-&gt;redis-&gt;rpop('goods_number');  \/\/\u4e0b\u5355\u65f6\u505arpop \u4ecegoods_number\u4e2d\u53d6\u51fa1\r\nif ($count===false) {\r\n    echo '\u5e93\u5b58\u4e0d\u8db3';exit();\r\n}\r\n$userid = uniqid();\r\n\/\/\u63d2\u5165\u8ba2\u5355\u8868\r\n$db-&gt;exec(\"insert into good_orders set user_id ='{$userid}',good_id='1'\");\r\n\/\/\u51cf\u5e93\u5b58\r\n$stock = $row['stock'] - 1;\r\n$db-&gt;exec(\"update goods set stock ='{$stock}' where good_id='1'\");\r\necho '\u8d2d\u4e70\u6210\u529f';<\/pre>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"\u573a\u666f\u63cf\u8ff0 \u5047\u8bbe\u516c\u53f8\u73b0\u5728\u8981\u641e\u4e00\u4e2a\u7279\u4ef7\u4fc3\u9500\u6d3b\u52a8\uff0c\u5546\u54c1\u4e3aiPhone11\u624b\u673a\uff0c\u5e93\u5b58\u4e3a2\u53f0\uff0c\u8d2d\u4e70\u65f6\u95f4\u4e3a\u665a\u4e0a8\u70b9\u3002\u9884\u8ba1\u6709 \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[14,268],"views":393,"_links":{"self":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/981"}],"collection":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/comments?post=981"}],"version-history":[{"count":0,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/posts\/981\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/media?parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/categories?post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.yyhcw.com\/api\/wp\/v2\/tags?post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}