

Also from Kynth Studios


Also from Kynth Studios


Also from Kynth Studios
12345# 缓存67为了方便开发者进行缓存操作的组件,它有利于改善项目的性能。它为我们提供了一个数据中心以便进行高效的数据访问。89:::1011## 使用1213```ts14import { InjectClient, Provide } from '@midwayjs/core';15import { CachingFactory, MidwayCache } from '@midwayjs/cache-manager';1617@Provide()18export class UserService {1920 @InjectClient(CachingFactory, 'default')21 cache: MidwayCache;2223 async invoke(name: string, value: string) {24 // 设置缓存25 await this.cache.set(name, value);26 // 获取缓存27 const data = await this.cache.get(name);28 // ...29 }30}31```3233## 换成 Redis (v7.1 版本)3435安装依赖,具体可以查看@midwayjs cache3637```bash38pnpm i cache-manager-ioredis-yet --save39```4041`src/config/config.default.ts`4243```ts44import { CoolFileConfig, MODETYPE } from "@cool-midway/file";45import { MidwayConfig } from "@midwayjs/core";46// redis缓存47import { redisStore } from "cache-manager-ioredis-yet";4849export default {50 // Redis缓存51 cacheManager: {52 clients: {53 default: {54 store: redisStore,55 options: {56 port: 6379,57 host: "127.0.0.1",58 password: "",59 ttl: 0,60 db: 0,61 },62 },63 },64 },65} as unknown as MidwayConfig;66```6768## 换成 Redis (以往版本)6970```bash71pnpm i cache-manager-ioredis --save72```7374`src/config/config.default.ts`7576```ts77import { CoolFileConfig, MODETYPE } from "@cool-midway/file";78import { MidwayConfig } from "@midwayjs/core";79// redis缓存80import * as redisStore from "cache-manager-ioredis";8182export default {83 // Redis缓存84 cache: {85 store: redisStore,86 options: {87 port: 6379,88 host: "127.0.0.1",89 password: "",90 db: 0,91 keyPrefix: "cool:",92 ttl: null,93 },94 },95} as unknown as MidwayConfig;96```9798## 使用99100`src/modules/demo/controller/open/cache.ts`101102```ts103import { DemoCacheService } from "../../service/cache";104import { Inject, Post, Provide, Get, InjectClient } from "@midwayjs/core";105import { CoolController, BaseController } from "@cool-midway/core";106import { CachingFactory, MidwayCache } from "@midwayjs/cache-manager";107108/**109 * 缓存110 */111@Provide()112@CoolController()113export class AppDemoCacheController extends BaseController {114 @InjectClient(CachingFactory, "default")115 midwayCache: MidwayCache;116117 @Inject()118 demoCacheService: DemoCacheService;119120 /**121 * 设置缓存122 * @returns123 */124 @Post("/set")125 async set() {126 await this.midwayCache.set("a", 1);127 // 缓存10秒128 await this.midwayCache.set("a", 1, 10 * 1000);129 return this.ok(await this.midwayCache.get("a"));130 }131132 /**133 * 获得缓存134 * @returns135 */136 @Get("/get")137 async get() {138 return this.ok(await this.demoCacheService.get());139 }140}141```142143## 方法缓存144145有些业务场景,我们并不希望每次请求接口都需要操作数据库,如:今日推荐、上个月排行榜等,数据存储在 redis146147框架提供了 `@CoolCache` 方法装饰器,方法设置缓存,让代码更优雅148149`src/modules/demo/service/cache.ts`150151```ts152import { Provide } from "@midwayjs/core";153import { CoolCache } from "@cool-midway/core";154155/**156 * 缓存157 */158@Provide()159export class DemoCacheService {160 // 数据缓存5秒161 @CoolCache(5000)162 async get() {163 console.log("执行方法");164 return {165 a: 1,166 b: 2,167 };168 }169}170```171172::: warning173service 主要是处理业务逻辑,`@CoolCache`应该要在 service 中使用,不要在 controller 等其他位置使用174:::175
One repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| cool-team-official/cool-admin-midway.cursor/rules/authority.mdc · 3.3k | Cursor rules | security | 54/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/controller.mdc · 3.3k | Cursor rules | api | 46/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/db.mdc · 3.3k | Cursor rules | setupdatabase | 62/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/event.mdc · 3.3k | Cursor rules | no sections | 45/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/module.mdc · 3.3k | Cursor rules | no sections | 50/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/service.mdc · 3.3k | Cursor rules | database | 54/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/socket.mdc · 3.3k | Cursor rules | no sections | 58/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/task.mdc · 3.3k | Cursor rules | style | 58/100 | today | |
| cool-team-official/cool-admin-midway.cursor/rules/tenant.mdc · 3.3k | Cursor rules | no sections | 50/100 | today | |
| cool-team-official/cool-admin-midway.cursorrules · 3.3k | .cursorrules | no sections | 39/100 | today |
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiromaily/go-crypto-wallet.cursor/rules/typescript.mdc · 126 | Cursor rules | setupbuildtestlint-format+6 | 100/100 | 14 days ago | |
| TechSquidTV/Hermes.cursor/rules/10-hermes-api.mdc · 46 | Cursor rules | testlint-formatstylearch+5 | 100/100 | 14 days ago | |
| Allymahmoud/case-intake-platform.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 14 days ago | |
| deifos/clipmira-subtitles.cursor/rules/frontend.mdc · 1 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 14 days ago | |
| markstev/mark-starter.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+6 | 99/100 | 14 days ago | |
| dodgecfr/combatfilms-webapp.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 14 days ago | |
| langflow-ai/langflow.cursor/rules/docs_development.mdc · 153k | Cursor rules | setupbuildtestlint-format+7 | 97/100 | 14 days ago | |
| TechSquidTV/Hermes.cursor/rules/20-hermes-api-tests.mdc · 46 | Cursor rules | teststyletesting-strategysecurity+3 | 97/100 | 14 days ago |
A badge carrying the measured quality of the strongest agent config file in this repository, out of 100. It reads from this index every time somebody loads your page, so it changes when the measurement changes and there is nothing to keep up to date. Free, no account, and the value is not something you or we can set by hand.
[](https://rulestack.kynth.studio/configs/cool-team-official-cool-admin-midway-cursor-rules-cache)Would rather not hotlink us? Every badge is also served in shields.io’s endpoint schema, so shields renders the image and your readers never talk to our domain:
Published by Toolproof, the masthead over this index and eight others. The method behind the number is at toolproof.kynth.studio/methodology, and the whole thing is readable as JSON with no key at /api.