เมื่อสร้าง Rails app ใหม่ caching พร้อมใช้งานแล้ว ทำให้เริ่มต้นได้เร็วโดยเพียงแค่ wrap view fragments ด้วย helper cache หรือใช้ Rails.cache.fetch เก็บผลลัพธ์จาก external API ข้อมูลที่ cache จะเก็บอยู่ใน cache stores ของ Rails ซึ่งอาจเก็บในหน่วยความจำ Memcached Redis หรือเขียนตรงไปที่ดิสก์
แต่ cache store ไหนเหมาะกับสถานการณ์ไหน? ในภาพรวมนี้ เราจะดูแต่ละตัวเลือก
👋 หากชอบบทความนี้ เรามีเนื้อหาเกี่ยวกับประสิทธิภาพ Ruby (on Rails) มากมาย ดู checklist การติดตามประสิทธิภาพ Ruby ของเราได้เลย
File Store
:file_store ใช้เป็นค่าเริ่มต้นเมื่อไม่ได้ config cache store โดยเฉพาะ ตามชื่อ มันเขียน cache entries ไปที่ file system ไฟล์เหล่านี้จะอยู่ในโฟลเดอร์ tmp/cache ใน root ของแอปพลิเคชัน ยกเว้นคุณระบุตำแหน่งอื่นโดยตั้งค่า cache_store เป็น :file_store พร้อมส่ง path ของโฟลเดอร์ที่ต้องการ
config.cache_store = :file_store, "/path/to/cache/directory"
Cache files ถูกบันทึกที่ดิสก์และไม่ถูกล้างอัตโนมัติ หมายความว่าคุณต้องล้างเองเพื่อไม่ให้เต็มดิสก์ เช่น รัน Rails.cache.cleanup เป็นระยะๆ จะทำให้ cache ไม่มี expired entries
File store มีประโยชน์ใน development เพราะแต่ละ entry เก็บใน cache directory ทำให้คุณ drop item จาก cache ได้โดยลบไฟล์ของมัน เหมาะสำหรับทดสอบ cache invalidation
ใน production file store ใช้ได้เมื่อแต่ละ server process รันบน file system เดียวกัน แม้ช้ากว่า memory-based stores แต่ file store เหมาะหาก cache อาจโตใหญ่มาก
Memory Store
ตั้งแต่เวอร์ชัน 5.0 Rails ตั้งค่า :memory_store ใน development config อัตโนมัติเมื่อสร้าง app ใหม่ เมื่อใช้ memory store ข้อมูลที่ cache จะเก็บในหน่วยความจำใน process ของ Ruby web server
เนื่องจากข้อมูลอยู่ในหน่วยความจำใน process เดียวกับ web server memory store เหมาะสำหรับ development เพราะล้างอัตโนมัติเมื่อ restart development web server
ค่าเริ่มต้น in-memory store ใช้ 32 megabytes แต่ override ได้โดยส่ง option :size ตอน config cache store
config.cache_store = :memory_store, { size: 16.megabytes }
เมื่อ cache เต็ม least recently used entries จะถูกลบอัตโนมัติเพื่อทำที่ว่างให้ entry ใหม่
ใช้ memory store ใน production ได้ (และเร็วที่สุด) แต่ไม่แนะนำสำหรับระบบที่รัน multiple server processes เนื่องจาก processes เข้าถึง cache ของกันไม่ได้ แต่ละ process ต้อง maintain cache copy ของตัวเอง
Memcache Store
:mem_cache_store ใช้ Dalli gem และ Memcached เก็บ entries ใน centralized in-memory cache
ข้อมูลอยู่ใน process แยกจาก Ruby server process ดังนั้น cache ไม่หายเมื่อ app restart แต่คงอยู่ในหน่วยความจำตราบใดที่ Memcached server ยังรันอยู่ เมื่อ restart นั้น คุณจะเริ่มต้นด้วย cache ใหม่
Memcache store สมมติ cache server รันบน localhost โดยค่าเริ่มต้น แต่ส่ง address หนึ่งหรือหลายตัวเพื่อใช้ remote servers ได้
config.cache_store = :mem_cache_store, "cache-1.example.com", "cache-2.example.com"
Memcached config ใช้ maximum cache size 64 MB โดยค่าเริ่มต้น แต่ config ได้ผ่าน command line options หรือไฟล์ memcached.conf เหมือน memory store จะเริ่มลบ least recently used items เมื่อ cache ถึงขนาดสูงสุด
:mem_cache_store เป็น cache store มาตรฐานสำหรับ production environments ด้วย central memcached server cache แบ่งปันได้ระหว่าง multiple web servers และแม้แต่ multiple hosts เมื่อใช้ remote memcached server
Redis Cache Store
Rails 5.2 เพิ่ม :redis_cache_store ให้เก็บ cache entries ใน Redis คล้ายกับ Memcache store
เพื่อใช้ Redis เป็น Rails cache store ใช้ dedicated Redis cache ที่ตั้งค่าเป็น LRU (Last Recently Used) cache แทนที่จะชี้ store ไปที่ Redis server ที่มีอยู่ เพื่อให้แน่ใจว่า entries ถูกลบเมื่อถึงขนาดสูงสุด
Redis store ทำงานกับ Redis gem (รวม Redis::Distributed) และ hiredis รวมถึงรองรับ configuration options หลายอย่าง เช่น ตั้งค่า remote servers หลายตัว
cache_servers = %w[redis://cache-01:6379/0 redis://cache-02:6379/0]
config.cache_store = :redis_cache_store, { url: cache_servers }
Redis เขียน dataset ไปที่ดิสก์เป็นระยะ ดังนั้น cached data ส่วนใหญ่จะรอดชีวิตเมื่อ cache server restart ใน development cached items ลบได้จาก console ด้วย Rails.cache.delete
ใน production Redis แข่งขัน Memcached ในการเป็น centralized cache store แม้การใช้งานใน Rails apps ยังไม่แพร่หลายเท่า Memcached แต่อาจกลายเป็น cache store ยอดนิยมในอนาคต
ควรใช้ Cache Store ไหน?
โดยทั่วไป file และ memory stores ของ Rails เหมาะสำหรับ development แต่ใช้ได้ใน production สำหรับแอปเล็กๆ เมื่อเข้าใจข้อจำกัด Production-grade Memcached และ Redis stores มักเป็นตัวเลือกดีกว่าสำหรับ production apps ขนาดใหญ่ โดยเฉพาะเมื่อรัน multiple web servers บน multiple hosts
นี่คือภาพรวม cache stores ใน Rails ตรวจสอบส่วน Cache stores ใน Rails guides เพื่อข้อมูลเพิ่มเติมและ configuration options สำหรับแต่ละ store
คุณชอบบทความนี้และบทความก่อนหน้าในซีรีส์ AppSignal Academy อย่างไร? เรามีบทความเกี่ยวกับ caching ใน Rails อีกหลาย篇 อย่าลังเลบอกเราว่าอยากให้เขียนเรื่องไหน (เกี่ยวกับ caching หรืออย่างอื่น) ต่อไป!