{"id":158207,"date":"2024-11-03T00:54:45","date_gmt":"2024-11-02T16:54:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/158207\/"},"modified":"2024-11-03T00:54:47","modified_gmt":"2024-11-02T16:54:47","slug":"%e9%ab%98%e6%95%88%e5%af%a6%e7%8f%be%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e4%bf%a1%e6%81%af-redis-%e5%ad%98%e5%84%b2%ef%bc%88%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e5%ad%98%e5%85%a5-redis%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/158207\/","title":{"rendered":"\u9ad8\u6548\u5be6\u73fe\u7528\u6236\u767b\u9304\u4fe1\u606f Redis \u5b58\u5132\uff08\u7528\u6236\u767b\u9304\u5b58\u5165 Redis\uff09"},"content":{"rendered":"<h1 id=\"%e9%ab%98%e6%95%88%e5%af%a6%e7%8f%be%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e4%bf%a1%e6%81%af-redis-%e5%ad%98%e5%84%b2%ef%bc%88%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e5%ad%98%e5%85%a5-redis%ef%bc%89-LQSKYyBPZg\">\u9ad8\u6548\u5be6\u73fe\u7528\u6236\u767b\u9304\u4fe1\u606f Redis \u5b58\u5132\uff08\u7528\u6236\u767b\u9304\u5b58\u5165 Redis\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u7528\u6236\u767b\u9304\u4fe1\u606f\u7684\u7ba1\u7406\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u5b58\u5132\u548c\u6aa2\u7d22\u9019\u4e9b\u4fe1\u606f\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u8b80\u5beb\u901f\u5ea6\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0c\u6210\u70ba\u4e86\u5b58\u5132\u7528\u6236\u767b\u9304\u4fe1\u606f\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis-%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-LQSKYyBPZg\">Redis \u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u591a\u7a2e\u6578\u64da\u985e\u578b\u3002\u5b83\u7684\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9ad8\u6027\u80fd\uff1aRedis \u53ef\u4ee5\u6bcf\u79d2\u8655\u7406\u6578\u5341\u842c\u6b21\u7684\u8b80\u5beb\u64cd\u4f5c\u3002<\/li>\n<li>\u6301\u4e45\u5316\uff1a\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u907f\u514d\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li>\u9748\u6d3b\u6027\uff1a\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u65b9\u4fbf\u958b\u767c\u8005\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u5b58\u5132\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87-redis-%e5%ad%98%e5%84%b2%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e4%bf%a1%e6%81%af-LQSKYyBPZg\">\u70ba\u4ec0\u9ebc\u9078\u64c7 Redis \u5b58\u5132\u7528\u6236\u767b\u9304\u4fe1\u606f<\/h2>\n<p>\u4f7f\u7528 Redis \u5b58\u5132\u7528\u6236\u767b\u9304\u4fe1\u606f\u6709\u4ee5\u4e0b\u5e7e\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u5feb\u901f\u97ff\u61c9\uff1a<\/strong>\u7531\u65bc Redis \u5c07\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u6975\u5feb\uff0c\u80fd\u5920\u6eff\u8db3\u9ad8\u4e26\u767c\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis \u63d0\u4f9b\u4e86\u7c21\u55ae\u7684 API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u652f\u6301\u5206\u4f48\u5f0f\uff1a<\/strong>Redis \u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\u4ee5\u61c9\u5c0d\u66f4\u5927\u7684\u7528\u6236\u91cf\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e4%bf%a1%e6%81%af%e7%9a%84-redis-%e5%ad%98%e5%84%b2-LQSKYyBPZg\">\u5982\u4f55\u5be6\u73fe\u7528\u6236\u767b\u9304\u4fe1\u606f\u7684 Redis \u5b58\u5132<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Redis \u5b58\u5132\u7528\u6236\u767b\u9304\u4fe1\u606f\u3002\u5047\u8a2d\u6211\u5011\u4f7f\u7528 Node.js \u548c `ioredis` \u5eab\u4f86\u5be6\u73fe\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-ioredis-LQSKYyBPZg\">\u5b89\u88dd ioredis<\/h3>\n<pre><code>npm install ioredis<\/code><\/pre>\n<h3 id=\"%e9%80%a3%e6%8e%a5-redis-LQSKYyBPZg\">\u9023\u63a5 Redis<\/h3>\n<pre><code>const Redis = require('ioredis');\nconst redis = new Redis(); \/\/ \u9ed8\u8a8d\u9023\u63a5\u5230 localhost:6379<\/code><\/pre>\n<h3 id=\"%e5%ad%98%e5%84%b2%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e4%bf%a1%e6%81%af-LQSKYyBPZg\">\u5b58\u5132\u7528\u6236\u767b\u9304\u4fe1\u606f<\/h3>\n<pre><code>function storeUserLoginInfo(userId, loginInfo) {\n    \/\/ \u5c07\u7528\u6236\u767b\u9304\u4fe1\u606f\u5b58\u5165 Redis\uff0c\u4f7f\u7528 userId \u4f5c\u70ba\u9375\n    redis.set(userId, JSON.stringify(loginInfo), 'EX', 3600); \/\/ \u8a2d\u7f6e\u904e\u671f\u6642\u9593\u70ba 1 \u5c0f\u6642\n}<\/code><\/pre>\n<h3 id=\"%e6%aa%a2%e7%b4%a2%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e4%bf%a1%e6%81%af-LQSKYyBPZg\">\u6aa2\u7d22\u7528\u6236\u767b\u9304\u4fe1\u606f<\/h3>\n<pre><code>function getUserLoginInfo(userId) {\n    return redis.get(userId).then(data =&gt; {\n        if (data) {\n            return JSON.parse(data);\n        }\n        return null; \/\/ \u5982\u679c\u6c92\u6709\u627e\u5230\uff0c\u8fd4\u56de null\n    });\n}<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-LQSKYyBPZg\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u5b58\u5132\u7528\u6236\u767b\u9304\u4fe1\u606f\u6642\uff0c\u5b89\u5168\u6027\u662f\u4e0d\u53ef\u5ffd\u8996\u7684\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li>\u5c0d\u654f\u611f\u4fe1\u606f\u9032\u884c\u52a0\u5bc6\uff0c\u4f8b\u5982\u5bc6\u78bc\u548c\u500b\u4eba\u8b58\u5225\u4fe1\u606f\u3002<\/li>\n<li>\u8a2d\u7f6e\u5408\u7406\u7684\u904e\u671f\u6642\u9593\uff0c\u907f\u514d\u9577\u6642\u9593\u5b58\u5132\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u3002<\/li>\n<li>\u4f7f\u7528\u5b89\u5168\u7684\u9023\u63a5\uff08\u5982 TLS\uff09\u4f86\u4fdd\u8b77\u6578\u64da\u50b3\u8f38\u904e\u7a0b\u4e2d\u7684\u5b89\u5168\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-LQSKYyBPZg\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528 Redis \u5b58\u5132\u7528\u6236\u767b\u9304\u4fe1\u606f\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7cfb\u7d71\u7684\u6027\u80fd\uff0c\u9084\u80fd\u7c21\u5316\u6578\u64da\u7ba1\u7406\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u65bd\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u5229\u7528 Redis \u7684\u512a\u52e2\uff0c\u70ba\u7528\u6236\u63d0\u4f9b\u66f4\u5feb\u7684\u767b\u9304\u9ad4\u9a57\u3002\u82e5\u60a8\u9700\u8981\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9ad8\u6548\u5be6\u73fe\u7528\u6236\u767b\u9304\u4fe1\u606f\u7684Redis\u5b58\u5132\uff0c\u63d0\u5347\u767b\u9304\u901f\u5ea6\u8207\u6578\u64da\u5b89\u5168\uff0c\u7c21\u5316\u7528\u6236\u9ad4\u9a57\uff0c\u78ba\u4fdd\u5feb\u901f\u8a2a\u554f\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-158207","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/158207","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=158207"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/158207\/revisions"}],"predecessor-version":[{"id":158208,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/158207\/revisions\/158208"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=158207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=158207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=158207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}