{"id":184278,"date":"2024-11-10T11:53:19","date_gmt":"2024-11-10T03:53:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184278\/"},"modified":"2024-11-10T11:53:20","modified_gmt":"2024-11-10T03:53:20","slug":"%e4%bd%bf%e7%94%a8redis%e7%ae%a1%e7%90%86%e7%99%bb%e9%8c%84%e7%8b%80%e6%85%8b%e5%af%a6%e7%8f%be%e5%ae%89%e5%85%a8%e8%aa%8d%e8%ad%89%ef%bc%88redis%e7%99%bb%e9%8c%84%e7%8b%80%e6%85%8b%e6%a0%a1%e9%a9%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184278\/","title":{"rendered":"\u4f7f\u7528Redis\u7ba1\u7406\u767b\u9304\u72c0\u614b\u5be6\u73fe\u5b89\u5168\u8a8d\u8b49\uff08redis\u767b\u9304\u72c0\u614b\u6821\u9a57\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e7%ae%a1%e7%90%86%e7%99%bb%e9%8c%84%e7%8b%80%e6%85%8b%e5%af%a6%e7%8f%be%e5%ae%89%e5%85%a8%e8%aa%8d%e8%ad%89%ef%bc%88redis%e7%99%bb%e9%8c%84%e7%8b%80%e6%85%8b%e6%a0%a1%e9%a9%97-YpLEYGSmJL\">\u4f7f\u7528Redis\u7ba1\u7406\u767b\u9304\u72c0\u614b\u5be6\u73fe\u5b89\u5168\u8a8d\u8b49\uff08redis\u767b\u9304\u72c0\u614b\u6821\u9a57\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u5b89\u5168\u6027\u662f\u6bcf\u500b\u61c9\u7528\u7a0b\u5e8f\u548c\u7db2\u7ad9\u7684\u9996\u8981\u8003\u91cf\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u7528\u6236\u7684\u767b\u9304\u72c0\u614b\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\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\u80fd\u529b\u548c\u7c21\u55ae\u7684\u6578\u64da\u7d50\u69cb\uff0c\u6210\u70ba\u4e86\u7ba1\u7406\u767b\u9304\u72c0\u614b\u7684\u7406\u60f3\u9078\u64c7\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Redis\u4f86\u5be6\u73fe\u5b89\u5168\u7684\u767b\u9304\u72c0\u614b\u6821\u9a57\u3002<\/p>\n<h2 id=\"redis%e7%b0%a1%e4%bb%8b-YpLEYGSmJL\">Redis\u7c21\u4ecb<\/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\u7531\u65bc\u5176\u5728\u5167\u5b58\u4e2d\u64cd\u4f5c\u6578\u64da\uff0cRedis\u7684\u8b80\u5beb\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u9700\u8981\u9ad8\u6027\u80fd\u7684\u61c9\u7528\u5834\u666f\u4e2d\u5ee3\u53d7\u6b61\u8fce\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e4%be%86%e7%ae%a1%e7%90%86%e7%99%bb%e9%8c%84%e7%8b%80%e6%85%8b-YpLEYGSmJL\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f86\u7ba1\u7406\u767b\u9304\u72c0\u614b<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u6578\u64da\u5b58\u53d6\u901f\u5ea6\u6975\u5feb\uff0c\u80fd\u5920\u652f\u6301\u5927\u91cf\u7684\u4e26\u767c\u8acb\u6c42\uff0c\u9069\u5408\u9ad8\u6d41\u91cf\u7684\u61c9\u7528\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u7684\u5b58\u53d6\u548c\u7ba1\u7406\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u6578\u64da\u6301\u4e45\u5316\u7684\u9078\u9805\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u70ba\u670d\u52d9\u5668\u91cd\u555f\u800c\u4e1f\u5931\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e7%8f%be%e7%99%bb%e9%8c%84%e7%8b%80%e6%85%8b%e6%a0%a1%e9%a9%97%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-YpLEYGSmJL\">\u5be6\u73fe\u767b\u9304\u72c0\u614b\u6821\u9a57\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u4f7f\u7528Redis\u4f86\u7ba1\u7406\u767b\u9304\u72c0\u614b\u7684\u57fa\u672c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<h3 id=\"1-%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84-YpLEYGSmJL\">1. \u7528\u6236\u767b\u9304<\/h3>\n<p>\u7576\u7528\u6236\u6210\u529f\u767b\u9304\u5f8c\uff0c\u7cfb\u7d71\u9700\u8981\u751f\u6210\u4e00\u500b\u552f\u4e00\u7684\u6703\u8a71ID\uff0c\u4e26\u5c07\u5176\u5b58\u5132\u5728Redis\u4e2d\u3002\u9019\u500b\u6703\u8a71ID\u53ef\u4ee5\u662f\u7528\u6236\u7684ID\u52a0\u4e0a\u4e00\u500b\u96a8\u6a5f\u751f\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u4ee5\u78ba\u4fdd\u5176\u552f\u4e00\u6027\u3002<\/p>\n<pre><code>const sessionId = generateSessionId(userId);\nredisClient.set(sessionId, JSON.stringify(userData), 'EX', 3600); \/\/ \u8a2d\u7f6e\u904e\u671f\u6642\u9593\u70ba1\u5c0f\u6642\n<\/code><\/pre>\n<h3 id=\"2-%e6%a0%a1%e9%a9%97%e7%99%bb%e9%8c%84%e7%8b%80%e6%85%8b-YpLEYGSmJL\">2. \u6821\u9a57\u767b\u9304\u72c0\u614b<\/h3>\n<p>\u5728\u7528\u6236\u767c\u9001\u8acb\u6c42\u6642\uff0c\u7cfb\u7d71\u9700\u8981\u6aa2\u67e5\u8acb\u6c42\u4e2d\u662f\u5426\u5305\u542b\u6709\u6548\u7684\u6703\u8a71ID\u3002\u82e5\u6703\u8a71ID\u5b58\u5728\u4e14\u672a\u904e\u671f\uff0c\u5247\u8a8d\u70ba\u7528\u6236\u5df2\u767b\u9304\uff0c\u5426\u5247\u9700\u8981\u91cd\u65b0\u767b\u9304\u3002<\/p>\n<pre><code>redisClient.get(sessionId, (err, result) =&gt; {\n    if (err || !result) {\n        \/\/ \u767b\u9304\u72c0\u614b\u7121\u6548\uff0c\u8981\u6c42\u91cd\u65b0\u767b\u9304\n    } else {\n        \/\/ \u767b\u9304\u72c0\u614b\u6709\u6548\uff0c\u7e7c\u7e8c\u8655\u7406\u8acb\u6c42\n    }\n});\n<\/code><\/pre>\n<h3 id=\"3-%e7%94%a8%e6%88%b6%e7%99%bb%e5%87%ba-YpLEYGSmJL\">3. \u7528\u6236\u767b\u51fa<\/h3>\n<p>\u7576\u7528\u6236\u9078\u64c7\u767b\u51fa\u6642\uff0c\u7cfb\u7d71\u9700\u8981\u522a\u9664Redis\u4e2d\u7684\u6703\u8a71ID\uff0c\u4ee5\u78ba\u4fdd\u7528\u6236\u7684\u767b\u9304\u72c0\u614b\u88ab\u6e05\u9664\u3002<\/p>\n<pre><code>redisClient.del(sessionId);\n<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-YpLEYGSmJL\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u7ba1\u7406\u767b\u9304\u72c0\u614b\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u4ee5\u4e0b\u5b89\u5168\u6027\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u6703\u8a71\u52ab\u6301\uff1a<\/strong>\u4f7f\u7528HTTPS\u5354\u8b70\u4f86\u52a0\u5bc6\u6578\u64da\u50b3\u8f38\uff0c\u9632\u6b62\u6703\u8a71ID\u88ab\u7aca\u53d6\u3002<\/li>\n<li><strong>\u6703\u8a71\u904e\u671f\uff1a<\/strong>\u8a2d\u7f6e\u5408\u7406\u7684\u6703\u8a71\u904e\u671f\u6642\u9593\uff0c\u9632\u6b62\u9577\u6642\u9593\u672a\u4f7f\u7528\u7684\u6703\u8a71\u88ab\u6feb\u7528\u3002<\/li>\n<li><strong>IP\u5730\u5740\u7d81\u5b9a\uff1a<\/strong>\u53ef\u4ee5\u8003\u616e\u5c07\u6703\u8a71ID\u8207\u7528\u6236\u7684IP\u5730\u5740\u7d81\u5b9a\uff0c\u589e\u52a0\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-YpLEYGSmJL\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u4f86\u7ba1\u7406\u767b\u9304\u72c0\u614b\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u5b89\u5168\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u7c21\u55ae\u7684API\u548c\u9ad8\u6027\u80fd\u7684\u6578\u64da\u5b58\u53d6\u80fd\u529b\uff0cRedis\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u8f15\u9b06\u5be6\u73fe\u7528\u6236\u7684\u767b\u9304\u72c0\u614b\u6821\u9a57\u3002\u96a8\u8457\u7db2\u7d61\u5b89\u5168\u554f\u984c\u7684\u65e5\u76ca\u56b4\u91cd\uff0c\u9078\u64c7\u5408\u9069\u7684\u6280\u8853\u4f86\u4fdd\u8b77\u7528\u6236\u6578\u64da\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u89e3\u6c7a\u65b9\u6848\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u7ba1\u7406\u767b\u9304\u72c0\u614b\uff0c\u5be6\u73fe\u9ad8\u6548\u5b89\u5168\u7684\u8a8d\u8b49\u6a5f\u5236\uff0c\u78ba\u4fdd\u7528\u6236\u767b\u9304\u72c0\u614b\u7684\u53ef\u9760\u6821\u9a57\u8207\u7ba1\u7406\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-184278","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184278","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=184278"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184278\/revisions"}],"predecessor-version":[{"id":184279,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184278\/revisions\/184279"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}