{"id":189150,"date":"2024-11-12T05:05:21","date_gmt":"2024-11-11T21:05:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/189150\/"},"modified":"2024-11-12T05:05:22","modified_gmt":"2024-11-11T21:05:22","slug":"%e6%8f%90%e5%8d%87%e6%95%88%e7%8e%87%ef%bc%8c%e4%bd%bf%e7%94%a8redis%e5%af%a6%e7%8f%be%e8%ae%80%e5%af%ab%e5%bf%ab%e5%8f%96%ef%bc%88redis%e8%ae%80%e5%af%ab%e5%bf%ab%e5%8f%96%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/189150\/","title":{"rendered":"\u63d0\u5347\u6548\u7387\uff0c\u4f7f\u7528Redis\u5be6\u73fe\u8b80\u5beb\u5feb\u53d6\uff08redis\u8b80\u5beb\u5feb\u53d6\uff09"},"content":{"rendered":"<h1 id=\"%e6%8f%90%e5%8d%87%e6%95%88%e7%8e%87%ef%bc%8c%e4%bd%bf%e7%94%a8redis%e5%af%a6%e7%8f%be%e8%ae%80%e5%af%ab%e5%bf%ab%e5%8f%96-HzNavetZIq\">\u63d0\u5347\u6548\u7387\uff0c\u4f7f\u7528Redis\u5be6\u73fe\u8b80\u5beb\u5feb\u53d6<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u5c0d\u65bc\u7528\u6236\u9ad4\u9a57\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u5b58\u53d6\u6578\u64da\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u8b80\u5beb\u5feb\u53d6\u529f\u80fd\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-HzNavetZIq\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\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\u5b83\u7684\u4e3b\u8981\u7279\u9ede\u662f\u5c07\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u6975\u5feb\u3002Redis\u9084\u652f\u6301\u6301\u4e45\u5316\uff0c\u53ef\u4ee5\u5c07\u6578\u64da\u5feb\u7167\u4fdd\u5b58\u5230\u78c1\u789f\u4e2d\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8redis%e4%bd%9c%e7%82%ba%e8%ae%80%e5%af%ab%e5%bf%ab%e5%8f%96%ef%bc%9f-HzNavetZIq\">\u70ba\u4ec0\u9ebc\u4f7f\u7528Redis\u4f5c\u70ba\u8b80\u5beb\u5feb\u53d6\uff1f<\/h2>\n<p>\u4f7f\u7528Redis\u4f5c\u70ba\u8b80\u5beb\u5feb\u53d6\u6709\u591a\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u901a\u5e38\u5728\u5fae\u79d2\u7d1a\u5225\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u7528\u65bc\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u5834\u666f\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u4e0d\u50c5\u652f\u6301\u7c21\u55ae\u7684\u9375\u503c\u5c0d\uff0c\u9084\u652f\u6301\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9748\u6d3b\u9078\u64c7\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u7684API\u8a2d\u8a08\u7c21\u55ae\uff0c\u6613\u65bc\u96c6\u6210\u5230\u5404\u7a2e\u61c9\u7528\u4e2d\uff0c\u4e26\u4e14\u6709\u8c50\u5bcc\u7684\u6587\u6a94\u548c\u793e\u5340\u652f\u6301\u3002<\/li>\n<li><strong>\u53ef\u64f4\u5c55\u6027\uff1a<\/strong>Redis\u652f\u6301\u5206\u7247\u548c\u96c6\u7fa4\u6a21\u5f0f\uff0c\u53ef\u4ee5\u8f15\u9b06\u64f4\u5c55\u4ee5\u61c9\u5c0d\u66f4\u9ad8\u7684\u8ca0\u8f09\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%beredis%e8%ae%80%e5%af%ab%e5%bf%ab%e5%8f%96-HzNavetZIq\">\u5982\u4f55\u5be6\u73feRedis\u8b80\u5beb\u5feb\u53d6<\/h2>\n<p>\u5be6\u73feRedis\u8b80\u5beb\u5feb\u53d6\u7684\u57fa\u672c\u601d\u8def\u662f\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u5b58\u5132\u5728Redis\u4e2d\uff0c\u5f9e\u800c\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8a2a\u554f\u6b21\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Node.js\u4e2d\u4f7f\u7528Redis\u4f5c\u70ba\u5feb\u53d6\u5c64\uff1a<\/p>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\nclient.on('error', (err) =&gt; {\n    console.log('Redis error: ' + err);\n});\n\n\/\/ \u5047\u8a2d\u6709\u4e00\u500b\u51fd\u6578\u7528\u65bc\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u7528\u6236\u4fe1\u606f\nfunction getUserFromDatabase(userId) {\n    \/\/ \u6a21\u64ec\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u6578\u64da\n    return { id: userId, name: 'User ' + userId };\n}\n\n\/\/ \u7372\u53d6\u7528\u6236\u4fe1\u606f\u7684\u51fd\u6578\nfunction getUser(userId) {\n    return new Promise((resolve, reject) =&gt; {\n        \/\/ \u5148\u67e5\u8a62Redis\u5feb\u53d6\n        client.get(userId, (err, result) =&gt; {\n            if (err) reject(err);\n            if (result) {\n                \/\/ \u5982\u679c\u5feb\u53d6\u4e2d\u6709\u6578\u64da\uff0c\u76f4\u63a5\u8fd4\u56de\n                resolve(JSON.parse(result));\n            } else {\n                \/\/ \u5982\u679c\u5feb\u53d6\u4e2d\u6c92\u6709\u6578\u64da\uff0c\u5f9e\u6578\u64da\u5eab\u7372\u53d6\n                const user = getUserFromDatabase(userId);\n                \/\/ \u5c07\u6578\u64da\u5b58\u5165Redis\u5feb\u53d6\n                client.setex(userId, 3600, JSON.stringify(user)); \/\/ \u8a2d\u7f6e\u5feb\u53d6\u6709\u6548\u671f\u70ba1\u5c0f\u6642\n                resolve(user);\n            }\n        });\n    });\n}\n\n\/\/ \u4f7f\u7528\u793a\u4f8b\ngetUser('1').then(user =&gt; {\n    console.log(user);\n}).catch(err =&gt; {\n    console.error(err);\n});<\/code><\/pre>\n<h2 id=\"%e5%bf%ab%e5%8f%96%e7%ad%96%e7%95%a5-HzNavetZIq\">\u5feb\u53d6\u7b56\u7565<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u4f5c\u70ba\u5feb\u53d6\u6642\uff0c\u9078\u64c7\u5408\u9069\u7684\u5feb\u53d6\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002\u5e38\u898b\u7684\u5feb\u53d6\u7b56\u7565\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>LRU\uff08Least Recently Used\uff09\uff1a<\/strong>\u7576\u5feb\u53d6\u6eff\u6642\uff0c\u512a\u5148\u6dd8\u6c70\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6578\u64da\u3002<\/li>\n<li><strong>TTL\uff08Time To Live\uff09\uff1a<\/strong>\u70ba\u6bcf\u500b\u5feb\u53d6\u9805\u8a2d\u7f6e\u6709\u6548\u671f\uff0c\u904e\u671f\u5f8c\u81ea\u52d5\u522a\u9664\u3002<\/li>\n<li><strong>\u5beb\u5165\u7a7f\u900f\uff1a <\/strong>\u5c0d\u65bc\u4e0d\u5b58\u5728\u7684\u6578\u64da\uff0c\u907f\u514d\u6bcf\u6b21\u90fd\u67e5\u8a62\u5f8c\u7aef\u6578\u64da\u5eab\uff0c\u53ef\u4ee5\u8a2d\u7f6e\u4e00\u500b\u7a7a\u503c\u5feb\u53d6\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HzNavetZIq\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u8b80\u5beb\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u3002\u901a\u904e\u5408\u7406\u7684\u5feb\u53d6\u7b56\u7565\u548c\u7c21\u55ae\u7684\u5be6\u73fe\u65b9\u5f0f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8a2a\u554f\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u5347\u6574\u9ad4\u7cfb\u7d71\u7684\u6548\u7387\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72Redis\uff0cServer.HK\u63d0\u4f9b\u4e86\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0c\u52a9\u60a8\u8f15\u9b06\u5be6\u73fe\u9ad8\u6027\u80fd\u61c9\u7528\u3002\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347\u6548\u7387\uff0c\u900f\u904eRedis\u5be6\u73fe\u9ad8\u6548\u8b80\u5beb\u5feb\u53d6\uff0c\u512a\u5316\u6578\u64da\u5b58\u53d6\u901f\u5ea6\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\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-189150","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189150","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=189150"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189150\/revisions"}],"predecessor-version":[{"id":189151,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189150\/revisions\/189151"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=189150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=189150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=189150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}