{"id":114323,"date":"2024-10-24T12:48:48","date_gmt":"2024-10-24T04:48:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/114323\/"},"modified":"2024-10-24T12:48:48","modified_gmt":"2024-10-24T04:48:48","slug":"redis-%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f%e8%a8%82%e5%96%ae%e6%9f%a5%e8%a9%a2%e6%95%88%e7%8e%87","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/114323\/","title":{"rendered":"Redis \u5feb\u53d6\u52a0\u901f\u8a02\u55ae\u67e5\u8a62\u6548\u7387"},"content":{"rendered":"<h1 id=\"redis-%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f%e8%a8%82%e5%96%ae%e6%9f%a5%e8%a9%a2%e6%95%88%e7%8e%87-SfvYxIGofd\">Redis \u5feb\u53d6\u52a0\u901f\u8a02\u55ae\u67e5\u8a62\u6548\u7387<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u96fb\u5b50\u5546\u52d9\u74b0\u5883\u4e2d\uff0c\u8a02\u55ae\u67e5\u8a62\u7684\u6548\u7387\u5c0d\u65bc\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u548c\u696d\u52d9\u904b\u71df\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u5feb\u901f\u3001\u6e96\u78ba\u5730\u8655\u7406\u8a02\u55ae\u67e5\u8a62\u6210\u70ba\u4e86\u4f01\u696d\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u8a02\u55ae\u67e5\u8a62\u7684\u6548\u7387\uff0c\u672c\u6587\u5c07\u63a2\u8a0e\u5176\u5de5\u4f5c\u539f\u7406\u53ca\u5be6\u969b\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis%ef%bc%9f-SfvYxIGofd\">\u4ec0\u9ebc\u662f Redis\uff1f<\/h2>\n<p>Redis \u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\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\u5167\u5b58\u5b58\u5132\u7684\u7279\u6027\uff0cRedis \u63d0\u4f9b\u4e86\u6975\u5feb\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u5feb\u53d6\u7cfb\u7d71\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8-redis-%e9%80%b2%e8%a1%8c%e5%bf%ab%e5%8f%96%ef%bc%9f-SfvYxIGofd\">\u70ba\u4ec0\u9ebc\u4f7f\u7528 Redis \u9032\u884c\u5feb\u53d6\uff1f<\/h2>\n<p>\u4f7f\u7528 Redis \u9032\u884c\u5feb\u53d6\u7684\u4e3b\u8981\u539f\u56e0\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis \u7684\u6578\u64da\u64cd\u4f5c\u901f\u5ea6\u6975\u5feb\uff0c\u80fd\u5920\u5728\u6beb\u79d2\u7d1a\u5225\u5167\u5b8c\u6210\u67e5\u8a62\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u9019\u4f7f\u5f97 Redis \u80fd\u5920\u9748\u6d3b\u5730\u5b58\u5132\u548c\u6aa2\u7d22\u5404\u7a2e\u683c\u5f0f\u7684\u6578\u64da\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis \u63d0\u4f9b\u4e86\u591a\u7a2e\u6301\u4e45\u5316\u6a5f\u5236\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u70ba\u7cfb\u7d71\u5d29\u6f70\u800c\u4e1f\u5931\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\u67b6\u69cb\uff1a<\/strong>Redis \u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\u4ee5\u61c9\u5c0d\u9ad8\u6d41\u91cf\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"redis-%e5%9c%a8%e8%a8%82%e5%96%ae%e6%9f%a5%e8%a9%a2%e4%b8%ad%e7%9a%84%e6%87%89%e7%94%a8-SfvYxIGofd\">Redis \u5728\u8a02\u55ae\u67e5\u8a62\u4e2d\u7684\u61c9\u7528<\/h2>\n<p>\u5728\u96fb\u5b50\u5546\u52d9\u5e73\u53f0\u4e2d\uff0c\u8a02\u55ae\u67e5\u8a62\u901a\u5e38\u6d89\u53ca\u5927\u91cf\u7684\u6578\u64da\u6aa2\u7d22\u3002\u4f7f\u7528 Redis \u4f5c\u70ba\u5feb\u53d6\u5c64\uff0c\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u67e5\u8a62\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u9ad4\u7684\u61c9\u7528\u5834\u666f\uff1a<\/p>\n<h3 id=\"1-%e5%bf%ab%e5%8f%96%e8%a8%82%e5%96%ae%e6%95%b8%e6%93%9a-SfvYxIGofd\">1. \u5feb\u53d6\u8a02\u55ae\u6578\u64da<\/h3>\n<p>\u7576\u7528\u6236\u67e5\u8a62\u8a02\u55ae\u6642\uff0c\u7cfb\u7d71\u53ef\u4ee5\u9996\u5148\u6aa2\u67e5 Redis \u4e2d\u662f\u5426\u5b58\u5728\u8a72\u8a02\u55ae\u7684\u5feb\u53d6\u6578\u64da\u3002\u5982\u679c\u5b58\u5728\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u5feb\u53d6\u6578\u64da\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u67e5\u8a62\u3002<\/p>\n<pre><code>if (redis.exists(orderId)) {\n    return redis.get(orderId);\n} else {\n    order = database.query(orderId);\n    redis.set(orderId, order);\n    return order;\n}<\/code><\/pre>\n<h3 id=\"2-%e9%99%90%e5%88%b6%e6%9f%a5%e8%a9%a2%e9%a0%bb%e7%8e%87-SfvYxIGofd\">2. \u9650\u5236\u67e5\u8a62\u983b\u7387<\/h3>\n<p>\u5728\u9ad8\u6d41\u91cf\u7684\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528 Redis \u53ef\u4ee5\u9650\u5236\u7528\u6236\u7684\u67e5\u8a62\u983b\u7387\uff0c\u9632\u6b62\u7cfb\u7d71\u904e\u8f09\u3002\u901a\u904e\u8a2d\u7f6e\u67e5\u8a62\u7684\u904e\u671f\u6642\u9593\uff0c\u53ef\u4ee5\u6709\u6548\u63a7\u5236\u7528\u6236\u7684\u67e5\u8a62\u884c\u70ba\u3002<\/p>\n<pre><code>redis.setex(userId + \":queryCount\", 60, currentCount);<\/code><\/pre>\n<h3 id=\"3-%e6%95%b8%e6%93%9a%e9%81%8e%e6%9c%9f%e7%ae%a1%e7%90%86-SfvYxIGofd\">3. \u6578\u64da\u904e\u671f\u7ba1\u7406<\/h3>\n<p>\u8a02\u55ae\u6578\u64da\u7684\u6709\u6548\u6027\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u4f7f\u7528 Redis \u7684\u904e\u671f\u6a5f\u5236\u53ef\u4ee5\u81ea\u52d5\u6e05\u9664\u904e\u671f\u7684\u5feb\u53d6\u6578\u64da\uff0c\u4fdd\u6301\u7cfb\u7d71\u7684\u9ad8\u6548\u904b\u884c\u3002<\/p>\n<pre><code>redis.expire(orderId, 3600); \/\/ \u8a2d\u7f6e\u5feb\u53d6\u4e00\u5c0f\u6642\u5f8c\u904e\u671f<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-SfvYxIGofd\">\u7d50\u8ad6<\/h2>\n<p>Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u8a02\u55ae\u67e5\u8a62\u7684\u6548\u7387\u3002\u901a\u904e\u5408\u7406\u7684\u5feb\u53d6\u7b56\u7565\u548c\u6578\u64da\u7ba1\u7406\uff0c\u4f01\u696d\u53ef\u4ee5\u5728\u9ad8\u6d41\u91cf\u7684\u60c5\u6cc1\u4e0b\u4fdd\u6301\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u97ff\u61c9\u901f\u5ea6\u3002\u96a8\u8457\u96fb\u5b50\u5546\u52d9\u7684\u767c\u5c55\uff0c\u4f7f\u7528 Redis \u9032\u884c\u5feb\u53d6\u5c07\u6210\u70ba\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u7684\u91cd\u8981\u624b\u6bb5\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a> \u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347\u8a02\u55ae\u67e5\u8a62\u6548\u7387\uff0cRedis \u5feb\u53d6\u6280\u8853\u52a9\u60a8\u5feb\u901f\u5b58\u53d6\u6578\u64da\uff0c\u512a\u5316\u696d\u52d9\u6d41\u7a0b\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\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-114323","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/114323","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=114323"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/114323\/revisions"}],"predecessor-version":[{"id":114324,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/114323\/revisions\/114324"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=114323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=114323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=114323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}