Demo

Caching

Root module ပေါ်တွင် @Cache တစ်ခု သို့မဟုတ် အများအပြား ကြေညာရပါသည်။ @CacheClient ဖြင့် owner plugin (နှင့် optional name) အလိုက် inject လုပ်ရပါသည် — အခြား plugin ပိုင် cache များ အပါအဝင်။ Route / method helper များသည် ထို named client များကိုသာ ညွှန်ပါသည်။

Stack

  • @Cache — root @Module; named cache အများအပြား ဖွင့်နိုင်ပါသည်
  • @CacheClient(plugin, name?) — ဤ plugin သို့မဟုတ် အခြား plugin မှ ICache
  • @CacheRoute / @DeleteCacheRoute — plugin + name
  • @CacheFn / @DeleteCacheFn — plugin + name
  • Cross-plugin — consumer က owner ၏ cache ကို inject လုပ်ပါသည်; တူညီသော @Cache ကို ထပ်မကြေညာရပါ

Root module ပေါ်တွင် @Cache

@Cache ကို @Module class ပေါ်တွင်သာ တပ်ရပါမည် — service သို့မဟုတ် controller ပေါ်တွင် မဟုတ်ပါ။ Cache အများအပြား ဖွင့်လျှင် unique name ပေးရပါသည်။ Module class တစ်ခုပေါ်တွင် @Cache အများအပြား ခွင့်ပြုပါသည်။

  • type — "in-memory" သို့မဟုတ် Redis
  • name — cache အများအပြား ရှိလျှင် လိုအပ်ပါသည်
  • Owner သည် loading plugin — consumer များက metadata.name (သို့မဟုတ် CacheManager.DEFAULT_PLUGIN) ဖြင့် ခေါ်ပါသည်
TSXmultiple caches in one plugin
1import { Cache, Module } from "@quan-erp/shared-backend-core"; 2import metadata from "../../module.metadata.json" with { type: "json" }; 3 4@Cache({ 5 name: "publisher", 6 type: "in-memory", 7 checkperiod: 1000, 8}) 9@Cache({ 10 name: "session", 11 type: "redis", 12 host: process.env.REDIS_HOST, 13 port: Number(process.env.REDIS_PORT ?? 6379), 14 password: process.env.REDIS_PASSWORD, 15}) 16@Module({ 17 name: metadata.name, 18 providers: [...], 19 controllers: [...], 20}) 21export class MyPluginModule {}

@CacheClient

Service property ပေါ်တွင် ICache inject လုပ်ရပါသည်။ ပထမ arg သည် cache ပိုင်သော plugin; ဒုတိယ arg သည် optional connection name ဖြစ်ပါသည်။

  • CacheManager.DEFAULT_PLUGIN — core / shared default
  • metadata.name — ဤ plugin ပိုင် cache
  • "other-plugin" — အခြား plugin က ဖန်တီးထားသော cache
  • ဒုတိယ arg name — named client (ဥပမာ "publisher")
TSXinject
1import { 2 CacheClient, 3 CacheManager, 4 Service, 5} from "@quan-erp/shared-backend-core"; 6import type { ICache } from "@quan-erp/shared-backend-core"; 7import metadata from "../../module.metadata.json" with { type: "json" }; 8 9@Service() 10export class PublisherService { 11 // Core / default owner 12 @CacheClient(CacheManager.DEFAULT_PLUGIN, "publisher") 13 private defaultPublisher: ICache; 14 15 // This plugin’s named cache 16 @CacheClient(metadata.name, "session") 17 private session: ICache; 18 19 // Cache owned by another plugin 20 @CacheClient("warehouse", "replica") 21 private warehouseReplica: ICache; 22}

အခြား plugin ၏ cache သုံးခြင်း

Cache များသည် owner plugin (+ optional name) ဖြင့် process-wide registry တွင် ရှိပါသည်။ Consumer သည် ထို client အတွက် @Cache ပြန်မကြေညာဘဲ owner ၏ cache ကို inject လုပ်သုံးပါသည်။

  • Owner — @Cache ကြေညာပြီး အရင် boot လုပ်ရပါသည် (pluginDependencies)
  • Consumer — @CacheClient("owner-plugin", "cache-name")
  • Route / method — plugin + name ကို owner နှင့် ကိုက်အောင် သတ်မှတ်ရပါသည်
  • Consumer တိုင်းတွင် တူညီသော @Cache ထပ်မကြေညာရပါ
TSXconsumer service
1import { CacheClient, Service } from "@quan-erp/shared-backend-core"; 2import type { ICache } from "@quan-erp/shared-backend-core"; 3 4@Service() 5export class ReportService { 6 // "warehouse" plugin declared @Cache({ name: "replica", ... }) 7 @CacheClient("warehouse", "replica") 8 private warehouseReplica: ICache; 9}

Route-level

  • @CacheRoute({ key, plugin, name }) — key သည် string သို့မဟုတ် (req) => string
  • @DeleteCacheRoute({ key, plugin, name }) — callback key သည် string[] ပြန်ပေးရပါမည်
  • plugin + name သည် ပိုင်ဆိုင်သော @Cache နှင့် ကိုက်ရပါမည် (အခြား plugin အပါအဝင်)
TSXroute cache
1import { 2 CacheRoute, 3 Controller, 4 DeleteCacheRoute, 5 Get, 6 Post, 7} from "@quan-erp/shared-backend-core"; 8 9@Controller("/branch") 10export class BranchController { 11 @Get("/") 12 @CacheRoute({ 13 key: (req) => `branch-${req.query.skip}-${req.query.limit}`, 14 plugin: "warehouse", 15 name: "replica", 16 }) 17 async list() { /* … */ } 18 19 @Post("/") 20 @DeleteCacheRoute({ 21 key: (req) => [`branch-${(req as any).user.payload.id}`], 22 plugin: "warehouse", 23 name: "replica", 24 }) 25 async create() { /* … */ } 26}

Method-level

  • @CacheFn({ key, plugin, name }) — key သည် (...args) => string
  • @DeleteCacheFn({ key, plugin, name }) — key သည် string[] ပြန်ပေးပါသည် (wildcard ခွင့်ပြုပါသည်)
  • @CacheClient / route helpers နှင့် တူညီသော plugin + name စည်းမျဉ်း
TSXmethod cache
1import { 2 CacheFn, 3 CacheManager, 4 DeleteCacheFn, 5 Service, 6} from "@quan-erp/shared-backend-core"; 7import type { CreateJobDTO } from "./create-job.dto.js"; 8 9@Service() 10export class JobService { 11 @CacheFn({ 12 plugin: CacheManager.DEFAULT_PLUGIN, 13 name: "publisher", 14 key: (skip: number, limit: number) => `active-jobs-${skip}-${limit}`, 15 }) 16 async getActiveJobs(skip: number, limit: number) { /* … */ } 17 18 @DeleteCacheFn({ 19 plugin: CacheManager.DEFAULT_PLUGIN, 20 name: "publisher", 21 key: () => [`active-jobs-*`], 22 }) 23 async register(dto: CreateJobDTO) { /* … */ } 24}