{"id":47836,"date":"2022-07-19T10:19:24","date_gmt":"2022-07-19T02:19:24","guid":{"rendered":"http:\/\/www.xyryd.com\/?p=47836"},"modified":"2022-07-19T10:21:03","modified_gmt":"2022-07-19T02:21:03","slug":"php%e4%b8%8b%e7%94%a8redis%e8%a7%a3%e5%86%b3%e7%a7%92%e6%9d%80%e8%b6%85%e5%8d%96%e9%97%ae%e9%a2%98%ef%bf%bc","status":"publish","type":"post","link":"http:\/\/www.xyryd.com\/47836.html","title":{"rendered":"php\u4e0b\u7528redis\u89e3\u51b3\u79d2\u6740\u8d85\u5356\u95ee\u9898"},"content":{"rendered":"\n

\u79d2\u6740\u8d85\u5356\u95ee\u9898\uff0c\u5c31\u662f\u6709\u4e00\u4e2a\u5546\u54c1\u62a2\u8d2d\u6d3b\u52a8\uff0c\u4e00\u4e2a\u5546\u54c1\u5047\u5982\u6709100\u4ef6\u5e93\u5b58\uff0c\u4f46\u662f\u5728\u62a2\u8d2d\u65f6\u6709200\u4eba\u6765\u62a2\u8d2d\uff0c\u8fd9\u65f6\u5c31\u4f1a\u5e76\u53d1\uff0c\u539f\u672c\u53ea\u6709100\u7684\u5e93\u5b58\u4f46\u662f\u62a2\u8d2d\u7684\u4eba\u8fc7\u591a\uff0c\u5c31\u4f1a\u53d1\u751f\u6570\u636e\u5e93\u91cc\u539f\u672c\u53ea\u6709100\u7684\u5e93\u5b58\u4f46\u662f\u5e93\u5b58\u4e3a0\u7684\u65f6\u5019\u8fd8\u4f1a\u6709\u4eba\u63d0\u4ea4\u6210\u529f\uff0c\u8fd9\u5c31\u662f\u8d85\u5356\u3002<\/p>\n\n\n\n

  \u4eca\u5929\u7b80\u5355\u7684\u7528redis\u7684\u961f\u5217\u6765\u89e3\u51b3\u8d85\u5356\u95ee\u9898\u3002\u56e0\u4e3aredis\u6709list\u7c7b\u578b\uff0clist\u7c7b\u578b\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\u3002\u901a\u8fc7push,pop\u64cd\u4f5c\u4ece\u94fe\u8868\u7684\u5934\u90e8\u6216\u8005\u5c3e\u90e8\u6dfb\u52a0\u5220\u9664\u5143\u7d20\u3002\u8fd9\u4f7f\u5f97list\u65e2\u53ef\u4ee5\u7528\u4f5c\u6808\uff0c\u4e5f\u53ef\u4ee5\u7528\u4f5c\u961f\u5217\u3002\u5148\u8fdb\u5148\u51fa  \u4e00\u7aef\u8fdb  \u4e00\u7aef\u51fa\u8fd9\u5c31\u662f\u961f\u5217\u3002\u8fd9\u91cc\u7528redis\u5c31\u89e3\u51b3\u4e86\u5e76\u53d1\u7684\u95ee\u9898\uff0c\u5728\u961f\u5217\u91cc\u524d\u4e00\u4e2a\u8d70\u5b8c\u4e4b\u540e\uff0c\u540e\u4e00\u4e2a\u624d\u4f1a\u8d70\u3002<\/p>\n\n\n\n

<\/a><\/a>  \u5b9e\u73b0\u539f\u7406<\/h3>\n\n\n\n

   \u5c06\u5e93\u5b58\u5faa\u73aflpush\u8fdb\u4e00\u4e2aredis\u503cgoods_number\u91cc\u53bb\uff0c\u7136\u540e\u5728\u4e0b\u5355\u7684\u65f6\u5019\u4f9d\u6b21rpop\u51fa\u6765\u3002\u8fd9\u6837\u5c31\u662f\u4e0b\u4e00\u4e2a\u5355\u53d6\u51fa\u6765\u4e00\u4e2a\uff0c\u7136\u540e\u7b49goods_number\u7684\u503c\u4e3a0\u65f6\uff0c\u505c\u6b62\u4e0b\u5355\u3002<\/p>\n\n\n\n

  \u9996\u5148\u5efa\u7acb\u4e09\u4e2a\u8868 store\u5546\u54c1\u8868  order\u8ba2\u5355\u8868  log\u65e5\u5fd7\u8868<\/p>\n\n\n\n

CREATE TABLE `store` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `goods_id` int(11) NOT NULL,\n  `sku_id` int(10) unsigned NOT NULL DEFAULT '0',\n  `number` int(10) NOT NULL DEFAULT '0',\n  `freez` float(11,2) NOT NULL DEFAULT '0.00' COMMENT '\u865a\u62df\u5e93\u5b58',\n  `price` int(10) NOT NULL COMMENT '\u4ef7\u683c\uff1a\u5355\u4f4d\u4e3a\u5206',\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='\u5e93\u5b58';\n\n \nCREATE TABLE `order` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `order_sn` char(32) NOT NULL,\n  `user_id` int(11) NOT NULL,\n  `status` int(11) NOT NULL DEFAULT '0',\n  `goods_id` int(11) NOT NULL DEFAULT '0',\n  `sku_id` int(11) NOT NULL DEFAULT '0',\n  `number` int(11) NOT NULL,\n  `price` int(10) NOT NULL COMMENT '\u4ef7\u683c\uff1a\u5355\u4f4d\u4e3a\u5206',\n  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=4743 DEFAULT CHARSET=utf8 COMMENT='\u8ba2\u5355\u8868';\n\n \nCREATE TABLE `order` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `order_sn` char(32) NOT NULL,\n  `user_id` int(11) NOT NULL,\n  `status` int(11) NOT NULL DEFAULT '0',\n  `goods_id` int(11) NOT NULL DEFAULT '0',\n  `sku_id` int(11) NOT NULL DEFAULT '0',\n  `number` int(11) NOT NULL,\n  `price` int(10) NOT NULL COMMENT '\u4ef7\u683c\uff1a\u5355\u4f4d\u4e3a\u5206',\n  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=4743 DEFAULT CHARSET=utf8 COMMENT='\u8ba2\u5355\u8868';<\/code><\/pre>\n\n\n\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n

<?php\nnamespace Home\\Controller;\nuse Think\\Cache\\Driver\\Redis;\nuse Think\\Controller;\n\nclass TestController extends Controller\n{\n    public function index()\n    {\n        $wheres = array();\n        $wheres['goods_id'] = 1;\n        $number = M('store')->where($wheres)->getField('number');\n        $redis = new Redis();\n        for ($i = 0; $i < $number; $i++) {\n            $redis->lpush('goods_number', 1);\n        }\n        echo $redis->llen('goods_number');\n    }\n\n    \/\/\u751f\u6210\u552f\u4e00\u8ba2\u5355\u53f7  \n    function build_order_no()\n    {\n        return date('ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);\n    }\n    \/\/\u8bb0\u5f55\u65e5\u5fd7  \n    function insertLog($event, $type = 0)\n    {\n        $data['event'] = $event;\n        $data['type']  = $type;\n        $res = M('log')->add($data);\n    }\n\n    \/\/\u6a21\u62df\u4e0b\u5355\u64cd\u4f5c  \n    \/\/\u4e0b\u5355\u524d\u5224\u65adredis\u961f\u5217\u5e93\u5b58\u91cf  \n    function order()\n    {\n        $sku_id  = 11;  \/\/\u4f20\u5165\u5df2\u77e5\u7684sku_id;\n\n        $wheres = array();\n        $wheres['sku_id'] = $sku_id;\n        $good_info = M('store')->where($wheres)->find();\n\n        $user_id  = rand(1, 200);\n        $goods_id = $good_info['goods_id'];\n        $price    = $good_info['price'];\n        $number   = 1; \/\/\u62a2\u8d2d\u65f6\u6bcf\u6b21\u4e70\u4e00\u4ef6\u5546\u54c1\n\n\n        $redis = new Redis();\n        $count = $redis->rpop('goods_number');  \/\/\u4e0b\u5355\u65f6\u505arpop \u4ecegoods_number\u4e2d\u53d6\u51fa1\n        if ($count == 0) {\n            $this->insertLog('error:no goods_number redis');\n            return;\n        }\n\n\n        if (($good_info['number'] - $number) <= 0) {\n            $this->insertLog('\u5546\u54c1\u552e\u7f44');  \/\/\u5982\u679c\u5e93\u5b58\u4e3a0\u5199\u5165\u65e5\u5fd7 \u5e76\u505c\u6b62\u4e0b\u5355\u64cd\u4f5c\n            return;\n        }\n\n\n        \/\/\u751f\u6210\u8ba2\u5355    \n        $order_sn = $this->build_order_no();\n\n        $data = array();\n        $data['order_sn'] = $order_sn;\n        $data['user_id']  = $user_id;\n        $data['goods_id'] = $goods_id;\n        $data['sku_id']   = $sku_id;\n        $data['number']   = $number;\n        $data['price']    = $price;\n\n        $order_rs = M('order')->add($data);\n\n        \/\/\u5e93\u5b58\u51cf\u5c11  \n        $wheres['sku_id'] = $sku_id;\n        $store_rs = M('store')->where($wheres)->setDec('number', $number);\n        if ($store_rs) {\n            $this->insertLog('\u5e93\u5b58\u51cf\u5c11\u6210\u529f');\n        } else {\n            $this->insertLog('\u5e93\u5b58\u51cf\u5c11\u5931\u8d25');\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n

\u5148\u8bbf\u95ee\uff1a\/test\/index \u5c06redis\u7684list\u521d\u59cb\u5316<\/p>\n\n\n\n

\u7136\u540e\u53ef\u4ee5\u8bbf\u95ee\uff1a\/test\/order \u8fdb\u884c\u6a21\u62df<\/p>\n","protected":false},"excerpt":{"rendered":"

\u79d2\u6740\u8d85\u5356\u95ee\u9898\uff0c\u5c31\u662f\u6709\u4e00\u4e2a\u5546\u54c1\u62a2\u8d2d\u6d3b\u52a8\uff0c\u4e00\u4e2a\u5546\u54c1\u5047\u5982\u6709100\u4ef6\u5e93\u5b58\uff0c\u4f46\u662f\u5728\u62a2\u8d2d\u65f6\u6709200\u4eba\u6765\u62a2\u8d2d\uff0c\u8fd9\u65f6\u5c31\u4f1a\u5e76\u53d1\uff0c […]<\/p>\n","protected":false},"author":1,"featured_media":43088,"comment_status":"open","ping_status":"closed","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13,29,168],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/posts\/47836"}],"collection":[{"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/comments?post=47836"}],"version-history":[{"count":0,"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/posts\/47836\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/media\/43088"}],"wp:attachment":[{"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/media?parent=47836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/categories?post=47836"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.xyryd.com\/wp-json\/wp\/v2\/tags?post=47836"}],"curies":[{"name":"wp","href":"http:\/\/api.w.org\/{rel}","templated":true}]}}