{"id":184566,"date":"2024-11-10T14:17:19","date_gmt":"2024-11-10T06:17:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184566\/"},"modified":"2024-11-10T14:17:20","modified_gmt":"2024-11-10T06:17:20","slug":"%e4%bd%bf%e7%94%a8redis%e8%a7%a3%e6%b1%ba%e9%ab%98%e4%b8%a6%e7%99%bc%e5%95%8f%e9%a1%8c%ef%bc%88%e4%bd%bf%e7%94%a8redis%e8%99%95%e7%90%86%e9%ab%98%e4%b8%a6%e7%99%bc%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184566\/","title":{"rendered":"\u4f7f\u7528Redis\u89e3\u6c7a\u9ad8\u4e26\u767c\u554f\u984c\uff08\u4f7f\u7528redis\u8655\u7406\u9ad8\u4e26\u767c\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e8%a7%a3%e6%b1%ba%e9%ab%98%e4%b8%a6%e7%99%bc%e5%95%8f%e9%a1%8c%ef%bc%88%e4%bd%bf%e7%94%a8redis%e8%99%95%e7%90%86%e9%ab%98%e4%b8%a6%e7%99%bc%ef%bc%89-pduadkzHjI\">\u4f7f\u7528Redis\u89e3\u6c7a\u9ad8\u4e26\u767c\u554f\u984c\uff08\u4f7f\u7528Redis\u8655\u7406\u9ad8\u4e26\u767c\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\u548c\u61c9\u7528\u7a0b\u5e8f\u9700\u6c42\u7684\u63d0\u5347\uff0c\u9ad8\u4e26\u767c\u554f\u984c\u5df2\u6210\u70ba\u8a31\u591a\u4f01\u696d\u9762\u81e8\u7684\u6311\u6230\u3002\u9ad8\u4e26\u767c\u6307\u7684\u662f\u5728\u540c\u4e00\u6642\u9593\u5167\uff0c\u6709\u5927\u91cf\u7684\u8acb\u6c42\u540c\u6642\u767c\u9001\u5230\u4f3a\u670d\u5668\uff0c\u9019\u53ef\u80fd\u5c0e\u81f4\u4f3a\u670d\u5668\u904e\u8f09\u3001\u97ff\u61c9\u6642\u9593\u5ef6\u9072\uff0c\u751a\u81f3\u7cfb\u7d71\u5d29\u6f70\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\uff0cRedis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u89e3\u6c7a\u65b9\u6848\u4f86\u61c9\u5c0d\u9ad8\u4e26\u767c\u60c5\u6cc1\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%89%b9%e6%80%a7-pduadkzHjI\">Redis\u7684\u57fa\u672c\u7279\u6027<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5177\u6709\u4ee5\u4e0b\u5e7e\u500b\u986f\u8457\u7279\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u4ee5\u6bcf\u79d2\u6578\u5341\u842c\u6b21\u7684\u901f\u5ea6\u8655\u7406\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u7528\u65bc\u9ad8\u4e26\u767c\u5834\u666f\u3002<\/li>\n<li><strong>\u6578\u64da\u6301\u4e45\u5316\uff1a<\/strong>Redis\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u9019\u6a23\u5373\u4f7f\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\uff0c\u6578\u64da\u4e5f\u4e0d\u6703\u4e1f\u5931\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u9664\u4e86\u57fa\u672c\u7684\u9375\u503c\u5c0d\uff0cRedis\u9084\u652f\u6301\u5217\u8868\u3001\u96c6\u5408\u3001\u6709\u5e8f\u96c6\u5408\u3001\u54c8\u5e0c\u7b49\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u8655\u7406\u8907\u96dc\u6578\u64da\u6642\u66f4\u52a0\u9748\u6d3b\u3002<\/li>\n<li><strong>\u539f\u5b50\u64cd\u4f5c\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u591a\u7a2e\u539f\u5b50\u64cd\u4f5c\uff0c\u9019\u5c0d\u65bc\u9ad8\u4e26\u767c\u74b0\u5883\u4e0b\u7684\u6578\u64da\u4e00\u81f4\u6027\u81f3\u95dc\u91cd\u8981\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8redis%e8%a7%a3%e6%b1%ba%e9%ab%98%e4%b8%a6%e7%99%bc%e5%95%8f%e9%a1%8c%e7%9a%84%e7%ad%96%e7%95%a5-pduadkzHjI\">\u4f7f\u7528Redis\u89e3\u6c7a\u9ad8\u4e26\u767c\u554f\u984c\u7684\u7b56\u7565<\/h2>\n<h3 id=\"1-%e7%b7%a9%e5%ad%98%e6%a9%9f%e5%88%b6-pduadkzHjI\">1. \u7de9\u5b58\u6a5f\u5236<\/h3>\n<p>\u5728\u9ad8\u4e26\u767c\u5834\u666f\u4e2d\uff0c\u7de9\u5b58\u662f\u63d0\u9ad8\u7cfb\u7d71\u6027\u80fd\u7684\u6709\u6548\u624b\u6bb5\u3002\u901a\u904e\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u7de9\u5b58\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\u3002\u4f8b\u5982\uff0c\u7576\u7528\u6236\u8acb\u6c42\u67d0\u500b\u71b1\u9580\u5546\u54c1\u7684\u8a73\u7d30\u4fe1\u606f\u6642\uff0c\u53ef\u4ee5\u5148\u67e5\u8a62Redis\u7de9\u5b58\uff0c\u5982\u679c\u5b58\u5728\u5247\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5247\u518d\u67e5\u8a62\u6578\u64da\u5eab\u4e26\u5c07\u7d50\u679c\u5b58\u5165Redis\u3002<\/p>\n<pre><code>if (redis.exists(\"product:123\")) {\n    return redis.get(\"product:123\");\n} else {\n    product = db.query(\"SELECT * FROM products WHERE id = 123\");\n    redis.set(\"product:123\", product);\n    return product;\n}<\/code><\/pre>\n<h3 id=\"2-%e9%99%90%e6%b5%81%e7%ad%96%e7%95%a5-pduadkzHjI\">2. \u9650\u6d41\u7b56\u7565<\/h3>\n<p>\u5728\u9ad8\u4e26\u767c\u60c5\u6cc1\u4e0b\uff0c\u70ba\u4e86\u4fdd\u8b77\u5f8c\u7aef\u670d\u52d9\uff0c\u901a\u5e38\u9700\u8981\u5be6\u65bd\u9650\u6d41\u7b56\u7565\u3002Redis\u53ef\u4ee5\u7528\u4f86\u5be6\u73fe\u7c21\u55ae\u7684\u4ee4\u724c\u6876\u6216\u6f0f\u6876\u7b97\u6cd5\uff0c\u901a\u904e\u63a7\u5236\u8acb\u6c42\u7684\u901f\u7387\u4f86\u9632\u6b62\u7cfb\u7d71\u904e\u8f09\u3002<\/p>\n<pre><code>if (redis.incr(\"rate_limit:api_request\") &gt; max_requests) {\n    return \"Too many requests\";\n} else {\n    \/\/ \u8655\u7406\u8acb\u6c42\n}<\/code><\/pre>\n<h3 id=\"3-%e6%b6%88%e6%81%af%e9%9a%8a%e5%88%97-pduadkzHjI\">3. \u6d88\u606f\u968a\u5217<\/h3>\n<p>\u5728\u9ad8\u4e26\u767c\u5834\u666f\u4e2d\uff0c\u4f7f\u7528\u6d88\u606f\u968a\u5217\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u7cfb\u7d71\u7684\u77ac\u6642\u8ca0\u8f09\u3002Redis\u7684\u5217\u8868\u7d50\u69cb\u53ef\u4ee5\u7528\u4f86\u5be6\u73fe\u7c21\u55ae\u7684\u6d88\u606f\u968a\u5217\uff0c\u5c07\u8acb\u6c42\u653e\u5165\u968a\u5217\u4e2d\uff0c\u7136\u5f8c\u7531\u5de5\u4f5c\u9032\u7a0b\u9010\u6b65\u8655\u7406\u3002<\/p>\n<pre><code>redis.rpush(\"task_queue\", task);\ntask = redis.lpop(\"task_queue\");\n\/\/ \u8655\u7406task\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-pduadkzHjI\">\u7d50\u8ad6<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u89e3\u6c7a\u9ad8\u4e26\u767c\u554f\u984c\u7684\u7b56\u7565\uff0c\u5305\u62ec\u7de9\u5b58\u6a5f\u5236\u3001\u9650\u6d41\u7b56\u7565\u548c\u6d88\u606f\u968a\u5217\u7b49\u3002\u9019\u4e9b\u7279\u6027\u4f7f\u5f97Redis\u5728\u8655\u7406\u9ad8\u4e26\u767c\u8acb\u6c42\u6642\uff0c\u80fd\u5920\u6709\u6548\u63d0\u9ad8\u7cfb\u7d71\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u61c9\u7528\u6027\u80fd\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u61c9\u5c0d\u9ad8\u4e26\u767c\u554f\u984c\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u90e8\u7f72\u60a8\u7684Redis\u670d\u52d9\uff0c\u9019\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u60a8\u7684\u61c9\u7528\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Redis\u6709\u6548\u89e3\u6c7a\u9ad8\u4e26\u767c\u554f\u984c\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u8207\u97ff\u61c9\u901f\u5ea6\uff0c\u5be6\u73fe\u7a69\u5b9a\u7684\u6578\u64da\u8655\u7406\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-184566","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184566","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=184566"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184566\/revisions"}],"predecessor-version":[{"id":184567,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184566\/revisions\/184567"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}