การใช้งานรูปแบบแคชพื้นฐาน
เรียนรู้การใช้งานรูปแบบอ่านจากแหล่งข้อมูลเมื่อไม่พบในแคชและเขียนผ่านแคช โดยใช้ Redis กับข้อมูลแอปพลิเคชันทั่วไป
การใช้งานรูปแบบแคชพื้นฐาน เป็นบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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:
- App requests data: Checks Redis for
user:123. - Cache Miss: Redis replies 'not found'.
- App queries DB: Fetches
user:123from the database. - App updates cache: Stores
user:123in Redis. - App returns data: Provides data to the user.
- 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:101Cache-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:
- App writes data: Sends new product price for
product:202to Redis. - Cache writes to DB: Redis immediately writes the same update to the database.
- Cache acknowledges: Redis confirms the write to the application only after the database write is complete.
- 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:456Write-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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Redis Caching & Messaging (Pub/Sub, Streams) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การใช้งานรูปแบบแคชพื้นฐาน”
เรียนรู้การใช้งานรูปแบบอ่านจากแหล่งข้อมูลเมื่อไม่พบในแคชและเขียนผ่านแคช โดยใช้ Redis กับข้อมูลแอปพลิเคชันทั่วไป คุณปฏิบัติ Redis Caching & Messaging (Pub/Sub, Streams) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Redis Caching & Messaging (Pub/Sub, Streams) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Redis Caching & Messaging (Pub/Sub, Streams) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การใช้งานรูปแบบแคชพื้นฐาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) นี้ได้ไหม
ได้ บทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำไมต้องใช้แคช บทนำสู่การแคช
- การใช้งานรูปแบบแคชพื้นฐาน
- การนำข้อมูลออกจากแคชและการหมดอายุ
- การป้องกันแคชถล่มและฝูงคำขอถล่ม