缓存 是Redis最常见的用例——将频繁访问的数据存储在快速内存中,以便应用程序避免执行较慢的操作(数据库查询、API调用、计算)。理解基本的缓存模式是日常工作中必不可少的知识。
cache-aside 模式(最常见)
text
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.
js
() {
cached = redis.();
(cached) .(cached);
user = db..(id);
redis.(, .(user), , );
user;
}
