Thuta Learning
ရှာဖွေရန်
Redis
ProjectsData & Databasesbeginner

Project 2 — Node.js Catalog Cache

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Project 2 — Node.js Catalog Cache concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ Redis command/code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် မှန်ကန်စွာအသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Production cache wrapper က serialization၊ timeout၊ key version၊ TTL jitter၊ metrics နဲ့ error policy ကိုနေရာတစ်ခုတည်းမှာထားသင့်ပါတယ်။ Caller က Redis details မသိဘဲ typed value/null ရမယ်။ Cache error ကို database error နဲ့ခွဲပြီး hit/miss/error/load latency များ observe လုပ်ရပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Node.js redis client တစ်ခုသုံးပြီး `getOrLoad<T>` helper ရေးမယ်။ JSON parse/validation error တွင် bad key ဖျက်၊ loader ကို single-flight lock ဖြင့်ကာကွယ်၊ DB update transaction commit ပြီး cache invalidate လုပ်မယ်။ Integration test တွင် hit/miss/expiry/update/Redis-down cases စစ်မယ်။

အတူတူ စမ်းရေးကြည့်မယ်

typescript
async function getOrLoad<T>(key: string, load: () => Promise<T | null>) {
  try {
    const hit = await redis.get(key);
    if (hit) return JSON.parse(hit) as T;
  } catch (error) { metrics.cacheError.inc(); }
  const value = await load();
  if (value) await redis.set(key, JSON.stringify(value), { EX: 300 + randomInt(30) });
  return value;
}
You should see
Cache unavailable ဖြစ်လည်း correctness မပျက်သော typed catalog service ရမည်။

၅ မိနစ် စမ်းကြည့်

Negative caching for missing tutorial ကို 15-second TTL ဖြင့်ထည့်ပြီး create-after-miss test ရေးပါ။

သတိလေးတစ်ချက်

Redis timeout ကို HTTP request timeout နီးပါးရှည်ထားရင် fallback အတွက်အချိန်မကျန်ပါ။ Cache timeout ကိုတိုထားပါ။

Redis — Node.js Client GuideRedis

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Redis timeout ကို HTTP request timeout နီးပါးရှည်ထားရင် fallback အတွက်အချိန်မကျန်ပါ။ Cache timeout ကိုတိုထားပါ။
  • နမူနာ command ကို production Redis ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test instance နဲ့ recoverable data ပေါ်တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

Negative caching for missing tutorial ကို 15-second TTL ဖြင့်ထည့်ပြီး create-after-miss test ရေးပါ။

You'll know it worked when: Cache unavailable ဖြစ်လည်း correctness မပျက်သော typed catalog service ရမည်။

Project 2 — Node.js Catalog Cache | Thuta Learning