แต่เมื่อกำหนดไว้สูง ก็จะมีปัญหาตามมาอีก นั้นคือ Allow memory size of ... exhausted ซึ่งจะเกิดกับเว็บที่มีปริมาณผู้ใช้งานสูง หรือมี code ที่ใช้แรมเยอะกว่าปกติ หรืออาจจะมี BUG ที่เกิดขึ้นโดยตั้งใจหรือไม่ตั้งใจก็ตาม
top - 19:16:12 up 23:47, 0 users, load average: 0.37, 0.24, 0.19
Tasks: 130 total, 2 running, 127 sleeping, 0 stopped, 1 zombie
Cpu(s): 6.7%us, 1.5%sy, 0.0%ni, 91.6%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1048576k total, 632996k used, 415580k free, 0k buffers
Swap: 0k total, 0k used, 0k free, 0k cached
ด้านบนนี้เป็นการ Monitor ดูการใช้งานของ Host สามารถดูได้ก็ต่อเมื่อเราเช่าเครื่อง Server เป็นของตัวเอง หรือเช่าแบบ VPS ก็สามารถดูได้เช่นกัน แต่สำหรับผู้ที่เช่า Host แบบทั่วไปนั้น ไม่สามารถทำการดูตรงส่วนนี้ได้ วิธีแก้ไขสำหรับเว็บที่มีปริมาณผู้ใช้เยอะ ควรมีการเก็บ Cache file ในหน้าที่มีการใช้งานสูง จะทำให้ช่วยละการประมวลผลของ php ซึ่งหน้าโดยปกติแล้วจะเป็นแบบ Dynamic การทำ Cache นั้น จะทำให้หน้าที่มีผู้ใช้งานเรียกบ่อยๆ เป็นแบบ Static
การทำ cache นั้นช่วยลดการใช้ Memory ได้จริงหรือ?
ผมเองมั่นใจว่า อย่างน้อย หน้าที่ทำ Cache นั้น จะไม่มีการประมวลผลของ php หรือใช้ mysql จึงทำให้ลดภาระการทำงานของ Server ไปในตัว การส่งหน้าที่เป็น Cache แบบ html ธรรมดานั้น จะไม่มีผลต่อ Traffic ของเว็บไซต์แต่อย่างได้ และยังทำให้ผู้ใช้งานเปิดหน้าที่มีผู้ใช้งานสูงๆ ได้ไวขึ้นอีกด้วย
ขั้นตอนการทำ Cache file แบบง่ายๆ
วางcode นี้ไว้บรรทัดบนสุดของไฟล์
<?php
ob_start(); // start the output buffer
$cachefile = “cache/home.html”;
if (file_exists($cachefile)) {
// the page has been cached from an earlier request
include($cachefile); // output the contents of the cache file
exit; // exit the script, so that the rest isnt executed
}
?>
.. Your usual PHP script and HTML here …
code php หรือ html ที่เราใช้งานในหน้านั้นๆ และบรรทัดสุดท้าย ให้ใส่
<?php
$cachefile = “cache/home.html”;
$fp = fopen($cachefile, ‘w’); // open the cache file “cache/home.html” for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser
?>
ดูรายละเอียดของการทำ cache file เพิ่มเติมที่