နားလည်ထားရမယ့် အချက်
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 စစ်မယ်။
အတူတူ စမ်းရေးကြည့်မယ်
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;
}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 Guide — Redis