{"id":67400,"date":"2025-05-10T08:27:07","date_gmt":"2025-05-10T00:27:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/67400\/"},"modified":"2025-05-10T08:27:07","modified_gmt":"2025-05-10T00:27:07","slug":"nestjsredis%e5%ae%9e%e7%8e%b0%e7%bc%93%e5%ad%98%e6%ad%a5%e9%aa%a4%e8%af%a6%e8%a7%a3-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/67400\/","title":{"rendered":"NestJS+Redis\u5b9e\u73b0\u7f13\u5b58\u6b65\u9aa4\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>NestJS+Redis\u5b9e\u73b0\u7f13\u5b58\u6b65\u9aa4\u8be6\u89e3<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aNestJS+Redis\u5b9e\u73b0\u7f13\u5b58\u6b65\u9aa4\u8be6\u89e3\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>NestJS\u7684\u7f13\u5b58\u6a21\u5757\u5929\u751f\u652f\u6301Redis\u7b49\u7f13\u5b58\u673a\u5236\u3002\u4ee5\u4e0b\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\uff0c\u8bf4\u660e\u5982\u4f55\u5728NestJS\u4e2d\u64cd\u4f5cRedis\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>\u5148\u5b89\u88c5\u8fd0\u884cRedis\u670d\u52a1,\u6b65\u9aa4\u53c2\u89c1<\/p>\n<p>\u65b0\u5efanestjs\u9879\u76ee\uff1a<br \/> nest new [\u9879\u76ee\u540d\u79f0]<\/p>\n<p>\u5b89\u88c5cache\u76f8\u5173\u4f9d\u8d56<\/p>\n<pre>\nnpm install cache-manager\nnpm install -D @types\/cache-manager\nnpm install cache-manager-redis-store --save<\/pre>\n<p>\u6ce8\u518cRedis Store<br \/> \u6253\u5f00src-&gt;app.module.ts\uff0c\u8fd9\u91cc\u5047\u8bbe\u5df2\u7ecf\u5728\u672c\u5730\u5b89\u88c5\u542f\u52a8\u4e86Redis\u670d\u52a1<\/p>\n<pre>\nimport { Module, CacheModule } from '@nestjs\/common';\nimport * as redisStore from 'cache-manager-redis-store';\n\nimports: [\n  CacheModule.register({\n    store: redisStore,\n    host: 'localhost',\n    port: 6379,\n  }),\n],<\/pre>\n<p>\u6253\u5f00src-&gt;app.controller.ts, \u4f7f\u7528Redis\u7f13\u5b58\u670d\u52a1<\/p>\n<pre>\nimport {\n  Controller,\n  Get,\n  Res,\n  Req,\n  Inject,\n  CACHE_MANAGER,\n} from '@nestjs\/common';\nimport { Cache } from 'cache-manager';\n\nfakeString = 'Hello World!';\n\n@Get('cache-test')\nasync setGetSimpleString() {\n  const value = await this.cacheManager.get('my-string');\n  if (value) {\n    return {\n      data: value,\n      loadsFrom: 'redis cache',\n    };\n  }\n  await this.cacheManager.set('my-string', this.fakeString, { ttl: 20 });\n  return {\n    data: this.fakeString,\n    loadsFrom: 'fake database',\n  };\n}<\/pre>\n<p>\u6700\u540e\uff0c\u8bbf\u95ee\u63a5\u53e3\uff0c\u6253\u5f00Redis\u5ba2\u6237\u7aef\u5de5\u5177RedisNav\uff0c\u9a8c\u8bc1\u7ed3\u679c\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167246462263afc8ee540a3.png\" class=\"aligncenter\" title=\"NestJS+Redis\u5b9e\u73b0\u7f13\u5b58\u6b65\u9aa4\u8be6\u89e3\u63d2\u56fe\" alt=\"NestJS+Redis\u5b9e\u73b0\u7f13\u5b58\u6b65\u9aa4\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NestJS+Redis\u5b9e\u73b0\u7f13\u5b58\u6b65\u9aa4\u8be6\u89e3 \u6536\u85cf \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aNestJS+Redis\u5b9e\u73b0\u7f13\u5b58\u6b65\u9aa4\u8be6\u89e3\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~ NestJS\u7684\u7f13\u5b58\u6a21\u5757\u5929\u751f\u652f\u6301Redis\u7b49\u7f13\u5b58\u673a\u5236\u3002\u4ee5\u4e0b\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\uff0c\u8bf4\u660e\u5982\u4f55\u5728NestJS\u4e2d\u64cd\u4f5cRedis\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a \u5148\u5b89\u88c5\u8fd0\u884cRedis\u670d\u52a1,\u6b65\u9aa4\u53c2\u89c1 \u65b0\u5efanestjs\u9879\u76ee\uff1a nest new [\u9879\u76ee\u540d\u79f0] \u5b89\u88c5cache\u76f8\u5173\u4f9d\u8d56 npm install cache-manager npm install -D @types\/cache-manager npm install cache-manager-redis-store &#8211;save \u6ce8\u518cRedis Store \u6253\u5f00src-&gt;app.module.ts\uff0c\u8fd9\u91cc\u5047\u8bbe\u5df2\u7ecf\u5728\u672c\u5730\u5b89\u88c5\u542f\u52a8\u4e86Redis\u670d\u52a1 import { Module, CacheModule } from &#8216;@nestjs\/common&#8217;; import * as redisStore from &#8216;cache-manager-redis-store&#8217;; imports: [ CacheModule.register({ store: redisStore, host: &#8216;localhost&#8217;, port: 6379, }), ], \u6253\u5f00src-&gt;app.controller.ts, \u4f7f\u7528Redis\u7f13\u5b58\u670d\u52a1 import { Controller, Get, Res, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-67400","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67400","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=67400"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67400\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=67400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=67400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=67400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}