{"id":197572,"date":"2024-11-14T09:04:18","date_gmt":"2024-11-14T01:04:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/197572\/"},"modified":"2024-11-14T09:04:19","modified_gmt":"2024-11-14T01:04:19","slug":"%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e7%9a%84%e7%95%b0%e6%ad%a5%e9%99%90%e6%b5%81%e8%a7%a3%e8%80%a6%ef%bc%88redis-%e9%99%90%e6%b5%81%e7%95%b0%e6%ad%a5%e8%a7%a3%e8%80%a6%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/197572\/","title":{"rendered":"\u5229\u7528Redis\u5be6\u73fe\u9ad8\u6548\u7684\u7570\u6b65\u9650\u6d41\u89e3\u8026\uff08redis \u9650\u6d41\u7570\u6b65\u89e3\u8026\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e7%9a%84%e7%95%b0%e6%ad%a5%e9%99%90%e6%b5%81%e8%a7%a3%e8%80%a6-zzFHruWLpo\">\u5229\u7528Redis\u5be6\u73fe\u9ad8\u6548\u7684\u7570\u6b65\u9650\u6d41\u89e3\u8026<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u9650\u6d41\u662f\u4e00\u500b\u81f3\u95dc\u91cd\u8981\u7684\u6280\u8853\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u4e26\u767c\u7684\u74b0\u5883\u4e0b\u3002\u9650\u6d41\u7684\u4e3b\u8981\u76ee\u7684\u662f\u63a7\u5236\u7cfb\u7d71\u7684\u8ca0\u8f09\uff0c\u9632\u6b62\u56e0\u70ba\u904e\u591a\u7684\u8acb\u6c42\u800c\u5c0e\u81f4\u670d\u52d9\u5668\u5d29\u6f70\u6216\u6027\u80fd\u4e0b\u964d\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u7d50\u69cb\u548c\u64cd\u4f5c\uff0c\u80fd\u5920\u6709\u6548\u5730\u5be6\u73fe\u9650\u6d41\u529f\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u5be6\u73fe\u9ad8\u6548\u7684\u7570\u6b65\u9650\u6d41\u89e3\u8026\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%99%90%e6%b5%81%ef%bc%9f-zzFHruWLpo\">\u4ec0\u9ebc\u662f\u9650\u6d41\uff1f<\/h2>\n<p>\u9650\u6d41\u662f\u6307\u5c0d\u7cfb\u7d71\u7684\u8acb\u6c42\u9032\u884c\u63a7\u5236\uff0c\u4ee5\u9632\u6b62\u904e\u591a\u7684\u8acb\u6c42\u540c\u6642\u5230\u9054\u7cfb\u7d71\uff0c\u5f9e\u800c\u4fdd\u8b77\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u3002\u5e38\u898b\u7684\u9650\u6d41\u7b56\u7565\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u4ee4\u724c\u6876\u7b97\u6cd5\uff1a<\/strong>\u901a\u904e\u751f\u6210\u4ee4\u724c\u4f86\u63a7\u5236\u8acb\u6c42\u7684\u6578\u91cf\uff0c\u7576\u8acb\u6c42\u5230\u9054\u6642\uff0c\u53ea\u6709\u7372\u5f97\u4ee4\u724c\u7684\u8acb\u6c42\u624d\u80fd\u88ab\u8655\u7406\u3002<\/li>\n<li><strong>\u6f0f\u6876\u7b97\u6cd5\uff1a<\/strong>\u4ee5\u56fa\u5b9a\u7684\u901f\u7387\u8655\u7406\u8acb\u6c42\uff0c\u8d85\u904e\u901f\u7387\u7684\u8acb\u6c42\u5c07\u88ab\u4e1f\u68c4\u6216\u6392\u968a\u3002<\/li>\n<li><strong>\u8a08\u6578\u5668\u7b97\u6cd5\uff1a<\/strong>\u5728\u4e00\u5b9a\u6642\u9593\u5167\u9650\u5236\u8acb\u6c42\u7684\u6578\u91cf\uff0c\u8d85\u904e\u9650\u5236\u7684\u8acb\u6c42\u5c07\u88ab\u62d2\u7d55\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-zzFHruWLpo\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u500b\u9ad8\u6027\u80fd\u7684\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\u80fd\u5920\u652f\u6301\u6bcf\u79d2\u6578\u5341\u842c\u6b21\u7684\u8acb\u6c42\u3002<\/li>\n<li><strong>\u6301\u4e45\u6027\uff1a<\/strong>\u652f\u6301\u6578\u64da\u6301\u4e45\u5316\uff0c\u80fd\u5920\u5728\u91cd\u555f\u5f8c\u6062\u5fa9\u6578\u64da\u3002<\/li>\n<li><strong>\u8c50\u5bcc\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\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<\/ul>\n<h2 id=\"redis%e9%99%90%e6%b5%81%e7%9a%84%e5%af%a6%e7%8f%be-zzFHruWLpo\">Redis\u9650\u6d41\u7684\u5be6\u73fe<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5229\u7528Redis\u5be6\u73fe\u9650\u6d41\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e7%bd%ae%e9%99%90%e6%b5%81%e5%8f%83%e6%95%b8-zzFHruWLpo\">1. \u8a2d\u7f6e\u9650\u6d41\u53c3\u6578<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5b9a\u7fa9\u9650\u6d41\u7684\u53c3\u6578\uff0c\u4f8b\u5982\u6bcf\u79d2\u5141\u8a31\u7684\u8acb\u6c42\u6578\u91cf\uff1a<\/p>\n<pre><code>const LIMIT = 100; \/\/ \u6bcf\u79d2\u5141\u8a31\u7684\u8acb\u6c42\u6578\u91cf<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8redis%e7%9a%84%e5%8e%9f%e5%ad%90%e6%93%8d%e4%bd%9c-zzFHruWLpo\">2. \u4f7f\u7528Redis\u7684\u539f\u5b50\u64cd\u4f5c<\/h3>\n<p>\u5229\u7528Redis\u7684\u539f\u5b50\u64cd\u4f5c\u4f86\u5be6\u73fe\u9650\u6d41\u3002\u53ef\u4ee5\u4f7f\u7528Redis\u7684<code>INCR<\/code>\u547d\u4ee4\u4f86\u589e\u52a0\u8a08\u6578\u5668\uff0c\u4e26\u4f7f\u7528<code>EXPIRE<\/code>\u547d\u4ee4\u8a2d\u7f6e\u904e\u671f\u6642\u9593\uff1a<\/p>\n<pre><code>\nfunction isAllowed($userId) {\n    $current = Redis::incr(\"rate_limit:{$userId}\");\n    if ($current == 1) {\n        Redis::expire(\"rate_limit:{$userId}\", 1); \/\/ \u8a2d\u7f6e\u904e\u671f\u6642\u9593\u70ba1\u79d2\n    }\n    return $current &lt;= LIMIT;\n}\n<\/code><\/pre>\n<h3 id=\"3-%e7%95%b0%e6%ad%a5%e8%99%95%e7%90%86%e8%ab%8b%e6%b1%82-zzFHruWLpo\">3. \u7570\u6b65\u8655\u7406\u8acb\u6c42<\/h3>\n<p>\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528\u7570\u6b65\u8655\u7406\u53ef\u4ee5\u6709\u6548\u6e1b\u5c11\u8acb\u6c42\u7684\u5ef6\u9072\u3002\u53ef\u4ee5\u4f7f\u7528\u6d88\u606f\u968a\u5217\u4f86\u5be6\u73fe\u8acb\u6c42\u7684\u7570\u6b65\u8655\u7406\uff0c\u4e26\u5728\u8655\u7406\u8acb\u6c42\u6642\u9032\u884c\u9650\u6d41\u5224\u65b7\uff1a<\/p>\n<pre><code>\nfunction handleRequest($userId) {\n    if (isAllowed($userId)) {\n        \/\/ \u8655\u7406\u8acb\u6c42\n    } else {\n        \/\/ \u62d2\u7d55\u8acb\u6c42\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zzFHruWLpo\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u5be6\u73fe\u9ad8\u6548\u7684\u7570\u6b65\u9650\u6d41\u89e3\u8026\uff0c\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\uff0c\u9084\u80fd\u5920\u6709\u6548\u5730\u7ba1\u7406\u9ad8\u4e26\u767c\u8acb\u6c42\u3002\u901a\u904e\u5408\u7406\u8a2d\u7f6e\u9650\u6d41\u53c3\u6578\u548c\u4f7f\u7528Redis\u7684\u539f\u5b50\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u9650\u6d41\u529f\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u9ad8\u53ef\u7528\u6027\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u6280\u8853\u67b6\u69cb\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u5be6\u73fe\u9ad8\u6548\u7684\u7570\u6b65\u9650\u6d41\u89e3\u8026\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u8207\u7a69\u5b9a\u6027\uff0c\u512a\u5316\u8cc7\u6e90\u4f7f\u7528\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-197572","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197572","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=197572"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197572\/revisions"}],"predecessor-version":[{"id":197573,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197572\/revisions\/197573"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=197572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=197572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=197572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}