cache.mdc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ---
  2. description: 缓存(Cache)
  3. globs:
  4. ---
  5. # 缓存
  6. 为了方便开发者进行缓存操作的组件,它有利于改善项目的性能。它为我们提供了一个数据中心以便进行高效的数据访问。
  7. :::
  8. ## 使用
  9. ```ts
  10. import { InjectClient, Provide } from '@midwayjs/core';
  11. import { CachingFactory, MidwayCache } from '@midwayjs/cache-manager';
  12. @Provide()
  13. export class UserService {
  14. @InjectClient(CachingFactory, 'default')
  15. cache: MidwayCache;
  16. async invoke(name: string, value: string) {
  17. // 设置缓存
  18. await this.cache.set(name, value);
  19. // 获取缓存
  20. const data = await this.cache.get(name);
  21. // ...
  22. }
  23. }
  24. ```
  25. ## 换成 Redis (v7.1 版本)
  26. 安装依赖,具体可以查看@midwayjs cache
  27. ```bash
  28. pnpm i cache-manager-ioredis-yet --save
  29. ```
  30. `src/config/config.default.ts`
  31. ```ts
  32. import { CoolFileConfig, MODETYPE } from "@cool-midway/file";
  33. import { MidwayConfig } from "@midwayjs/core";
  34. // redis缓存
  35. import { redisStore } from "cache-manager-ioredis-yet";
  36. export default {
  37. // Redis缓存
  38. cacheManager: {
  39. clients: {
  40. default: {
  41. store: redisStore,
  42. options: {
  43. port: 6379,
  44. host: "127.0.0.1",
  45. password: "",
  46. ttl: 0,
  47. db: 0,
  48. },
  49. },
  50. },
  51. },
  52. } as unknown as MidwayConfig;
  53. ```
  54. ## 换成 Redis (以往版本)
  55. ```bash
  56. pnpm i cache-manager-ioredis --save
  57. ```
  58. `src/config/config.default.ts`
  59. ```ts
  60. import { CoolFileConfig, MODETYPE } from "@cool-midway/file";
  61. import { MidwayConfig } from "@midwayjs/core";
  62. // redis缓存
  63. import * as redisStore from "cache-manager-ioredis";
  64. export default {
  65. // Redis缓存
  66. cache: {
  67. store: redisStore,
  68. options: {
  69. port: 6379,
  70. host: "127.0.0.1",
  71. password: "",
  72. db: 0,
  73. keyPrefix: "cool:",
  74. ttl: null,
  75. },
  76. },
  77. } as unknown as MidwayConfig;
  78. ```
  79. ## 使用
  80. `src/modules/demo/controller/open/cache.ts`
  81. ```ts
  82. import { DemoCacheService } from "../../service/cache";
  83. import { Inject, Post, Provide, Get, InjectClient } from "@midwayjs/core";
  84. import { CoolController, BaseController } from "@cool-midway/core";
  85. import { CachingFactory, MidwayCache } from "@midwayjs/cache-manager";
  86. /**
  87. * 缓存
  88. */
  89. @Provide()
  90. @CoolController()
  91. export class AppDemoCacheController extends BaseController {
  92. @InjectClient(CachingFactory, "default")
  93. midwayCache: MidwayCache;
  94. @Inject()
  95. demoCacheService: DemoCacheService;
  96. /**
  97. * 设置缓存
  98. * @returns
  99. */
  100. @Post("/set")
  101. async set() {
  102. await this.midwayCache.set("a", 1);
  103. // 缓存10秒
  104. await this.midwayCache.set("a", 1, 10 * 1000);
  105. return this.ok(await this.midwayCache.get("a"));
  106. }
  107. /**
  108. * 获得缓存
  109. * @returns
  110. */
  111. @Get("/get")
  112. async get() {
  113. return this.ok(await this.demoCacheService.get());
  114. }
  115. }
  116. ```
  117. ## 方法缓存
  118. 有些业务场景,我们并不希望每次请求接口都需要操作数据库,如:今日推荐、上个月排行榜等,数据存储在 redis
  119. 框架提供了 `@CoolCache` 方法装饰器,方法设置缓存,让代码更优雅
  120. `src/modules/demo/service/cache.ts`
  121. ```ts
  122. import { Provide } from "@midwayjs/core";
  123. import { CoolCache } from "@cool-midway/core";
  124. /**
  125. * 缓存
  126. */
  127. @Provide()
  128. export class DemoCacheService {
  129. // 数据缓存5秒
  130. @CoolCache(5000)
  131. async get() {
  132. console.log("执行方法");
  133. return {
  134. a: 1,
  135. b: 2,
  136. };
  137. }
  138. }
  139. ```
  140. ::: warning
  141. service 主要是处理业务逻辑,`@CoolCache`应该要在 service 中使用,不要在 controller 等其他位置使用
  142. :::