Caching là use case phổ biến nhất của Redis — lưu dữ liệu hay truy cập trong bộ nhớ nhanh để ứng dụng tránh các thao tác chậm hơn (truy vấn database, gọi API, tính toán). Hiểu pattern caching cơ bản là kiến thức hàng ngày thiết yếu.
Pattern cache-aside (phổ biến nhất)
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;
}
