Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

แก้ปัญหา Cache ค้างด้วยการปรับโค้ดเล็กน้อย

Partial Caching วิธีดีได้ page speed improvements ใหญ่โดยไม่ทำงานมาก แตถ้าลืม touch: true บน associations หรือ template dependencies ไม่ทำงาน cached partials จะไม่ update

development environment ปกติ run caching disabled จึงค้นพบใน staging หรือแย่กว่า production! Debug ต้อง reproduce ใน development mode โดยตั้ง

config/environments/development.rb
config.action_controller.perform_caching = false

ใน config/environments/development.rb เป็น true ต้องทำทุกครั้ง debug cache problems และจำเปลี่ยนกลับก่อน check in ผมเกลียดทำแบบนั้น ตอนเริ่มโปรเจกต์ใหม่ ตั้งแบบนี้:

config/environments/development.rb
config.action_controller.perform_caching = ENV['CACHING'] == 'true'

แบบนี้ enable caching ได้ตอนต้องการ โดยรัน server ด้วย

CACHING=true rails server

รันแค่ rails server จะ run caching disabled ตามปกติ

ตอนเริ่มปรับ config parameters บ่อย pattern แปลง hardcoded parameters เป็น environment variables ประหยัดเวลาและการปรับแต่ง ลองในโปรเจกต์ของคุณ!