ถามวิธีการ Cache ไฟล์

เริ่มโดย Lunifer, 24 สิงหาคม 2012, 22:45:15

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

Lunifer

<?php
//Configuration
$cache_time = 60*60*24;      //second
$cache_file = "cache/".md5($_SERVER['REQUEST_URI']);

if(file_exists($cache_file)) {
   if((time()-filemtime($cache_file)) < $cache_time) {
      $cache_data = file_get_contents($cache_file);
      if (!(strlen($cache_data) < 18 || strcmp(substr($cache_data,0,2),"\x1f\x8b"))) header( 'Content-Encoding: gzip' );
      echo $cache_data;
      die();
   }
}

ob_start(); // start the output buffer

//----------- Your PHP Code -----------//

$ob_buffer = ob_get_contents();
ob_end_flush(); // Send the output to the browser

$fp = fopen($cache_file, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, $ob_buffer); // save the contents of output buffer to the file
fclose($fp); // close the file
?>

โค้ด cache //และสร้าง โฟลเดอร์ cache ในโฟลเดอร์มี home.php
คือ พอเปิดหน้าเว็บ ก็มีจะไฟลที่ถูก cache แต่เป็น 0 kb แล้วพอผม  f5 หน้าเว็บจะกลายเป็นหน้าขาวๆ
ใครพอทราบวิธีแก้ไขมัยครับ +1  :wanwan017:

obi001

#1
$cachefile = "cache/" . $_SERVER['HTTP_HOST'] . "/$cacheflie_req";
$mtime = time() - @filemtime($cachefile);
if($mtime > $time_cache || !file_exists($cachefile)) {

   ob_start();
   echo $stories;
   $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);
   ob_end_flush();

} else

echo file_get_contents($cachefile);

ไม่รู้เหมือนกัน ว่าของคุณทำไม ทำงานไม่ได้
แต่ที่ผมใช้ก้อใช้ได้ดีนะ code ก้อไม่ต่างอะไร กันมาก
ขอให้เงินทองจงอยุ่กับท่าน

Lunifer

อ้างถึงจาก: obi001 ใน 24 สิงหาคม 2012, 23:19:08
$cachefile = "cache/" . $_SERVER['HTTP_HOST'] . "/$cacheflie_req";
$mtime = time() - @filemtime($cachefile);
if($mtime > $time_cache || !file_exists($cachefile)) {

   ob_start();
   echo $stories;
   $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);
   ob_end_flush();

} else

echo file_get_contents($cachefile);

ไม่รู้เหมือนกัน ว่าของคุณทำไม ทำงานไม่ได้
แต่ที่ผมใช้ก้อใช้ได้ดีนะ code ก้อไม่ต่างอะไร กันมาก
อันนี้มีการรับส่งคาหรือป่าวครับ

xvlnw.com

