Cache บน Hosting คืออะไร? วิธีตั้งค่าและเคลียร์ Cache 2026
Cache คือกลไกที่เก็บสำเนาของข้อมูลไว้ในที่ที่เข้าถึงได้เร็วกว่า ทำให้การโหลดครั้งต่อไปเร็วขึ้นมาก การเข้าใจและตั้งค่า cache อย่างถูกต้องเป็นหนึ่งในวิธีที่ง่ายที่สุดในการเร่งความเร็วเว็บไซต์
Cache is a mechanism that stores copies of data in faster-accessible locations, making subsequent loads much quicker. Properly configuring cache is one of the easiest ways to dramatically speed up your website.
ประเภทของ Cache
Browser Cache
บราวเซอร์เก็บสำเนา CSS, JavaScript, รูปภาพไว้ใน local เมื่อผู้ใช้กลับมาเยี่ยมชมไม่ต้องดาวน์โหลดซ้ำ ควบคุมผ่าน HTTP headers Cache-Control และ Expires
Server Cache / Page Cache
Server เก็บ HTML ที่ render แล้วไว้ ครั้งต่อไปที่มี request เหมือนกันจะส่ง cached HTML ทันทีแทนที่จะสร้างใหม่ ลด PHP processing และ database queries อย่างมาก
Object Cache
เก็บผลลัพธ์ของ database queries ไว้ใน memory (Redis, Memcached) เมื่อ query เดิมถูกเรียกซ้ำ ดึงจาก memory แทน database ทำให้เร็วขึ้นหลายร้อยเท่า
CDN Cache
Content Delivery Network เก็บสำเนาไฟล์ static ไว้ใน server หลายจุดทั่วโลก ผู้ใช้จะได้รับไฟล์จาก server ที่ใกล้ที่สุด ลด latency อย่างมีนัยสำคัญ
ตั้งค่า Browser Cache ผ่าน .htaccess
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType text/html "access plus 0 seconds"
</IfModule>
ตั้ง cache สำหรับ static files 1 ปี แต่ HTML ไม่ cache (เพื่อให้ content อัพเดทเสมอ)
WordPress Cache Plugin
- W3 Total Cache: ครบที่สุด รองรับ page cache, browser cache, object cache, CDN
- WP Super Cache: ง่ายที่สุด เหมาะสำหรับมือใหม่
- WP Rocket: Paid แต่ตั้งค่าง่ายและมีฟีเจอร์ครบ (ล่าสุด ~$59/ปี)
- LiteSpeed Cache: ฟรี ทำงานได้ดีที่สุดกับ LiteSpeed server
วิธีเคลียร์ Cache เมื่อจำเป็น
เมื่อไรที่ต้องเคลียร์ cache?
- หลังอัพเดทเนื้อหา, CSS หรือ JavaScript
- หลัง deploy code ใหม่
- เมื่อผู้ใช้รายงานว่าเห็นข้อมูลเก่า
- หลังแก้ไข bug
เคลียร์ Cache ใน WordPress
- W3 Total Cache: Performance → Purge All Caches
- WP Super Cache: Settings → WP Super Cache → Delete Cache
- WP Rocket: Rocket icon → Clear Cache
เคลียร์ Cloudflare Cache
Cloudflare Dashboard → Caching → Configuration → Purge Cache → Purge Everything หรือ Custom Purge เพื่อเคลียร์เฉพาะ URL
คำถามที่พบบ่อย (FAQ)
Cache ทำให้เว็บเร็วขึ้นเท่าไหร่?
ขึ้นอยู่กับ baseline แต่โดยทั่วไป page cache ลด TTFB (Time to First Byte) จาก 500ms+ เหลือ ไม่ถึง 100ms Browser cache ลด repeat visit load time ลงได้ 50-80%
Cache ทำให้ผู้ใช้เห็น content เก่าไหม?
ได้ ถ้า cache TTL ยาวและไม่เคลียร์ cache หลัง publish content ใหม่ ตั้ง cache TTL ให้เหมาะสม และ configure cache invalidation เมื่อมี update
ถ้าใช้ Cloudflare ต้องตั้ง cache อีกไหม?
Cloudflare cache ช่วยในระดับ CDN แต่ยังต้องตั้ง server cache ด้วย เพราะทั้งสองระดับทำงานต่างกัน Cloudflare = global CDN, server cache = ลด load ใน origin server
Cache ใช้พื้นที่ disk เยอะไหม?
ขึ้นอยู่กับจำนวนหน้าและขนาดไฟล์ page cache สำหรับเว็บทั่วไปใช้ 100MB-1GB ตรวจสอบพื้นที่ใน hosting และตั้งค่า cache expiry เพื่อล้าง cached files เก่าอัตโนมัติ