{"id":124214,"date":"2024-10-25T14:48:24","date_gmt":"2024-10-25T06:48:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/124214\/"},"modified":"2024-10-25T14:48:25","modified_gmt":"2024-10-25T06:48:25","slug":"redis-%e9%9b%86%e7%be%a4%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e9%99%90%e6%b5%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/124214\/","title":{"rendered":"Redis \u96c6\u7fa4\u5be6\u73fe\u9ad8\u6548\u9650\u6d41"},"content":{"rendered":"<h1 id=\"redis-%e9%9b%86%e7%be%a4%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e9%99%90%e6%b5%81-cZBEBLWdfg\">Redis \u96c6\u7fa4\u5be6\u73fe\u9ad8\u6548\u9650\u6d41<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u9650\u6d41\u6280\u8853\u6210\u70ba\u4e86\u4fdd\u8b77\u61c9\u7528\u7a0b\u5e8f\u548c\u670d\u52d9\u7684\u91cd\u8981\u624b\u6bb5\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u8acb\u6c42\u6d41\u91cf\uff0c\u907f\u514d\u670d\u52d9\u904e\u8f09\uff0c\u6210\u70ba\u4e86\u958b\u767c\u8005\u548c\u904b\u7dad\u4eba\u54e1\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\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\u9650\u6d41\u529f\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528 Redis \u96c6\u7fa4\u4f86\u5be6\u73fe\u9ad8\u6548\u7684\u9650\u6d41\u7b56\u7565\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%99%90%e6%b5%81%ef%bc%9f-cZBEBLWdfg\">\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\u5c0e\u81f4\u7cfb\u7d71\u6027\u80fd\u4e0b\u964d\u6216\u5d29\u6f70\u3002\u5e38\u898b\u7684\u9650\u6d41\u7b56\u7565\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u4ee4\u724c\u6876\u7b97\u6cd5<\/li>\n<li>\u6f0f\u6876\u7b97\u6cd5<\/li>\n<li>\u56fa\u5b9a\u7a97\u53e3\u8a08\u6578<\/li>\n<li>\u6ed1\u52d5\u7a97\u53e3\u8a08\u6578<\/li>\n<\/ul>\n<p>\u9019\u4e9b\u7b97\u6cd5\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u9078\u64c7\u5408\u9069\u7684\u9650\u6d41\u7b56\u7565\u5c0d\u65bc\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87-redis%ef%bc%9f-cZBEBLWdfg\">\u70ba\u4ec0\u9ebc\u9078\u64c7 Redis\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\u7684\u8b80\u5beb\u6027\u80fd\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>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis \u63d0\u4f9b\u4e86\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u65b9\u4fbf\u958b\u767c\u8005\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u985e\u578b\u3002<\/li>\n<li><strong>\u96c6\u7fa4\u652f\u6301\uff1a<\/strong>Redis \u652f\u6301\u96c6\u7fa4\u6a21\u5f0f\uff0c\u53ef\u4ee5\u8f15\u9b06\u64f4\u5c55\uff0c\u6eff\u8db3\u9ad8\u4e26\u767c\u7684\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"redis-%e9%9b%86%e7%be%a4%e9%99%90%e6%b5%81%e5%af%a6%e7%8f%be-cZBEBLWdfg\">Redis \u96c6\u7fa4\u9650\u6d41\u5be6\u73fe<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 Redis \u96c6\u7fa4\u5be6\u73fe\u9650\u6d41\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e5%ae%9a%e9%99%90%e6%b5%81%e5%8f%83%e6%95%b8-cZBEBLWdfg\">1. \u8a2d\u5b9a\u9650\u6d41\u53c3\u6578<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u8a2d\u5b9a\u9650\u6d41\u7684\u53c3\u6578\uff0c\u4f8b\u5982\u6bcf\u79d2\u5141\u8a31\u7684\u8acb\u6c42\u6578\u91cf\u548c\u6642\u9593\u7a97\u53e3\u3002\u5047\u8a2d\u6211\u5011\u5e0c\u671b\u6bcf\u500b\u7528\u6236\u6bcf\u79d2\u6700\u591a\u80fd\u767c\u9001 5 \u6b21\u8acb\u6c42\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8-redis-%e9%8d%b5%e5%80%bc%e5%ad%98%e5%84%b2%e8%ab%8b%e6%b1%82%e8%a8%88%e6%95%b8-cZBEBLWdfg\">2. \u4f7f\u7528 Redis \u9375\u503c\u5b58\u5132\u8acb\u6c42\u8a08\u6578<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 Redis \u7684\u5b57\u7b26\u4e32\u985e\u578b\u4f86\u5b58\u5132\u6bcf\u500b\u7528\u6236\u7684\u8acb\u6c42\u8a08\u6578\u3002\u4ee5\u4e0b\u662f\u793a\u7bc4\u4ee3\u78bc\uff1a<\/p>\n<pre><code>SET user:123:request_count 0 EX 1<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u7528\u6236 123 \u7684\u8acb\u6c42\u8a08\u6578\u521d\u59cb\u5316\u70ba 0\uff0c\u4e26\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u70ba 1 \u79d2\u3002<\/p>\n<h3 id=\"3-%e5%a2%9e%e5%8a%a0%e8%ab%8b%e6%b1%82%e8%a8%88%e6%95%b8-cZBEBLWdfg\">3. \u589e\u52a0\u8acb\u6c42\u8a08\u6578<\/h3>\n<p>\u6bcf\u7576\u7528\u6236\u767c\u9001\u8acb\u6c42\u6642\uff0c\u589e\u52a0\u8acb\u6c42\u8a08\u6578\uff1a<\/p>\n<pre><code>INCR user:123:request_count<\/code><\/pre>\n<p>\u5982\u679c\u8acb\u6c42\u8a08\u6578\u8d85\u904e\u9650\u5236\uff0c\u5247\u62d2\u7d55\u8a72\u8acb\u6c42\uff1a<\/p>\n<pre><code>if (GET user:123:request_count &gt; 5) {\n    \/\/ \u62d2\u7d55\u8acb\u6c42\n}<\/code><\/pre>\n<h3 id=\"4-%e4%bd%bf%e7%94%a8-lua-%e8%85%b3%e6%9c%ac%e9%80%b2%e8%a1%8c%e5%8e%9f%e5%ad%90%e6%93%8d%e4%bd%9c-cZBEBLWdfg\">4. \u4f7f\u7528 Lua \u8173\u672c\u9032\u884c\u539f\u5b50\u64cd\u4f5c<\/h3>\n<p>\u70ba\u4e86\u78ba\u4fdd\u8acb\u6c42\u8a08\u6578\u7684\u539f\u5b50\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528 Redis \u7684 Lua \u8173\u672c\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>local current_count = redis.call('INCR', KEYS[1])\nif current_count == 1 then\n    redis.call('EXPIRE', KEYS[1], 1)\nend\nif current_count &gt; tonumber(ARGV[1]) then\n    return false\nend\nreturn true<\/code><\/pre>\n<p>\u9019\u6bb5\u8173\u672c\u5c07\u8acb\u6c42\u8a08\u6578\u548c\u904e\u671f\u6642\u9593\u7684\u8a2d\u7f6e\u5408\u4f75\u5728\u4e00\u8d77\uff0c\u78ba\u4fdd\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-cZBEBLWdfg\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528 Redis \u96c6\u7fa4\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5be6\u73fe\u9ad8\u6548\u7684\u9650\u6d41\u7b56\u7565\uff0c\u4fdd\u8b77\u7cfb\u7d71\u514d\u53d7\u904e\u8f09\u5f71\u97ff\u3002Redis \u7684\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\u4f7f\u5176\u6210\u70ba\u9650\u6d41\u5be6\u73fe\u7684\u7406\u60f3\u9078\u64c7\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u9ad8\u4e26\u767c\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u642d\u914d Redis \u96c6\u7fa4\uff0c\u5c07\u80fd\u5920\u66f4\u597d\u5730\u61c9\u5c0d\u6d41\u91cf\u6311\u6230\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528 Redis \u96c6\u7fa4\u5be6\u73fe\u9ad8\u6548\u9650\u6d41\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u8207\u7a69\u5b9a\u6027\uff0c\u78ba\u4fdd\u8cc7\u6e90\u5408\u7406\u5206\u914d\u8207\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-124214","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/124214","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=124214"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/124214\/revisions"}],"predecessor-version":[{"id":124215,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/124214\/revisions\/124215"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=124214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=124214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=124214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}