{"id":1301,"date":"2021-04-25T10:04:10","date_gmt":"2021-04-25T02:04:10","guid":{"rendered":"https:\/\/www.siediyer.cn\/?p=1301"},"modified":"2021-04-25T10:04:10","modified_gmt":"2021-04-25T02:04:10","slug":"redis%e7%bc%93%e5%ad%98%e7%a9%bf%e9%80%8f%e3%80%81%e7%bc%93%e5%ad%98%e5%87%bb%e7%a9%bf%e3%80%81%e7%bc%93%e5%ad%98%e9%9b%aa%e5%b4%a9%e7%9a%84%e5%8e%9f%e7%90%86%e5%92%8c%e8%a7%a3%e5%86%b3%e5%8a%9e","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=1301","title":{"rendered":"Redis\u7f13\u5b58\u7a7f\u900f\u3001\u7f13\u5b58\u51fb\u7a7f\u3001\u7f13\u5b58\u96ea\u5d29\u7684\u539f\u7406\u548c\u89e3\u51b3\u529e\u6cd5"},"content":{"rendered":"<p><strong>1.\u524d\u8a00<\/strong><\/p>\n<p>\u5728\u5927\u6570\u636e\u65f6\u4ee3\uff0c\u7531\u4e8e\u7f51\u7edc\u8bf7\u6c42\u7684\u5e76\u53d1\uff0c\u5bfc\u81f4\u7684\u6570\u636e\u5e93I\/O\u5f00\u9500\u5de8\u5927\uff0c\u6240\u4ee5\u4e3a\u4e86\u7f13\u89e3\u6570\u636e\u5e93\u7684\u538b\u529b\uff0c\u7f13\u5b58\u6280\u672f\u5fc5\u4e0d\u53ef\u5c11\uff0c\u800c\u8fd9\u5176\u4e2dredis\u57fa\u672c\u662f\u670d\u52a1\u5668\u7684\u7f13\u5b58\u670d\u52a1\u4e4b\u4e00\uff0c\u867d\u7136\u7f13\u5b58\u6280\u672f\u5f88\u597d\u7528\uff0c\u4f46\u662f\u4e5f\u4f1a\u51fa\u73b0\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u8fd9\u91cc\u5c31\u6700\u5e38\u89c1\u7684\u4e09\u79cd\u95ee\u9898\u8fdb\u884c\u539f\u7406\u5206\u6790\u548c\u89e3\u51b3\uff0c\u5e0c\u671b\u7ed9\u5927\u5bb6\u5e26\u6765\u5e2e\u52a9<\/p>\n<ul class=\"list-paddingleft-2\">\n<li><strong>\u7f13\u5b58\u7a7f\u900f<\/strong>\uff1akey\u4e2d\u5bf9\u5e94\u7684\u7f13\u5b58\u6570\u636e\u4e0d\u5b58\u5728\uff0c\u5bfc\u81f4\u53bb\u8bf7\u6c42\u6570\u636e\u5e93\uff0c\u9020\u6210\u6570\u636e\u5e93\u7684\u538b\u529b\u500d\u589e\u7684\u60c5\u51b5<\/li>\n<li><strong>\u7f13\u5b58\u51fb\u7a7f<\/strong>\uff1aredis\u8fc7\u671f\u540e\u7684\u4e00\u77ac\u95f4\uff0c\u6709\u5927\u91cf\u7528\u6237\u8bf7\u6c42\u540c\u4e00\u4e2a\u7f13\u5b58\u6570\u636e\uff0c\u5bfc\u81f4\u8fd9\u4e9b\u8bf7\u6c42\u90fd\u53bb\u8bf7\u6c42\u6570\u636e\u5e93\uff0c\u9020\u6210\u6570\u636e\u5e93\u538b\u529b\u500d\u589e\u7684\u60c5\uff0c\u9488\u5bf9\u4e00\u4e2akey\u800c\u8a00<\/li>\n<li><strong>\u7f13\u5b58\u96ea\u5d29<\/strong>\uff1a\u7f13\u5b58\u670d\u52a1\u5668\u5b95\u673a\u6216\u8005\u5927\u91cf\u7f13\u5b58\u96c6\u4e2d\u67d0\u4e2a\u65f6\u95f4\u6bb5\u5931\u6548\uff0c\u5bfc\u81f4\u8bf7\u6c42\u5168\u90e8\u53bb\u5230\u6570\u636e\u5e93\uff0c\u9020\u6210\u6570\u636e\u5e93\u538b\u529b\u500d\u589e\u7684\u60c5\u51b5\uff0c\u8fd9\u4e2a\u662f\u9488\u5bf9\u591a\u4e2akey\u800c\u8a00<\/li>\n<\/ul>\n<h3><strong>2.\u7f13\u5b58\u7a7f\u900f\u7684\u89e3\u51b3\u529e\u6cd5<\/strong><\/h3>\n<p>\u5e38\u7528\u65b9\u6cd5\u53ef\u4ee5\u91c7\u7528\u5e03\u9686\u8fc7\u6ee4\u5668\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u62e6\u622a\uff0c\u5176\u6b21\u53ef\u4ee5\u8fd8\u6709\u4e00\u79cd\u89e3\u51b3\u601d\u8def\uff0c\u5c31\u662f\u5982\u679c\u8bf7\u6c42\u7684\u6570\u636e\u4e3a\u7a7a\uff0c\u5c06\u7a7a\u503c\u4e5f\u8fdb\u884c\u7f13\u5b58\uff0c\u5c31\u4e0d\u4f1a\u53d1\u751f\u7a7f\u900f\u60c5\u51b5<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\nclass getPrizeList {\r\n    \/**\r\n     * redis\u5b9e\u4f8b\r\n     * @var \\Redis\r\n     *\/\r\n    private $redis;\r\n \r\n    \/**\r\n     * @var string\r\n     *\/\r\n    private $redis_key = '|prize_list';\r\n \r\n    \/**\r\n     * \u8fc7\u671f\u65f6\u95f4\r\n     * @var int\r\n     *\/\r\n    private $expire = 30;\r\n \r\n    \/**\r\n     * getPrizeList constructor.\r\n     * @param $redis\r\n     *\/\r\n    public function __construct($redis)\r\n{\r\n        $this-&gt;redis = $redis;\r\n    }\r\n \r\n    \/**\r\n     * @return array|bool|string\r\n     *\/\r\n    public function fetch()\r\n{\r\n        $result = $this-&gt;redis-&gt;get($this-&gt;redis_key);\r\n        if(!isset($result)) {\r\n            \/\/\u6b64\u5904\u5e94\u8be5\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2...\r\n            \/\/\u5982\u679c\u67e5\u8be2\u7ed3\u679c\u4e0d\u5b58\u5728\uff0c\u7ed9\u5176\u9ed8\u8ba4\u7a7a\u6570\u7ec4\u8fdb\u884c\u7f13\u5b58\r\n            $result = [];\r\n            $this-&gt;redis-&gt;set($this-&gt;redis_key, $result, $this-&gt;expire);\r\n        }\r\n \r\n        return $result;\r\n    }\r\n}<\/pre>\n<h3><strong>3.\u7f13\u5b58\u51fb\u7a7f\u89e3\u51b3\u529e\u6cd5<\/strong><\/h3>\n<p>\u4f7f\u7528\u4e92\u65a5\u9501(mutex key),\u5c31\u662f\u4e00\u4e2akey\u8fc7\u671f\u65f6\uff0c\u591a\u4e2a\u8bf7\u6c42\u8fc7\u6765\u5141\u8bb8\u5176\u4e2d\u4e00\u4e2a\u8bf7\u6c42\u53bb\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u5176\u4ed6\u8bf7\u6c42\u7b49\u5f85\u7b2c\u4e00\u4e2a\u8bf7\u6c42\u6210\u529f\u8fd4\u56de\u7ed3\u679c\u540e\u518d\u8bf7\u6c42\u3002<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\nclass getPrizeList {\r\n    \/**\r\n     * redis\u5b9e\u4f8b\r\n     * @var \\Redis\r\n     *\/\r\n    private $redis;\r\n \r\n    \/**\r\n     * @var string\r\n     *\/\r\n    private $redis_key = '|prize_list';\r\n \r\n    \/**\r\n     * @var string\r\n     *\/\r\n    private $setnx_key = '|prize_list_setnx';\r\n \r\n    \/**\r\n     * \u8fc7\u671f\u65f6\u95f4\r\n     * @var int\r\n     *\/\r\n    private $expire = 30;\r\n \r\n    \/**\r\n     * getPrizeList constructor.\r\n     * @param $redis\r\n     *\/\r\n    public function __construct($redis)\r\n{\r\n        $this-&gt;redis = $redis;\r\n    }\r\n \r\n    \/**\r\n     * @return array|bool|string\r\n     *\/\r\n    public function fetch()\r\n{\r\n        $result = $this-&gt;redis-&gt;get($this-&gt;redis_key);\r\n        if(!isset($result)) {\r\n            if($this-&gt;redis-&gt;setnx($this-&gt;setnx_key, 1, $this-&gt;expire)) {\r\n                \/\/\u6b64\u5904\u5e94\u8be5\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2...\r\n                \/\/$result = \u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c;\r\n                $this-&gt;redis-&gt;set($this-&gt;redis_key, $result, $this-&gt;expire);\r\n                $this-&gt;redis-&gt;del($this-&gt;setnx_key); \/\/\u5220\u9664\u4e92\u65a5\u9501\r\n            } else {\r\n                \/\/\u5176\u4ed6\u8bf7\u6c42\u6bcf\u7b49\u5f8510\u6beb\u79d2\u91cd\u65b0\u8bf7\u6c42\u4e00\u6b21\r\n                sleep(10);\r\n                self::fetch();\r\n            }\r\n        }\r\n \r\n        return $result;\r\n    }\r\n}<\/pre>\n<p><strong>4.\u7f13\u5b58\u96ea\u5d29\u7684\u89e3\u51b3\u529e\u6cd5<\/strong><\/p>\n<ul class=\"list-paddingleft-2\">\n<li>\u8fd9\u79cd\u60c5\u51b5\u662f\u56e0\u4e3a\u591a\u4e2akey\u540c\u65f6\u8fc7\u671f\u5bfc\u81f4\u7684\u6570\u636e\u5e93\u538b\u529b\uff0c\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728key\u8fc7\u671f\u65f6\u95f4\u57fa\u7840\u4e0a\u589e\u52a0\u65f6\u95f4\u968f\u673a\u6570\uff0c\u8ba9\u8fc7\u671f\u65f6\u95f4\u5206\u6563\u5f00\uff0c\u51cf\u5c11\u7f13\u5b58\u65f6\u95f4\u8fc7\u671f\u7684\u91cd\u590d\u7387<\/li>\n<li>\u53e6\u4e00\u79cd\u65b9\u6cd5\u5c31\u662f\u52a0\u9501\u6392\u961f\uff0c\u8fd9\u79cd\u6709\u70b9\u50cf\u4e0a\u9762\u7f13\u5b58\u51fb\u7a7f\u7684\u89e3\u51b3\u65b9\u5f0f\uff0c\u4f46\u662f\u8fd9\u79cd\u8bf7\u6c42\u91cf\u592a\u5927\uff0c\u6bd4\u59825000\u4e2a\u8bf7\u6c42\u8fc7\u6765\uff0c4999\u4e2a\u90fd\u9700\u8981\u7b49\u5f85\uff0c\u8fd9\u5fc5\u7136\u662f\u6307\u6807\u4e0d\u6cbb\u672c\uff0c\u4e0d\u4ec5\u7528\u6237\u4f53\u9a8c\u6027\u5dee\uff0c\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\u5c31\u66f4\u52a0\u590d\u6742\uff0c\u56e0\u6b64\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5f88\u5c11\u4f7f\u7528<\/li>\n<li>\u6700\u597d\u7684\u89e3\u51b3\u65b9\u6cd5\uff0c\u662f\u4f7f\u7528\u7f13\u5b58\u6807\u8bb0\uff0c\u5224\u65ad\u8be5\u6807\u8bb0\u662f\u5426\u8fc7\u671f\uff0c\u8fc7\u671f\u5219\u53bb\u8bf7\u6c42\u6570\u636e\u5e93\uff0c\u800c\u7f13\u5b58\u6570\u636e\u7684\u8fc7\u671f\u65f6\u95f4\u8981\u8bbe\u7f6e\u7684\u6bd4\u7f13\u5b58\u6807\u8bb0\u7684\u957f\uff0c\u8fd9\u6837\u5f53\u4e00\u4e2a\u8bf7\u6c42\u53bb\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u65f6\u5019\uff0c\u5176\u4ed6\u8bf7\u6c42\u62ff\u7684\u662f\u4e0a\u4e00\u6b21\u7f13\u5b58\u6570\u636e<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true  \">&lt;?php\r\nclass getPrizeList {\r\n    \/**\r\n     * redis\u5b9e\u4f8b\r\n     * @var \\Redis\r\n     *\/\r\n    private $redis;\r\n \r\n    \/**\r\n     * @var string\r\n     *\/\r\n    private $redis_key = '|prize_list';\r\n \r\n    \/**\r\n     * \u7f13\u5b58\u6807\u8bb0key\r\n     * @var string\r\n     *\/\r\n    private $cash_key = '|prize_list_cash';\r\n \r\n    \/**\r\n     * \u8fc7\u671f\u65f6\u95f4\r\n     * @var int\r\n     *\/\r\n    private $expire = 30;\r\n \r\n    \/**\r\n     * getPrizeList constructor.\r\n     * @param $redis\r\n     *\/\r\n    public function __construct($redis)\r\n{\r\n        $this-&gt;redis = $redis;\r\n    }\r\n \r\n    \/**\r\n     * @return array|bool|string\r\n     *\/\r\n    public function fetch()\r\n{\r\n        $cash_result = $this-&gt;redis-&gt;get($this-&gt;cash_key);\r\n        $result = $this-&gt;redis-&gt;get($this-&gt;redis_key);\r\n        if(!$cash_result) {\r\n            $this-&gt;redis-&gt;set($this-&gt;cash_key, 1, $this-&gt;expire);\r\n            \/\/\u6b64\u5904\u5e94\u8be5\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2...\r\n            \/\/$result = \u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c, \u5e76\u4e14\u8bbe\u7f6e\u7684\u65f6\u95f4\u8981\u6bd4cash_key\u957f\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u4e3a2\u500d;\r\n            $this-&gt;redis-&gt;set($this-&gt;redis_key, $result, $this-&gt;expire * 2);\r\n        }\r\n \r\n        return $result;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u524d\u8a00 \u5728\u5927\u6570\u636e\u65f6\u4ee3\uff0c\u7531\u4e8e\u7f51\u7edc\u8bf7\u6c42\u7684\u5e76\u53d1\uff0c\u5bfc\u81f4\u7684\u6570\u636e\u5e93I\/O\u5f00\u9500\u5de8\u5927\uff0c\u6240\u4ee5\u4e3a\u4e86\u7f13\u89e3\u6570\u636e\u5e93\u7684\u538b\u529b\uff0c\u7f13\u5b58\u6280\u672f\u5fc5\u4e0d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1301","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1301"}],"version-history":[{"count":0,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1301\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}