{"id":317966,"date":"2021-02-05T03:39:12","date_gmt":"2021-02-04T19:39:12","guid":{"rendered":"http:\/\/4563.org\/?p=317966"},"modified":"2021-02-05T03:39:12","modified_gmt":"2021-02-04T19:39:12","slug":"%e5%9c%a8-node-js-%e4%b8%ad%e5%a6%82%e4%bd%95%e5%90%88%e7%90%86%e7%9a%84%e8%b0%83%e9%85%8d%e6%9f%90%e4%ba%9b%e9%80%9a%e7%94%a8%e5%ae%9e%e4%be%8b%e5%af%b9%e8%b1%a1%ef%bc%9f%ef%bc%88%e5%a6%82-redis","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=317966","title":{"rendered":"\u5728 Node.js \u4e2d\u5982\u4f55\u5408\u7406\u7684\u8c03\u914d\u67d0\u4e9b\u901a\u7528\u5b9e\u4f8b\u5bf9\u8c61\uff1f\uff08\u5982 Redis\uff09"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5728 Node.js \u4e2d\u5982\u4f55\u5408\u7406\u7684\u8c03\u914d\u67d0\u4e9b\u901a\u7528\u5b9e\u4f8b\u5bf9\u8c61\uff1f\uff08\u5982 Redis\uff09               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chogath <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">  <\/p>\n<p>\u4e00\u3001\u6a21\u5757\u5bfc\u5165\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre><code>\/\/ redis.ts  export const redis = new ioRedis(options);  \/\/ service.ts  import { redis } from '.\/redis'  const func1 = async () =&gt; { await redis.get('demo') }; <\/code><\/pre>\n<p>\u4e8c\u3001egg.js app \u5168\u5c40\u5c5e\u6027\uff1a<\/p>\n<pre><code> module.exports.login = function* (ctx) {     const {app} = this     const form = ctx.request.body     let time = 3600 * 24 * 30 \/\/token \u8fc7\u671f\u65f6\u95f4     let token = generateToken({_id: form._id}, time)\/\/ \u751f\u6210 token     app.redis.set(form.username, token) \/\/ \u628a token \u5b58\u5165 redis <\/code><\/pre>\n<p>\u4e09\u3001Nest.js \u4f9d\u8d56\u6ce8\u5165<\/p>\n<pre><code>\/\/ redis.ts  export const redisProviders = {     provide: 'redis',     useFactory: async () =&gt; {     redis = new ioRedis(options);     return redis;     }, };  @Module({   providers: [redisProviders],   exports: [redisProviders], }) export class RedisModule {}  \/\/ service.ts  @Injectable() export class Service {   constructor(     @Inject('redis')     private readonly redis   ) {}    async func1() {     await this.redis.get('demo')   } }  <\/code><\/pre>\n<p>\u4e2a\u4eba\u503e\u5411\u4e8e\u7b2c\u4e09\u79cd\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u4e2d\u7528\u7684\u662f\u7b2c\u4e00\u79cd\u3002\u6309\u53e6\u4e00\u4e2a\u4f8b\u5b50 logger \u5b9e\u4f8b\u6765\u8bf4\uff0clogger \u6709 info \/ error \u7b49\u65b9\u6cd5\u6765\u8bb0\u5f55\u65e5\u5fd7\uff0c\u5728\u5168\u5c40\u542f\u52a8\u7684\u65f6\u5019\uff0c\u4e5f\u9700\u8981 logger \u5b9e\u4f8b\uff0c\u8fd9\u79cd\u60c5\u51b5\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f\u53cd\u800c\u6210\u4e3a\u4e00\u79cd\u9650\u5236\u3002<\/p>\n<p>Node.js \u540e\u7aef\u5f00\u53d1\u7ecf\u9a8c\u6709\u9650\uff0c\u8bf7\u5927\u5bb6\u6307\u6b63\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5196754\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chogath <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u73b0\u5728\u4f7f\u7528\u7684\u5f00\u53d1\u6846\u67b6\u662f ts + nest.js \uff0c\u5206\u6210\u4e86 lib \/ modules \u4e24\u5c42<\/p>\n<p>lib \u7528\u6765\u5b9a\u4e49\u4e00\u4e9b\u516c\u5171\u7ec4\u4ef6\uff0c\u5982 redis \u3001database \u3001logger \u7b49\u3002<br \/>modules \u7528\u6765\u5b9e\u73b0\u4e1a\u52a1\uff0c\u5176\u4e2d controller \u3001service \u3001dao \u4f7f\u7528\u7684\u662f\u4f9d\u8d56\u6ce8\u5165\u3002<\/p>\n<p>\u5982\u679c modules \u5c42\u4e2d\u9700\u8981 \u516c\u5171\u7ec4\u4ef6\uff0c\u5219\u901a\u8fc7\u6a21\u5757\u5f15\u5165\u7684\u65b9\u5f0f\u8c03\u7528\u5177\u4f53\u65b9\u6cd5\uff08\u5982 logger.info \uff09                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5196755\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : optional <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e00\u79cd\u5176\u5b9e\u4e5f\u662f\u4f9d\u8d56\u6ce8\u5165                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5196756\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : chogath <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @optional emm&#8230;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Node.js \u4e2d\u5982\u4f55\u5408\u7406\u7684\u8c03&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317966"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317966"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317966\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317966"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}