0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · 课时

实现基本缓存模式

学习使用 Redis 实现旁路缓存和写穿透模式,处理常见的应用数据

实现基本缓存模式 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Basic Cache Patterns Intro

Welcome! In this lesson, we'll dive into two fundamental caching patterns: Cache-Aside and Write-Through. These patterns help you integrate Redis into your applications to store and retrieve data efficiently.

Understanding them is key to building responsive and scalable systems.

What is Cache-Aside?

The Cache-Aside pattern is one of the most common ways to use a cache. Here, your application is responsible for managing both the cache and the primary data store (like a database).

  • When reading data, the app first checks the cache.
  • If found (a 'cache hit'), it returns the cached data.
  • If not found (a 'cache miss'), it fetches the data from the database, stores it in the cache, and then returns it.

Cache-Aside: The Read Flow

Imagine your app needs user data. Here's how Cache-Aside works:

  1. App requests data: Checks Redis for user:123.
  2. Cache Miss: Redis replies 'not found'.
  3. App queries DB: Fetches user:123 from the database.
  4. App updates cache: Stores user:123 in Redis.
  5. App returns data: Provides data to the user.
  6. Subsequent requests: Now, Redis will have user:123, leading to a fast 'cache hit'!

Cache-Aside CLI Example

Let's simulate a Cache-Aside read flow using Redis CLI. First, we'll try to get a key that isn't in the cache (miss), then retrieve it from a conceptual database and store it. Finally, we'll get it again (hit).

DEL product:101
GET product:101

# Simulate fetching from DB: "Laptop X"
SET product:101 "Laptop X" EX 3600

GET product:101

Cache-Aside: Pros & Cons

Benefits:

  • Simplicity: Easy to implement.
  • Read-Heavy Workloads: Excellent for data that is read often but changes infrequently.
  • Data Freshness: New data is added to cache only when requested, reducing cache pollution.

Drawbacks:

  • Initial Latency: First read for any data always results in a cache miss, making it slower.
  • Stale Data: If the database is updated directly, the cache might hold old data until it expires or is explicitly invalidated.

What is Write-Through?

The Write-Through pattern ensures that data is written to both the cache and the primary data store (database) at the same time. The application writes to the cache, and the cache is responsible for writing that data to the database.

  • When data is written, it goes to the cache first.
  • The cache then immediately writes the data to the database.
  • The write operation is only considered complete after both operations succeed.

Write-Through: The Write Flow

Consider updating a product's price. Here's how Write-Through works:

  1. App writes data: Sends new product price for product:202 to Redis.
  2. Cache writes to DB: Redis immediately writes the same update to the database.
  3. Cache acknowledges: Redis confirms the write to the application only after the database write is complete.
  4. App continues: The application proceeds, knowing both cache and DB are consistent.

Write-Through CLI Example

In Write-Through, your application typically performs a single write operation to the cache, and the cache (or a client library implementing the pattern) handles the database persistence. Here, we simulate setting a value which would conceptually also update the DB.

SET user:456 '{"name": "Alice", "email": "alice@example.com"}'

# Conceptually, this SET command
# would trigger an update to your
# primary database as well.

GET user:456

Write-Through: Pros & Cons

Benefits:

  • Data Consistency: Cache and database are always in sync.
  • Reliability: Data is immediately persistent.
  • Simpler Reads: All reads are cache hits (assuming data is always written through).

Drawbacks:

  • Write Latency: Writes are slower because data must be written twice (cache + database).
  • Cache Pollution: Data written to the cache might never be read, wasting cache space.
  • Increased Load: Every write operation incurs a database write, potentially increasing database load.

Pattern Comparison Quiz

You're designing a feature where user profiles are frequently read but updated less often. Which caching pattern would generally be more suitable to optimize read performance and simplify implementation?

Recap: Basic Cache Patterns

Great job! You've learned about two fundamental caching strategies:

  • Cache-Aside: Your application manages the cache. Reads check cache first; on a miss, data is fetched from the DB, cached, and then returned. Best for read-heavy data.
  • Write-Through: Writes go to the cache, which then immediately writes to the database. Ensures strong consistency between cache and DB.

These patterns form the basis for more advanced caching techniques you'll explore in future lessons!

常见问题解答

「实现基本缓存模式」课时是免费的吗?

是的 — 「实现基本缓存模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

「实现基本缓存模式」这节课中我会学到什么?

学习使用 Redis 实现旁路缓存和写穿透模式,处理常见的应用数据 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「实现基本缓存模式」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?

能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为什么需要缓存?缓存简介
  2. 实现基本缓存模式
  3. 缓存淘汰与过期
  4. 防止缓存击穿与惊群
← 返回 Redis Caching & Messaging (Pub/Sub, Streams)