ส่วนหัวควบคุมแคชของ CDN
เจาะลึกส่วนหัวควบคุมแคชของ HTTP (Cache-Control, Expires, ETag) และบทบาทของส่วนหัวเหล่านี้ต่อพฤติกรรมการแคชของ CDN
ส่วนหัวควบคุมแคชของ CDN เป็นบทเรียน Caching Strategies: Redis + CDN + Edge Computing ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Caching Strategies: Redis + CDN + Edge Computing และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Control CDN Caching
Welcome to CDN Cache Control Headers! HTTP headers are like instructions attached to web content. They tell browsers and Content Delivery Networks (CDNs) exactly how to handle caching.
Understanding these headers is crucial for optimizing your CDN's performance, ensuring content freshness, and managing user privacy.
Meet `Cache-Control`
The Cache-Control header is the most powerful and flexible HTTP header for dictating caching behavior. It allows you to specify directives that apply to both private caches (like a user's browser) and shared caches (like CDNs).
It gives you fine-grained control over how long content should be stored and under what conditions.
Set Cache Lifespan with `max-age`
One of the most common Cache-Control directives is max-age. It tells the cache (CDN or browser) how long, in seconds, the resource is considered fresh and can be served without revalidating with the origin server.
For example, max-age=3600 means the content is fresh for one hour.
Cache-Control: max-age=3600Public, Private, No-Cache, No-Store
Here are other key Cache-Control directives:
public: The response can be cached by any cache, including shared CDNs.private: The response is intended for a single user and can only be cached by private (browser) caches. CDNs should not cache it.no-cache: The cache must revalidate with the origin server before serving the content, even if it appears fresh.no-store: The cache must not store any part of the client request or server response. This is for highly sensitive data.
Cache-Control: public, max-age=86400`Expires`: An Older Caching Friend
The Expires header specifies an absolute date and time after which the response is considered stale. It's an older header, largely superseded by Cache-Control: max-age.
If both Cache-Control and Expires are present, Cache-Control usually takes precedence. CDNs will respect Expires if no Cache-Control header is set.
Expires: Thu, 01 Jan 2025 00:00:00 GMT`ETag`: The Entity Tag
An ETag (Entity Tag) is a unique identifier for a specific version of a resource. It's like a fingerprint or a hash of the content.
CDNs use ETag for efficient revalidation. When a cached resource becomes stale, the CDN sends an If-None-Match header with the stored ETag to the origin server. If the resource hasn't changed, the origin responds with a lightweight 304 Not Modified.
ETag: "abcdef123456"`Last-Modified` for Timestamps
Similar to ETag, the Last-Modified header provides a timestamp indicating when the resource was last changed on the origin server.
CDNs can use this for revalidation by sending an If-Modified-Since header with the stored timestamp. If the resource hasn't been modified since that time, the origin responds with 304 Not Modified.
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMTCDN Revalidation in Action
When a CDN receives a request for content it has cached, it first checks if the content is still fresh based on Cache-Control: max-age or Expires.
If the content is stale, or if no-cache is specified, the CDN will contact the origin server. It sends a conditional request using If-None-Match (with ETag) or If-Modified-Since (with Last-Modified). This prevents re-downloading the entire content if it hasn't changed, saving bandwidth and speeding up delivery.
Check Your Knowledge
Which of these Cache-Control directives prevent a CDN from storing a response, or require revalidation with the origin server before serving?
Headers: Your CDN's Instructions
In this lesson, we explored how HTTP cache control headers guide CDNs and browsers. You learned about:
- The powerful
Cache-Controlheader and its directives likemax-age,public,private,no-cache, andno-store. - The older
Expiresheader for absolute expiry dates. ETagandLast-Modifiedfor efficient content revalidation.
Mastering these headers is key to fine-tuning your CDN for optimal performance, ensuring content freshness, and handling sensitive data securely.
คำถามที่พบบ่อย
บทเรียน “ส่วนหัวควบคุมแคชของ CDN” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ส่วนหัวควบคุมแคชของ CDN” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Caching Strategies: Redis + CDN + Edge Computing ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ส่วนหัวควบคุมแคชของ CDN”
เจาะลึกส่วนหัวควบคุมแคชของ HTTP (Cache-Control, Expires, ETag) และบทบาทของส่วนหัวเหล่านี้ต่อพฤติกรรมการแคชของ CDN คุณปฏิบัติ Caching Strategies: Redis + CDN + Edge Computing ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Caching Strategies: Redis + CDN + Edge Computing หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Caching Strategies: Redis + CDN + Edge Computing บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ส่วนหัวควบคุมแคชของ CDN” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Caching Strategies: Redis + CDN + Edge Computing นี้ได้ไหม
ได้ บทเรียน Caching Strategies: Redis + CDN + Edge Computing ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ส่วนหัวควบคุมแคชของ CDN
- คุณลักษณะด้านความปลอดภัยของ CDN
- การวัดประสิทธิภาพ CDN
- TLS, HTTPS และการจัดการใบรับรองบน CDN