{"id":114385,"date":"2024-10-24T12:53:50","date_gmt":"2024-10-24T04:53:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/114385\/"},"modified":"2024-10-24T12:53:50","modified_gmt":"2024-10-24T04:53:50","slug":"redis-%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f%e8%a6%96%e9%a0%bb%e4%b8%8b%e8%bc%89%e9%ab%94%e9%a9%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/114385\/","title":{"rendered":"Redis \u5feb\u53d6\u52a0\u901f\u8996\u983b\u4e0b\u8f09\u9ad4\u9a57"},"content":{"rendered":"<h1 id=\"redis-%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f%e8%a6%96%e9%a0%bb%e4%b8%8b%e8%bc%89%e9%ab%94%e9%a9%97-DJotrZgtjg\">Redis \u5feb\u53d6\u52a0\u901f\u8996\u983b\u4e0b\u8f09\u9ad4\u9a57<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u4f4d\u6642\u4ee3\uff0c\u8996\u983b\u5167\u5bb9\u7684\u9700\u6c42\u65e5\u76ca\u589e\u52a0\uff0c\u7121\u8ad6\u662f\u5728\u7dda\u4e32\u6d41\u5e73\u53f0\u9084\u662f\u793e\u4ea4\u5a92\u9ad4\uff0c\u8996\u983b\u4e0b\u8f09\u7684\u901f\u5ea6\u548c\u6548\u7387\u90fd\u6210\u70ba\u4e86\u7528\u6236\u9ad4\u9a57\u7684\u95dc\u9375\u56e0\u7d20\u3002\u70ba\u4e86\u63d0\u5347\u8996\u983b\u4e0b\u8f09\u7684\u901f\u5ea6\uff0c\u8a31\u591a\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u958b\u59cb\u63a1\u7528\u5feb\u53d6\u6280\u8853\uff0c\u5176\u4e2d Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u6b63\u9010\u6f38\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis%ef%bc%9f-DJotrZgtjg\">\u4ec0\u9ebc\u662f Redis\uff1f<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u901a\u5e38\u7528\u4f5c\u5feb\u53d6\u7cfb\u7d71\u3002\u5b83\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\uff0c\u4e26\u4e14\u5177\u6709\u6301\u4e45\u5316\u529f\u80fd\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u5167\u5b58\u5b58\u53d6\u80fd\u529b\uff0cRedis \u80fd\u5920\u986f\u8457\u63d0\u9ad8\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u7684\u901f\u5ea6\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u7a0b\u5e8f\u4f86\u8aaa\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2 id=\"redis-%e5%9c%a8%e8%a6%96%e9%a0%bb%e4%b8%8b%e8%bc%89%e4%b8%ad%e7%9a%84%e6%87%89%e7%94%a8-DJotrZgtjg\">Redis \u5728\u8996\u983b\u4e0b\u8f09\u4e2d\u7684\u61c9\u7528<\/h2>\n<p>\u5728\u8996\u983b\u4e0b\u8f09\u7684\u5834\u666f\u4e2d\uff0cRedis \u53ef\u4ee5\u7528\u4f86\u5feb\u53d6\u8996\u983b\u6578\u64da\uff0c\u5f9e\u800c\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\u4e26\u63d0\u9ad8\u7528\u6236\u7684\u4e0b\u8f09\u901f\u5ea6\u3002\u4ee5\u4e0b\u662f Redis \u5728\u8996\u983b\u4e0b\u8f09\u4e2d\u61c9\u7528\u7684\u5e7e\u500b\u95dc\u9375\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u6e1b\u5c11\u5ef6\u9072\uff1a<\/strong>\u901a\u904e\u5c07\u5e38\u7528\u7684\u8996\u983b\u6578\u64da\u5feb\u53d6\u5230 Redis \u4e2d\uff0c\u7528\u6236\u5728\u4e0b\u8f09\u6642\u53ef\u4ee5\u76f4\u63a5\u5f9e\u5feb\u53d6\u4e2d\u7372\u53d6\u6578\u64da\uff0c\u9019\u6a23\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u5f9e\u539f\u59cb\u4f3a\u670d\u5668\u7372\u53d6\u6578\u64da\u7684\u5ef6\u9072\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u4e26\u767c\u8655\u7406\u80fd\u529b\uff1a<\/strong>Redis \u7684\u9ad8\u6027\u80fd\u7279\u6027\u4f7f\u5176\u80fd\u5920\u540c\u6642\u8655\u7406\u5927\u91cf\u8acb\u6c42\uff0c\u9019\u5c0d\u65bc\u9ad8\u6d41\u91cf\u7684\u8996\u983b\u4e0b\u8f09\u5e73\u53f0\u5c24\u70ba\u91cd\u8981\u3002<\/li>\n<li><strong>\u52d5\u614b\u5feb\u53d6\u7b56\u7565\uff1a<\/strong>Redis \u652f\u6301\u591a\u7a2e\u5feb\u53d6\u7b56\u7565\uff0c\u5982 LRU\uff08\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff09\u548c LFU\uff08\u6700\u4e0d\u5e38\u4f7f\u7528\uff09\uff0c\u9019\u4f7f\u5f97\u7cfb\u7d71\u80fd\u5920\u6839\u64da\u5be6\u969b\u9700\u6c42\u52d5\u614b\u8abf\u6574\u5feb\u53d6\u5167\u5bb9\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e7%8f%be-redis-%e5%bf%ab%e5%8f%96%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-DJotrZgtjg\">\u5be6\u73fe Redis \u5feb\u53d6\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u8981\u5728\u8996\u983b\u4e0b\u8f09\u4e2d\u5be6\u73fe Redis \u5feb\u53d6\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>1. \u5b89\u88dd Redis\uff1a\n   \u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Redis\uff1a\n   sudo apt-get install redis-server\n\n2. \u914d\u7f6e Redis\uff1a\n   \u7de8\u8f2f Redis \u914d\u7f6e\u6587\u4ef6\uff0c\u8a2d\u7f6e\u5feb\u53d6\u5927\u5c0f\u548c\u904e\u671f\u6642\u9593\u7b49\u53c3\u6578\u3002\n\n3. \u96c6\u6210 Redis \u5ba2\u6236\u7aef\uff1a\n   \u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210 Redis \u5ba2\u6236\u7aef\u5eab\uff0c\u4f8b\u5982\u4f7f\u7528 Node.js \u7684 ioredis\uff1a\n   const Redis = require('ioredis');\n   const redis = new Redis();\n\n4. \u5be6\u73fe\u5feb\u53d6\u908f\u8f2f\uff1a\n   \u5728\u8996\u983b\u4e0b\u8f09\u8acb\u6c42\u4e2d\uff0c\u9996\u5148\u6aa2\u67e5 Redis \u4e2d\u662f\u5426\u5b58\u5728\u5feb\u53d6\u6578\u64da\uff1a\n   redis.get(videoId, (err, result) =&gt; {\n       if (result) {\n           \/\/ \u5f9e\u5feb\u53d6\u4e2d\u8fd4\u56de\u6578\u64da\n           return sendResponse(result);\n       } else {\n           \/\/ \u5f9e\u4f3a\u670d\u5668\u7372\u53d6\u6578\u64da\u4e26\u5feb\u53d6\n           fetchVideoFromServer(videoId).then(videoData =&gt; {\n               redis.set(videoId, videoData);\n               sendResponse(videoData);\n           });\n       }\n   });\n<\/code><\/pre>\n<h2 id=\"%e6%a1%88%e4%be%8b%e5%88%86%e6%9e%90-DJotrZgtjg\">\u6848\u4f8b\u5206\u6790<\/h2>\n<p>\u8a31\u591a\u77e5\u540d\u7684\u8996\u983b\u5e73\u53f0\u5df2\u7d93\u958b\u59cb\u4f7f\u7528 Redis \u4f86\u63d0\u5347\u5176\u670d\u52d9\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u67d0\u4e9b\u6d41\u884c\u7684\u4e32\u6d41\u670d\u52d9\u5728\u9ad8\u5cf0\u6642\u6bb5\u4f7f\u7528 Redis \u5feb\u53d6\u4f86\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\uff0c\u5f9e\u800c\u78ba\u4fdd\u7528\u6236\u80fd\u5920\u6d41\u66a2\u5730\u4e0b\u8f09\u548c\u89c0\u770b\u8996\u983b\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u7528\u6236\u6eff\u610f\u5ea6\uff0c\u9084\u964d\u4f4e\u4e86\u4f3a\u670d\u5668\u7684\u904b\u884c\u6210\u672c\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-DJotrZgtjg\">\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\u8996\u983b\u4e0b\u8f09\u7684\u901f\u5ea6\u548c\u7528\u6236\u9ad4\u9a57\u3002\u901a\u904e\u5408\u7406\u7684\u5feb\u53d6\u7b56\u7565\u548c\u5be6\u73fe\u65b9\u5f0f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u5ef6\u9072\uff0c\u63d0\u9ad8\u4e26\u767c\u8655\u7406\u80fd\u529b\uff0c\u5f9e\u800c\u6eff\u8db3\u65e5\u76ca\u589e\u9577\u7684\u8996\u983b\u5167\u5bb9\u9700\u6c42\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u5176\u8996\u983b\u4e0b\u8f09\u6027\u80fd\u7684\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u4f86\u8aaa\uff0cRedis \u7121\u7591\u662f\u4e00\u500b\u503c\u5f97\u8003\u616e\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u548c\u5176\u4ed6\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347\u8996\u983b\u4e0b\u8f09\u901f\u5ea6\uff0cRedis \u5feb\u53d6\u6280\u8853\u52a9\u60a8\u5feb\u901f\u7372\u53d6\u5167\u5bb9\uff0c\u4eab\u53d7\u6d41\u66a2\u7684\u89c0\u770b\u9ad4\u9a57\uff0c\u7121\u9700\u7b49\u5f85\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-114385","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/114385","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=114385"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/114385\/revisions"}],"predecessor-version":[{"id":114386,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/114385\/revisions\/114386"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=114385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=114385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=114385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}