{"id":5055,"date":"2024-11-14T09:34:24","date_gmt":"2024-11-14T01:34:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5055\/"},"modified":"2024-11-14T09:34:24","modified_gmt":"2024-11-14T01:34:24","slug":"redis%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%e5%bd%93%e5%89%8d%e7%94%a8%e6%88%b7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5055\/","title":{"rendered":"redis\u600e\u4e48\u83b7\u53d6\u5f53\u524d\u7528\u6237"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 redis \u83b7\u53d6\u5f53\u524d\u7528\u6237\uff0c\u60a8\u53ef\u4ee5\uff1a\u521b\u5efa\u4f1a\u8bdd\uff0c\u6bcf\u4e2a\u7528\u6237\u90fd\u6709\u552f\u4e00\u7684 token\u3002\u4f7f\u7528 redis \u54c8\u5e0c\u8868\u5b58\u50a8\u7528\u6237\u7684\u8eab\u4efd\u4fe1\u606f\uff0ctoken \u4e3a\u952e\uff0c\u8be6\u7ec6\u4fe1\u606f\u4e3a\u503c\u3002\u63d0\u4f9b\u7528\u6237 token \u4ece redis \u4e2d\u68c0\u7d22\u5e76\u8bc6\u522b\u7528\u6237\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/21\/2024052113121892256.jpg\" class=\"aligncenter\" title=\"redis\u600e\u4e48\u83b7\u53d6\u5f53\u524d\u7528\u6237\u63d2\u56fe\" alt=\"redis\u600e\u4e48\u83b7\u53d6\u5f53\u524d\u7528\u6237\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7 Redis \u83b7\u53d6\u5f53\u524d\u7528\u6237<\/strong><\/p>\n<p>Redis \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u952e\u503c\u6570\u636e\u5e93\uff0c\u5b83\u4e0d\u63d0\u4f9b\u5185\u7f6e\u7684\u673a\u5236\u6765\u83b7\u53d6\u5f53\u524d\u7528\u6237\u3002\u7136\u800c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u6a21\u62df\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u5e76\u5b9e\u73b0\u7528\u6237\u8eab\u4efd\u8bc6\u522b\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 token \u521b\u5efa\u4f1a\u8bdd<\/strong><\/p>\n<ul>\n<li>\u4e3a\u6bcf\u4e2a\u7528\u6237\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684 token\u3002<\/li>\n<li>\u5c06 token \u548c\u7528\u6237\u8be6\u7ec6\u4fe1\u606f\u5b58\u50a8\u5728 Redis \u4e2d\uff0c\u4ee5\u521b\u5efa\u4f1a\u8bdd\u3002<\/li>\n<li>\u6709\u6548\u671f\u5185\uff0c\u4f7f\u7528\u76f8\u540c\u7684 token \u53ef\u8bc6\u522b\u7528\u6237\u3002<\/li>\n<\/ul>\n<p><strong>2. \u8bbe\u7f6e Redis \u54c8\u5e0c\u8868<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Redis \u54c8\u5e0c\u8868\u6765\u5b58\u50a8\u7528\u6237\u7684\u8eab\u4efd\u4fe1\u606f\uff0c\u4f8b\u5982\u7528\u6237\u540d\u3001\u7535\u5b50\u90ae\u4ef6\u6216\u5176\u4ed6\u6807\u8bc6\u7b26\u3002<\/li>\n<li>\u5c06\u7528\u6237 token \u8bbe\u7f6e\u4e3a\u54c8\u5e0c\u8868\u7684\u952e\uff0c\u7528\u6237\u8be6\u7ec6\u4fe1\u606f\u8bbe\u7f6e\u4e3a\u503c\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4ece Redis \u4e2d\u83b7\u53d6\u7528\u6237<\/strong><\/p>\n<ul>\n<li>\u901a\u8fc7\u63d0\u4f9b\u7528\u6237 token\uff0c\u4ece Redis \u4e2d\u68c0\u7d22\u76f8\u5e94\u7684\u54c8\u5e0c\u8868\u3002<\/li>\n<li>\u54c8\u5e0c\u8868\u7684\u503c\u5c06\u5305\u542b\u5f53\u524d\u7528\u6237\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4ece\u800c\u5b9e\u73b0\u7528\u6237\u8eab\u4efd\u8bc6\u522b\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528 Node.js \u548c Redis \u5e93\uff09<\/strong><\/p>\n<pre>const redis = require(\"redis\");\nconst client = redis.createClient();\n\nasync function getCurrentUser(token) {\n  const userHash = await client.hgetall(token);\n  return userHash;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<pre>const currentUser = await getCurrentUser(\"my_token\");\nconsole.log(currentUser);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65b9\u6cd5\u5141\u8bb8\u60a8\u5728 Redis \u4e2d\u8ddf\u8e2a\u4f1a\u8bdd\uff0c\u5e76\u901a\u8fc7\u63d0\u4f9b token \u6765\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u8eab\u4efd\u4fe1\u606f\u3002\u8bf7\u6ce8\u610f\uff0c\u60a8\u8d1f\u8d23\u7ef4\u62a4 token \u7684\u6709\u6548\u6027\u548c\u5b89\u5168\u6027\uff0c\u4ee5\u9632\u6b62\u672a\u7ecf\u6388\u6743\u7684\u8bbf\u95ee\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7 redis \u83b7\u53d6\u5f53\u524d\u7528\u6237\uff0c\u60a8\u53ef\u4ee5\uff1a\u521b\u5efa\u4f1a\u8bdd\uff0c\u6bcf\u4e2a\u7528\u6237\u90fd\u6709\u552f\u4e00\u7684 token\u3002\u4f7f\u7528 redis \u54c8\u5e0c\u8868\u5b58\u50a8\u7528\u6237\u7684\u8eab\u4efd\u4fe1\u606f\uff0ctoken \u4e3a\u952e\uff0c\u8be6\u7ec6\u4fe1\u606f\u4e3a\u503c\u3002\u63d0\u4f9b\u7528\u6237 token \u4ece redis \u4e2d\u68c0\u7d22\u5e76\u8bc6\u522b\u7528\u6237\u3002 \u5982\u4f55\u901a\u8fc7 Redis \u83b7\u53d6\u5f53\u524d\u7528\u6237 Redis \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u952e\u503c\u6570\u636e\u5e93\uff0c\u5b83\u4e0d\u63d0\u4f9b\u5185\u7f6e\u7684\u673a\u5236\u6765\u83b7\u53d6\u5f53\u524d\u7528\u6237\u3002\u7136\u800c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u6a21\u62df\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u5e76\u5b9e\u73b0\u7528\u6237\u8eab\u4efd\u8bc6\u522b\uff1a 1. \u4f7f\u7528 token \u521b\u5efa\u4f1a\u8bdd \u4e3a\u6bcf\u4e2a\u7528\u6237\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684 token\u3002 \u5c06 token \u548c\u7528\u6237\u8be6\u7ec6\u4fe1\u606f\u5b58\u50a8\u5728 Redis \u4e2d\uff0c\u4ee5\u521b\u5efa\u4f1a\u8bdd\u3002 \u6709\u6548\u671f\u5185\uff0c\u4f7f\u7528\u76f8\u540c\u7684 token \u53ef\u8bc6\u522b\u7528\u6237\u3002 2. \u8bbe\u7f6e Redis \u54c8\u5e0c\u8868 \u4f7f\u7528 Redis \u54c8\u5e0c\u8868\u6765\u5b58\u50a8\u7528\u6237\u7684\u8eab\u4efd\u4fe1\u606f\uff0c\u4f8b\u5982\u7528\u6237\u540d\u3001\u7535\u5b50\u90ae\u4ef6\u6216\u5176\u4ed6\u6807\u8bc6\u7b26\u3002 \u5c06\u7528\u6237 token \u8bbe\u7f6e\u4e3a\u54c8\u5e0c\u8868\u7684\u952e\uff0c\u7528\u6237\u8be6\u7ec6\u4fe1\u606f\u8bbe\u7f6e\u4e3a\u503c\u3002 3. \u4ece Redis \u4e2d\u83b7\u53d6\u7528\u6237 \u901a\u8fc7\u63d0\u4f9b\u7528\u6237 token\uff0c\u4ece Redis \u4e2d\u68c0\u7d22\u76f8\u5e94\u7684\u54c8\u5e0c\u8868\u3002 \u54c8\u5e0c\u8868\u7684\u503c\u5c06\u5305\u542b\u5f53\u524d\u7528\u6237\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4ece\u800c\u5b9e\u73b0\u7528\u6237\u8eab\u4efd\u8bc6\u522b\u3002 \u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528 Node.js \u548c Redis \u5e93\uff09 const [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5055","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5055"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5055\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}