{"id":184866,"date":"2024-11-10T16:47:18","date_gmt":"2024-11-10T08:47:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184866\/"},"modified":"2024-11-10T16:47:19","modified_gmt":"2024-11-10T08:47:19","slug":"%e4%bd%bf%e7%94%a8redis%e5%af%a6%e7%8f%bephp%e4%b8%ad%e8%ab%8b%e6%b1%82%e9%99%90%e6%b5%81%ef%bc%88redis%e9%99%90%e9%80%9fphp%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184866\/","title":{"rendered":"\u4f7f\u7528Redis\u5be6\u73fePHP\u4e2d\u8acb\u6c42\u9650\u6d41\uff08redis\u9650\u901fphp\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e5%af%a6%e7%8f%bephp%e4%b8%ad%e8%ab%8b%e6%b1%82%e9%99%90%e6%b5%81%ef%bc%88redis%e9%99%90%e9%80%9fphp%ef%bc%89-HVywYaVvOu\">\u4f7f\u7528Redis\u5be6\u73fePHP\u4e2d\u8acb\u6c42\u9650\u6d41\uff08redis\u9650\u901fphp\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u8acb\u6c42\u9650\u6d41\u662f\u4e00\u500b\u91cd\u8981\u7684\u6280\u8853\uff0c\u7279\u5225\u662f\u5728\u9ad8\u6d41\u91cf\u7684\u61c9\u7528\u4e2d\u3002\u8acb\u6c42\u9650\u6d41\u53ef\u4ee5\u5e6b\u52a9\u9632\u6b62\u670d\u52d9\u5668\u904e\u8f09\uff0c\u4fdd\u8b77\u61c9\u7528\u7a0b\u5e8f\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u7d50\u69cb\u548c\u64cd\u4f5c\uff0c\u7279\u5225\u9069\u5408\u7528\u65bc\u5be6\u73fe\u8acb\u6c42\u9650\u6d41\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728PHP\u4e2d\u4f7f\u7528Redis\u4f86\u5be6\u73fe\u8acb\u6c42\u9650\u6d41\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e8%ab%8b%e6%b1%82%e9%99%90%e6%b5%81%ef%bc%9f-HVywYaVvOu\">\u4ec0\u9ebc\u662f\u8acb\u6c42\u9650\u6d41\uff1f<\/h2>\n<p>\u8acb\u6c42\u9650\u6d41\u662f\u6307\u5c0d\u7528\u6236\u7684\u8acb\u6c42\u9032\u884c\u9650\u5236\uff0c\u4ee5\u9632\u6b62\u904e\u591a\u7684\u8acb\u6c42\u5f71\u97ff\u7cfb\u7d71\u7684\u6027\u80fd\u3002\u9019\u7a2e\u6280\u8853\u901a\u5e38\u7528\u65bcAPI\u670d\u52d9\u3001\u7db2\u7ad9\u548c\u5176\u4ed6\u9700\u8981\u8655\u7406\u5927\u91cf\u8acb\u6c42\u7684\u61c9\u7528\u4e2d\u3002\u901a\u904e\u8a2d\u7f6e\u8acb\u6c42\u7684\u6700\u5927\u6578\u91cf\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u6d41\u91cf\uff0c\u907f\u514d\u670d\u52d9\u5668\u904e\u8f09\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-HVywYaVvOu\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u5177\u6709\u4ee5\u4e0b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>Redis\u7684\u6578\u64da\u64cd\u4f5c\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9069\u5408\u7528\u65bc\u9ad8\u983b\u7387\u7684\u8acb\u6c42\u9650\u6d41\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u652f\u6301\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9748\u6d3b\u6027\u9ad8\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u591a\u7a2e\u6301\u4e45\u5316\u9078\u9805\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8php%e4%b8%ad%e4%bd%bf%e7%94%a8redis%e5%af%a6%e7%8f%be%e8%ab%8b%e6%b1%82%e9%99%90%e6%b5%81-HVywYaVvOu\">\u5982\u4f55\u5728PHP\u4e2d\u4f7f\u7528Redis\u5be6\u73fe\u8acb\u6c42\u9650\u6d41<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u5be6\u73fe\u8acb\u6c42\u9650\u6d41\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dredis%e5%92%8cphp%e6%93%b4%e5%b1%95-HVywYaVvOu\">1. \u5b89\u88ddRedis\u548cPHP\u64f4\u5c55<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u7684\u74b0\u5883\u4e2d\u5df2\u7d93\u5b89\u88dd\u4e86Redis\u670d\u52d9\u5668\u548cPHP\u7684Redis\u64f4\u5c55\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88ddRedis\uff1a<\/p>\n<pre><code>sudo apt-get install redis-server<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u5b89\u88ddPHP\u7684Redis\u64f4\u5c55\uff1a<\/p>\n<pre><code>sudo pecl install redis<\/code><\/pre>\n<h3 id=\"2-%e9%80%a3%e6%8e%a5redis-HVywYaVvOu\">2. \u9023\u63a5Redis<\/h3>\n<p>\u5728PHP\u4e2d\u9023\u63a5Redis\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>&lt;?php\n$redis = new Redis();\n$redis-&gt;connect('127.0.0.1', 6379);\n?&gt;<\/code><\/pre>\n<h3 id=\"3-%e5%af%a6%e7%8f%be%e8%ab%8b%e6%b1%82%e9%99%90%e6%b5%81%e9%82%8f%e8%bc%af-HVywYaVvOu\">3. \u5be6\u73fe\u8acb\u6c42\u9650\u6d41\u908f\u8f2f<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u8acb\u6c42\u9650\u6d41\u5be6\u73fe\u793a\u4f8b\uff0c\u9650\u5236\u6bcf\u500b\u7528\u6236\u6bcf\u5206\u9418\u6700\u591a\u8acb\u6c425\u6b21\uff1a<\/p>\n<pre><code>&lt;?php\nfunction rateLimit($userId) {\n    global $redis;\n    $key = \"rate_limit:\" . $userId;\n    $current = $redis-&gt;get($key);\n    \n    if ($current === false) {\n        \/\/ \u5982\u679c\u6c92\u6709\u8acb\u6c42\u8a18\u9304\uff0c\u8a2d\u7f6e\u521d\u59cb\u503c\n        $redis-&gt;set($key, 1, 60); \/\/ \u8a2d\u7f6e\u904e\u671f\u6642\u9593\u70ba60\u79d2\n        return true; \/\/ \u5141\u8a31\u8acb\u6c42\n    } elseif ($current incr($key);\n        return true; \/\/ \u5141\u8a31\u8acb\u6c42\n    } else {\n        \/\/ \u8d85\u904e\u9650\u5236\uff0c\u62d2\u7d55\u8acb\u6c42\n        return false; \/\/ \u62d2\u7d55\u8acb\u6c42\n    }\n}\n\n\/\/ \u4f7f\u7528\u793a\u4f8b\n$userId = 'user123';\nif (rateLimit($userId)) {\n    echo \"\u8acb\u6c42\u88ab\u5141\u8a31\";\n} else {\n    echo \"\u8acb\u6c42\u88ab\u62d2\u7d55\uff0c\u8d85\u904e\u9650\u6d41\u6b21\u6578\";\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HVywYaVvOu\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u5be6\u73fePHP\u4e2d\u7684\u8acb\u6c42\u9650\u6d41\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u7c21\u55ae\u7684\u4ee3\u78bc\u5be6\u73fe\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u7528\u6236\u7684\u8acb\u6c42\u983b\u7387\uff0c\u4fdd\u8b77\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u8acb\u6c42\u7684\u61c9\u7528\uff0c\u9019\u7a2e\u6280\u8853\u5c24\u70ba\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u5728PHP\u4e2d\u5be6\u73fe\u8acb\u6c42\u9650\u6d41\uff0c\u8f15\u9b06\u63a7\u5236API\u8a2a\u554f\u983b\u7387\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-184866","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=184866"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184866\/revisions"}],"predecessor-version":[{"id":184867,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184866\/revisions\/184867"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}