{"id":167327,"date":"2024-11-06T00:15:26","date_gmt":"2024-11-05T16:15:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167327\/"},"modified":"2024-11-06T00:15:27","modified_gmt":"2024-11-05T16:15:27","slug":"%e5%88%a9%e7%94%a8redis%e7%b7%a9%e8%a7%a3%e9%ab%98%e4%b8%a6%e7%99%bc%ef%bc%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e8%8b%a6%e6%83%b1%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-redis%e8%a7%a3%e6%b1%ba%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167327\/","title":{"rendered":"\u5229\u7528Redis\u7de9\u89e3\u9ad8\u4e26\u767c\uff1a\u6578\u64da\u5eab\u82e6\u60f1\u89e3\u6c7a\u65b9\u6848 (redis\u89e3\u6c7a\u6578\u64da\u5eab\u9ad8\u4e26\u767c)"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e7%b7%a9%e8%a7%a3%e9%ab%98%e4%b8%a6%e7%99%bc%ef%bc%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e8%8b%a6%e6%83%b1%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-ZEUXSKyfDC\">\u5229\u7528Redis\u7de9\u89e3\u9ad8\u4e26\u767c\uff1a\u6578\u64da\u5eab\u82e6\u60f1\u89e3\u6c7a\u65b9\u6848<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u6578\u64da\u5eab\u9762\u81e8\u7684\u9ad8\u4e26\u767c\u8acb\u6c42\u4e5f\u96a8\u4e4b\u4e0a\u5347\uff0c\u9019\u5c0d\u65bc\u8a31\u591a\u4f01\u696d\u4f86\u8aaa\u662f\u4e00\u500b\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u7de9\u89e3\u9019\u4e9b\u9ad8\u4e26\u767c\u554f\u984c\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-ZEUXSKyfDC\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\uff08REmote DIctionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u5728\u5167\u5b58\u4e2d\u64cd\u4f5c\u6578\u64da\u7684\u7279\u6027\uff0cRedis\u80fd\u5920\u63d0\u4f9b\u6975\u5feb\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u9ad8\u4e26\u767c\u5834\u666f\u4e0b\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e9%ab%98%e4%b8%a6%e7%99%bc%e7%9a%84%e6%8c%91%e6%88%b0-ZEUXSKyfDC\">\u9ad8\u4e26\u767c\u7684\u6311\u6230<\/h2>\n<p>\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e2d\uff0c\u6578\u64da\u5eab\u7d93\u5e38\u6703\u9047\u5230\u4ee5\u4e0b\u5e7e\u500b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u74f6\u9838\uff1a<\/strong>\u96a8\u8457\u8acb\u6c42\u6578\u91cf\u7684\u589e\u52a0\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u53ef\u80fd\u6703\u4e0b\u964d\uff0c\u5c0e\u81f4\u97ff\u61c9\u6642\u9593\u5ef6\u9577\u3002<\/li>\n<li><strong>\u9396\u7af6\u722d\uff1a<\/strong>\u591a\u500b\u8acb\u6c42\u540c\u6642\u8a2a\u554f\u6578\u64da\u6642\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u9396\u7af6\u722d\uff0c\u9032\u4e00\u6b65\u5f71\u97ff\u6027\u80fd\u3002<\/li>\n<li><strong>\u6578\u64da\u4e00\u81f4\u6027\uff1a<\/strong>\u5728\u9ad8\u4e26\u767c\u60c5\u6cc1\u4e0b\uff0c\u5982\u4f55\u4fdd\u6301\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u662f\u4e00\u500b\u91cd\u8981\u6311\u6230\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-ZEUXSKyfDC\">Redis\u7684\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>Redis\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u5e7e\u7a2e\u65b9\u5f0f\u4f86\u7de9\u89e3\u9ad8\u4e26\u767c\u554f\u984c\uff1a<\/p>\n<h3 id=\"1-%e7%b7%a9%e5%ad%98%e6%a9%9f%e5%88%b6-ZEUXSKyfDC\">1. \u7de9\u5b58\u6a5f\u5236<\/h3>\n<p>Redis\u6700\u5e38\u898b\u7684\u7528\u9014\u4e4b\u4e00\u662f\u4f5c\u70ba\u7de9\u5b58\u5c64\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\u61c9\u7528\u7a0b\u5e8f\u53ef\u4ee5\u9996\u5148\u67e5\u8a62Redis\uff0c\u5982\u679c\u6578\u64da\u5b58\u5728\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5247\u518d\u67e5\u8a62\u6578\u64da\u5eab\u4e26\u5c07\u7d50\u679c\u7de9\u5b58\u5230Redis\u4e2d\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-ZEUXSKyfDC\">2. \u9650\u6d41<\/h3>\n<p>Redis\u9084\u53ef\u4ee5\u7528\u65bc\u5be6\u73fe\u9650\u6d41\u7b56\u7565\uff0c\u9632\u6b62\u7cfb\u7d71\u904e\u8f09\u3002\u901a\u904e\u4f7f\u7528Redis\u7684\u539f\u5b50\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u57fa\u65bc\u4ee4\u724c\u6876\u6216\u6f0f\u6876\u7b97\u6cd5\u7684\u9650\u6d41\u6a5f\u5236\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Redis\u7684INCR\u547d\u4ee4\u4f86\u8a08\u7b97\u8acb\u6c42\u6578\u91cf\uff0c\u4e26\u5728\u8d85\u904e\u9650\u5236\u6642\u62d2\u7d55\u8acb\u6c42\u3002<\/p>\n<pre><code>if (redis.incr(\"request_count\") &gt; limit) {\n    return \"Too many requests\";\n}<\/code><\/pre>\n<h3 id=\"3-%e6%b6%88%e6%81%af%e9%9a%8a%e5%88%97-ZEUXSKyfDC\">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\u6578\u64da\u5eab\u7684\u58d3\u529b\u3002Redis\u652f\u6301\u5217\u8868\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u7528\u4f5c\u7c21\u55ae\u7684\u6d88\u606f\u968a\u5217\u3002\u7576\u8acb\u6c42\u5230\u9054\u6642\uff0c\u53ef\u4ee5\u5c07\u8acb\u6c42\u653e\u5165Redis\u5217\u8868\u4e2d\uff0c\u7136\u5f8c\u7531\u5f8c\u7aef\u5de5\u4f5c\u8005\u9032\u884c\u8655\u7406\uff0c\u9019\u6a23\u53ef\u4ee5\u5e73\u6ed1\u5730\u8655\u7406\u9ad8\u4e26\u767c\u8acb\u6c42\u3002<\/p>\n<pre><code>redis.lpush(\"request_queue\", request_data);<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-ZEUXSKyfDC\">\u7d50\u8ad6<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u80fd\u5920\u6709\u6548\u5730\u7de9\u89e3\u9ad8\u4e26\u767c\u5e36\u4f86\u7684\u6311\u6230\u3002\u901a\u904e\u7de9\u5b58\u6a5f\u5236\u3001\u9650\u6d41\u548c\u6d88\u606f\u968a\u5217\u7b49\u7b56\u7565\uff0c\u4f01\u696d\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u7cfb\u7d71\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002\u5728\u9078\u64c7\u5408\u9069\u7684\u670d\u52d9\u5668\u67b6\u69cb\u6642\uff0c\u8003\u616e\u4f7f\u7528Redis\u4f5c\u70ba\u89e3\u6c7a\u65b9\u6848\u4e4b\u4e00\uff0c\u5c07\u6709\u52a9\u65bc\u61c9\u5c0d\u672a\u4f86\u7684\u9ad8\u4e26\u767c\u9700\u6c42\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc\u5982\u4f55\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c\u5176\u4ed6\u670d\u52d9\u4f86\u63d0\u5347\u60a8\u7684\u61c9\u7528\u6027\u80fd\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u7de9\u89e3\u9ad8\u4e26\u767c\u554f\u984c\uff0c\u63d0\u4f9b\u6709\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u63d0\u5347\u7cfb\u7d71\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-167327","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167327","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=167327"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167327\/revisions"}],"predecessor-version":[{"id":167328,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167327\/revisions\/167328"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}