Caching เป็น use case ที่พบบ่อยที่สุดของ Redis — เก็บข้อมูลที่เข้าถึงบ่อยไว้ในหน่วยความจำที่รวดเร็วเพื่อให้แอปพลิเคชันหลีกเลี่ยงการดำเนินการที่ช้ากว่า (การ query database, การเรียก API, การคำนวณ) การเข้าใจรูปแบบการ caching พื้นฐานเป็นความรู้ที่จำเป็นในชีวิตประจำวัน
รูปแบบ cache-aside (พบบ่อยที่สุด)
1. App needs data → CHECK Redis first (cache lookup)
2. CACHE HIT (found) → return it immediately (fast!) ✓
3. CACHE MISS (not found) → query the database, store the result in Redis (with TTL),
then return it. Next time it'll be a hit.
() {
cached = redis.();
(cached) .(cached);
user = db..(id);
redis.(, .(user), , );
user;
}
