{"id":189142,"date":"2024-11-12T05:01:21","date_gmt":"2024-11-11T21:01:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/189142\/"},"modified":"2024-11-12T05:01:22","modified_gmt":"2024-11-11T21:01:22","slug":"%e6%8f%90%e5%8d%87%e7%b3%bb%e7%b5%b1%e6%80%a7%e8%83%bd%e7%94%a8redis%e5%bf%ab%e5%8f%96%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88%ef%bc%88redis%e5%bf%ab%e5%8f%96%e6%95%b4%e5%80%8b%e6%96%b9%e6%b3%95%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/189142\/","title":{"rendered":"\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u7528Redis\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff08redis\u5feb\u53d6\u6574\u500b\u65b9\u6cd5\uff09"},"content":{"rendered":"<h1 id=\"%e6%8f%90%e5%8d%87%e7%b3%bb%e7%b5%b1%e6%80%a7%e8%83%bd%e7%94%a8redis%e5%bf%ab%e5%8f%96%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88%ef%bc%88redis%e5%bf%ab%e5%8f%96%e6%95%b4%e5%80%8b%e6%96%b9%e6%b3%95%ef%bc%89-QpnfqHWBOT\">\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u7528Redis\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff08redis\u5feb\u53d6\u6574\u500b\u65b9\u6cd5\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7cfb\u7d71\u6027\u80fd\u7684\u512a\u5316\u5df2\u6210\u70ba\u4f01\u696d\u6210\u529f\u7684\u95dc\u9375\u56e0\u7d20\u4e4b\u4e00\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\u548c\u6578\u64da\u91cf\u7684\u6fc0\u589e\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u5b58\u53d6\u6578\u64da\u6210\u70ba\u4e86\u6280\u8853\u4eba\u54e1\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\u7cfb\u7d71\u6027\u80fd\uff0c\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eRedis\u7684\u5feb\u53d6\u65b9\u6cd5\u53ca\u5176\u5728\u7cfb\u7d71\u6027\u80fd\u63d0\u5347\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-QpnfqHWBOT\">\u4ec0\u9ebc\u662fRedis\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\u80fd\u5920\u63d0\u4f9b\u6975\u5feb\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis%e5%bf%ab%e5%8f%96%e7%9a%84%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86-QpnfqHWBOT\">Redis\u5feb\u53d6\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>Redis\u5feb\u53d6\u7684\u57fa\u672c\u539f\u7406\u662f\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u5f9e\u800c\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8a2a\u554f\u6b21\u6578\u3002\u7576\u7528\u6236\u8acb\u6c42\u6578\u64da\u6642\uff0c\u7cfb\u7d71\u9996\u5148\u6aa2\u67e5Redis\u5feb\u53d6\u4e2d\u662f\u5426\u5b58\u5728\u8a72\u6578\u64da\uff0c\u5982\u679c\u5b58\u5728\u5247\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5247\u5f9e\u5f8c\u7aef\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\u4e26\u5c07\u5176\u5b58\u5165Redis\u5feb\u53d6\u4e2d\u3002<\/p>\n<h3 id=\"%e5%bf%ab%e5%8f%96%e6%b5%81%e7%a8%8b%e7%a4%ba%e6%84%8f%e5%9c%96-QpnfqHWBOT\">\u5feb\u53d6\u6d41\u7a0b\u793a\u610f\u5716<\/h3>\n<pre>\n\u7528\u6236\u8acb\u6c42 -&gt; \u6aa2\u67e5Redis\u5feb\u53d6 -&gt; \n  \u251c\u2500\u2500 \u5b58\u5728 -&gt; \u8fd4\u56de\u6578\u64da\n  \u2514\u2500\u2500 \u4e0d\u5b58\u5728 -&gt; \u5f9e\u6578\u64da\u5eab\u7372\u53d6 -&gt; \u5b58\u5165Redis\u5feb\u53d6 -&gt; \u8fd4\u56de\u6578\u64da\n<\/pre>\n<h2 id=\"redis%e5%bf%ab%e5%8f%96%e7%9a%84%e5%84%aa%e5%8b%a2-QpnfqHWBOT\">Redis\u5feb\u53d6\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5176\u80fd\u5920\u63d0\u4f9b\u6beb\u79d2\u7d1a\u7684\u97ff\u61c9\u6642\u9593\uff0c\u986f\u8457\u63d0\u9ad8\u7cfb\u7d71\u7684\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u80fd\u5920\u6eff\u8db3\u4e0d\u540c\u61c9\u7528\u5834\u666f\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis\u63d0\u4f9b\u591a\u7a2e\u6301\u4e45\u5316\u6a5f\u5236\uff0c\u80fd\u5920\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\u6062\u5fa9\u6578\u64da\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\u67b6\u69cb\uff1a<\/strong>\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=\"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%beredis%e5%bf%ab%e5%8f%96-QpnfqHWBOT\">\u5982\u4f55\u5be6\u73feRedis\u5feb\u53d6<\/h2>\n<p>\u5be6\u73feRedis\u5feb\u53d6\u7684\u904e\u7a0b\u53ef\u4ee5\u5206\u70ba\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dredis-QpnfqHWBOT\">1. \u5b89\u88ddRedis<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5728\u4f3a\u670d\u5668\u4e0a\u5b89\u88ddRedis\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5728Ubuntu\u4e0a\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install redis-server<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%aeredis-QpnfqHWBOT\">2. \u914d\u7f6eRedis<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u9700\u8981\u5c0dRedis\u9032\u884c\u57fa\u672c\u914d\u7f6e\u3002\u53ef\u4ee5\u7de8\u8f2f\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/redis\/redis.conf<\/code><\/pre>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u8a2d\u7f6e\u5feb\u53d6\u7684\u904e\u671f\u6642\u9593\u3001\u6700\u5927\u5167\u5b58\u4f7f\u7528\u91cf\u7b49\u53c3\u6578\u3002<\/p>\n<h3 id=\"3-%e9%9b%86%e6%88%90redis%e5%88%b0%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f-QpnfqHWBOT\">3. \u96c6\u6210Redis\u5230\u61c9\u7528\u7a0b\u5e8f<\/h3>\n<p>\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210Redis\uff0c\u901a\u5e38\u4f7f\u7528\u76f8\u61c9\u7684\u5ba2\u6236\u7aef\u5eab\u3002\u4f8b\u5982\uff0c\u5728Node.js\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u9023\u63a5Redis\uff1a<\/p>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\nclient.on('error', (err) =&gt; {\n    console.log('Error ' + err);\n});<\/code><\/pre>\n<h3 id=\"4-%e5%af%a6%e7%8f%be%e5%bf%ab%e5%8f%96%e9%82%8f%e8%bc%af-QpnfqHWBOT\">4. \u5be6\u73fe\u5feb\u53d6\u908f\u8f2f<\/h3>\n<p>\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u5be6\u73fe\u5feb\u53d6\u908f\u8f2f\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>function getData(key) {\n    return new Promise((resolve, reject) =&gt; {\n        client.get(key, (err, data) =&gt; {\n            if (err) reject(err);\n            if (data) {\n                resolve(JSON.parse(data)); \/\/ \u8fd4\u56de\u5feb\u53d6\u6578\u64da\n            } else {\n                \/\/ \u5f9e\u6578\u64da\u5eab\u7372\u53d6\u6578\u64da\n                const dbData = fetchFromDatabase(key);\n                client.setex(key, 3600, JSON.stringify(dbData)); \/\/ \u5b58\u5165\u5feb\u53d6\n                resolve(dbData);\n            }\n        });\n    });\n}<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-QpnfqHWBOT\">\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\u7cfb\u7d71\u6027\u80fd\uff0c\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u3002\u901a\u904e\u5408\u7406\u7684\u914d\u7f6e\u548c\u96c6\u6210\uff0c\u4f01\u696d\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u5b58\u53d6\u6578\u64da\uff0c\u5f9e\u800c\u63d0\u9ad8\u7528\u6236\u9ad4\u9a57\u548c\u7cfb\u7d71\u7a69\u5b9a\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u7684\u61c9\u7528\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\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u53ca\u5176\u76f8\u95dc\u670d\u52d9\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u7684Redis\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u7c21\u5316\u6578\u64da\u5b58\u53d6\uff0c\u512a\u5316\u97ff\u61c9\u901f\u5ea6\uff0c\u52a9\u529b\u9ad8\u6548\u80fd\u61c9\u7528\u958b\u767c\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-189142","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189142","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=189142"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189142\/revisions"}],"predecessor-version":[{"id":189143,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189142\/revisions\/189143"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=189142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=189142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=189142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}