home.php ที่เอาไว้รันโค๊ด เอามาไว้นอกโฟเดอร์ cache ครับ
[direct=https://cloudhost.in.th/wordpress-hosting]ツ ⓌⓄⓇⒹⓅⓇⒺⓈⓈ ⒽⓄⓈⓉⒾⓃⒼ [/direct] :wanwan014:  :D
[direct=https://cloudhost.in.th/cloudhosting.html]Cloud Hosting[/direct] [direct=https://cloudhost.in.th/cloudvps.html]Cloud Server[/direct] [direct=https://cloudhost.in.th/vpshosting.html]Cloud VPS Hosting[/direct] [direct=https://cloudhost.in.th/windowsvps.html]Cloud Windows[/direct] Tel: 080-348-0843 LINE: ixvlnw

Lunifer

#4
ขอบคุณทั้ง 2 ท่านครับ ยังไม่ได้ครับ คือไฟล์ cache มาเป็น 0kb เปิดดูก็ว่างๆ  :'(

มันเหมือนว่า Cache ได้แต่ไม่มีข้อมูลในไฟล์ที่ cache

kanin03

<?php
$cachefile 
"cache/main.html";
$mtime time() - @filemtime($cachefile);

if(
$mtime 10 || !file_exists($cachefile)) {


ob_start(); // start the output buffer
?>

<!-- แสดงผล main -->

<!-- สิ้นสุด main -->
<?php
 
     $fp 
fopen($cachefile'w+'); // open the cache file "cache/home.html" for writing
     
fwrite($fpob_get_contents()); // save the contents of output buffer to the file
     
fclose($fp); // close the file
     
ob_end_flush();
} else
echo 
file_get_contents($cachefile);

?>


ลองดูอันนี้นะครับไม่รู้ใช้ได้ไหม

xvlnw.com

อ้างถึงจาก: Lunifer ใน 25 สิงหาคม 2012, 01:43:34
ขอบคุณทั้ง 2 ท่านครับ ยังไม่ได้ครับ คือไฟล์ cache มาเป็น 0kb เปิดดูก็ว่างๆ  :'(

มันเหมือนว่า Cache ได้แต่ไม่มีข้อมูลในไฟล์ที่ cache

ไอ้ตรงที่บอกว่า
//----------- Your PHP Code -----------//

ได้ใส่อะไรไปไหมครับ
ถ้าไม่มีอะไรเลย แน่นอน ไฟล์ที่มันสร้างขึ้นมา 0kb ครับ
[direct=https://cloudhost.in.th/wordpress-hosting]ツ ⓌⓄⓇⒹⓅⓇⒺⓈⓈ ⒽⓄⓈⓉⒾⓃⒼ [/direct] :wanwan014:  :D
[direct=https://cloudhost.in.th/cloudhosting.html]Cloud Hosting[/direct] [direct=https://cloudhost.in.th/cloudvps.html]Cloud Server[/direct] [direct=https://cloudhost.in.th/vpshosting.html]Cloud VPS Hosting[/direct] [direct=https://cloudhost.in.th/windowsvps.html]Cloud Windows[/direct] Tel: 080-348-0843 LINE: ixvlnw

kobkung

ใช่ ถ้าจะ cache ไฟล์ต้องบอกมันด้วยว่า cache อะไร เอาโค้ดเราไปแปะด้วย
ไม่งั้นมันก็ว่างสิ
เราเป็นบริษัท รับทำเว็บไซต์ แก้เว็บเดิม เขียน Php+Mysql+jQuery+css+bootstrap  งานตามสั่ง ประสบการณ์ จะ 19 ปี ละจ้า
[direct=http://www.smilephp.com]Smilephp.com[/direct] รับทำเว็บไซต์บริษัท e-commerce + ระบบชำระเงิน paypal,ธนาคาร  เว็บบริษัท เว็บขายของ ระบบจอง เช่า เขียน PHP ได้ทุกแนว  ช่วงนี้เน้นรับงาน ERP ระบบเอกสารบริษัท ,บัญชี ,  Barcode, Stock , Warehouse , Logistic ติดต่อ 086-364-5262
[direct=http://www.sellerorder.com ระบบหลังบ้าน จัดการ Order ขายของ  ช่วงนี้หาคนทดสอบฟรี ขอได้ตามเมลในเว็บ[/direct]
งดตอบคนทำเว็บนอกลู่นอกทาง ไม่ทำเว็บ WP ปั่นแชร์ เว็บประมูล สคริปปั่นใดๆ ไม่ทำเว็บบอลและพ

ohmohm

แนะนำเพิ่มครับ ถ้าใน cache ไฟล์มันขนาด 0 ก็ให้ไม่ใช้ cache

จาก

if(file_exists($cache_file)) {


แก้เพิ่ม

if(file_exists($cache_file) && (filesize($cache_file)>0)) {


หรือไม่ก็ใช้ความสามารถ cache ของ web server เลยครับ มีใน nginx, varnish