# ช่วยแนะนำ script php ทำระบบ cache ให้เวบไซต์หน่อยครับ

เริ่มโดย น้องบิ้ว, 30 สิงหาคม 2013, 15:34:54

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

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

น้องบิ้ว

อยากได้ script php ที่สามารถ Cache ระบบได้  คือเว็บผมเป็น php มีไฟล์รูปและไฟล์ java แค่อยากหาวิธีที cache หาใน google ไม่รู้จะ search ยังไง  คืออยากระบบ cache สามารถ เก็บไฟล์ภาพได้ ไฟล์ java ไฟล์อื่นๆ และ gen ออกมาเป็น .html โดยเราจะเรียกใช้ไฟล์ .html  โดย cahce สามารถกำหนดระยะเวลา cache ได้ด้วยเผื่อข้อมูล update ต้องไป gen ใหม่ให้อัตโนมัตตามที่เวลาตั้งไว้  มีคำแนะนำไหมคับ ผมพูดงงไหม หะๆ จะเป็นตัวอย่างในเนทก็ได้คับพอดีผมหาไม่เจอไม่รู้ว่าจะค้นหาใช้คำอะไร ขอบคุณล่วงหน้าคับ  :wanwan017:

beg

สคริปแคชไฟล์ แบบง่ายๆ ตั้งเวลาอัพเดตอัตโนมัติได้ตามต้องการครับ


<?php
ob_start
();  
// TOP of your script
$cachedir='cache'// ชื่อแฟ้มเก็บไฟล์แคช 
$file='index'// ชื่อไฟล์เว็บ
$cachefile=$cachedir.'/'.$file;
$cachetime =5*60;  // ระยะเวลา 5 นาที 
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime filemtime($cachefile))) {
include(
$cachefile);
echo 
" <p style='color:gray;font-size:0.7em;'> ข้อมูลเมื่อ ".date('jS F Y H:i'filemtime($cachefile))." </p> "// แสดงข้อความเวลาที่บันทึก
//exit;
}else{


/**
  Your normal PHP script and HTML content here
โค้ดเว็บของเราทั้งหมด  html php javascript ตั้งแต่ <htm> จนสุดท้าย </html>  ใส่ตรงนี้เลยครับ
**/



// BOTTOM of your script
$fp fopen($cachefile'w'); // open the cache file for writing
fwrite($fpob_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
}

?>


น้องบิ้ว

อ้างถึงจาก: beg ใน 30 สิงหาคม 2013, 16:18:58
สคริปแคชไฟล์ แบบง่ายๆ ตั้งเวลาอัพเดตอัตโนมัติได้ตามต้องการครับ


<?php
ob_start
();  
// TOP of your script
$cachedir='cache'// ชื่อแฟ้มเก็บไฟล์แคช 
$file='index.php'// ชื่อไฟล์เว็บ
$cachefile=$cachedir.'/'.$file;
$cachetime =5*60;  // ระยะเวลา 5 นาที 
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime filemtime($cachefile))) {
include(
$cachefile);
echo 
" <p style='color:gray;font-size:0.7em;'> ข้อมูลเมื่อ ".date('jS F Y H:i'filemtime($cachefile))." </p> "// แสดงข้อความเวลาที่บันทึก
//exit;
}else{


/**
  Your normal PHP script and HTML content here
โค้ดเว็บของเราทั้งหมด  html php javascript ตั้งแต่ <htm> จนสุดท้าย </html>  ใส่ตรงนี้เลยครับ
**/



// BOTTOM of your script
$fp fopen($cachefile'w'); // open the cache file for writing
fwrite($fpob_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
}

?>



ขอบคุณมากคับ จะลองให้เพื่อนทำให้คับ  คือระบบนี้มันเก็บไฟล์รูปไว้ด้วยไหมคับ เพราะไฟล์รูปมันไปดึง url จากเวบอื่นมานะคับ อยากให้เกบเป็น url ของตัวเอง  ขอบคุณล่วงหน้าคับ

beg

อ้างถึงจาก: น้องบิ้ว ใน 30 สิงหาคม 2013, 16:25:55

ขอบคุณมากคับ จะลองให้เพื่อนทำให้คับ  คือระบบนี้มันเก็บไฟล์รูปไว้ด้วยไหมคับ เพราะไฟล์รูปมันไปดึง url จากเวบอื่นมานะคับ อยากให้เกบเป็น url ของตัวเอง  ขอบคุณล่วงหน้าคับ

ถ้าจะเก็บไฟล์รูปต้องเขียนโค้ดเพิ่มครับ 

ohmohm


beg

สคริปเก็บแคชไฟล์รูป กำหนดไว้ 1 ปี หรือเปลี่ยนได้ตามต้องการครับ
ชื่อไฟล์โค้ด ให้ตั้งชื่อเป็น  cachepic.php
เวลาเรียกใช้งาน จะเป็นแบบนี้  <img src="cachepic.ph?url=ไฟล์รูปจากเว็บอื่นๆ"/>


<?php
if(!EMPTY($_GET['url'])){
ob_start();  
$ctype="image/jpg";
header('Content-type: ' $ctype);
// TOP of your script
$cachedir='cachepic'// ชื่อแฟ้มเก็บไฟล์แคชภาพ
$url=$_GET['url'];
$file=md5($url).'.jpg'
$cachefile=$cachedir.'/'.$file;
$cachetime =60*60*24*366;  // ระยะเวลา 1 ปี
if (file_exists($cachefile) && (time() - $cachetime filemtime($cachefile))) {
include(
$cachefile);
//exit;
}else{

echo 
file_get_contents($url);

// BOTTOM of your script
$fp fopen($cachefile'w'); // open the cache file for writing
fwrite($fpob_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
}
}
?>






ส่วนสคริปแคชหน้าเว็บ ตรงเนื้อหาเว็บเดิม ตรงโค้ดรูป <img src="http://domain.com/aaa.jpg"/>  ให้เปลี่ยนเป็น <img src="cachepic.php?url=http://domain.com/aaa.jpg"/>

ถ้าขี้เกียจแก้ ก็ให้ใช้โค้ดสคริปแคชด้านล่างนี้ได้เลยครับ ผมแก้ไขให้แล้ว

สรุปคือ จะต้องสร้างไฟล์ cachepic.php ขึ้นมา (โค้ดอยู่ด้านบน)  เอาไปไว้ในแฟ้มเดียวกันกับไฟล์สคริปเว็บแคชของหน้าเว็บ (โค้ดด้านล่าง)  เช่น  index.php  แล้วก็เรียกใช้งานตามปกติครับ

ทีสำคัญ อย่าลืมสร้างแฟ้มว่างๆ ชื่อ cache กับ cachepic ด้วยนะครับ


<?php
ob_start
();  
// TOP of your script
$cachedir='cache'// ชื่อแฟ้มเก็บไฟล์แคช 
$file='index.php'// ชื่อไฟล์เว็บ
$cachefile=$cachedir.'/'.$file;
$cachetime =5*60;  // ระยะเวลา 5 นาที 
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime filemtime($cachefile))) {
include(
$cachefile);
echo 
" <p style='color:gray;font-size:0.7em;'> ข้อมูลเมื่อ ".date('jS F Y H:i'filemtime($cachefile))." </p> "// แสดงข้อความเวลาที่บันทึก
//exit;
}else{


/**
  Your normal PHP script and HTML content here
โค้ดเว็บของเราทั้งหมด  html php javascript ตั้งแต่ <htm> จนสุดท้าย </html>  ใส่ตรงนี้เลยครับ
**/



// BOTTOM of your script
$mydata=ob_get_contents();
$mydata=str_replace('<img src="','<img src="cachepic.php?url=',$mydata);
$fp fopen($cachefile'w'); // open the cache file for writing
fwrite($fp,$mydata); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser
}

?>




น้องบิ้ว

#6
คุณ beg ขอบคุณสำหรับน้ำใจมากๆคับ เพื่อนผมอ่านแล้วน่าจะพอทำได้คับ  ขอบคุณมากๆอีกครั้งคับ ขอให้ร่ำรวย สุขภาพแข็งแรงคับ  :wanwan017: :wanwan017:
ขอบคุณคุณ ohmohm ด้วยคับ


phpwin

ใช้ php cache kit ครับ วิธีทำไม่ยาก เอาชื่อไปเสริชในกูเกิล มีสอนเยอะแยะครับ  :wanwan019:
[direct=http://php-for-ecommerce.blogspot.com]สอน PHP ร้านค้าออนไลน์[/direct]  [direct=http://php-for-ecommerce.blogspot.com/p/blog-page.html]รับทำเว็บไซต์ ร้านค้าออนไลน์ ราคาถูก[/direct] #รับแก้ไขเว็บไซต์ #รับปรับแต่ง Open Cart #รับปรับแต่งเว็บไซต์ให้ไวติดจรวด #ขายเว็บไซต์เช็คราคาสินค้าแบบเทพๆ lnwprice.com

ayeweb

มีหลายตัวเลยครับ แต่ผมชอบโค้ดแคชเหมือนข้างบนเลย ใช้อยู่ นิ่งมาก

[direct=https://www.bitimer.in.th/]ข่าวไอที[/direct] ข่าวสารไอที แอพไอโฟน บริการอื่นๆ
[direct=https://page.line.me/gnm7628z]บริการ ตรวจหวย[/direct] อื่นๆ ทั่วไปสาระน่ารู้ ความรู้ต่างๆ
[direct=https://goo.gl/XQp91t]Host ไทย[/direct] เว็บผู้หญิงนะคะ แต่ผู้ชายก็เข้าได้ค่ะ
[direct=https://page.line.me/oer1981h]ตรวจหวย[/direct]
ตรวจหวย ผลสลากกินแบ่งรัฐบาล>>
[direct=https://goo.gl/H3JCzK]เช่าโฮสติ้ง Ruk-com[/direct]

xvlnw.com

ลองดูครับ
http://xvlnw.com/?s=.htaccess+cache

เพิ่มเติม:
ถ้าเช่า vps ก็ใช้พวก Nginx ได้ครับ
รับรองเลยว่าเบาเวอร์ (Static File) ><

ส่วน PHP ก็แคชเป็นไฟล์ html แทนได้ (Cache File)
แล้วก็ใช้งานร่วมกันกับ Nginx ในรูปแบบของ Static File แจ่มครับ
[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

vii

http://okvee.net/web-resources/download/okveecache-class

อันนี้ของผมทำไว้เป็น cache php ที่ใช้ได้กับข้อมูล php ทุกประเภท (ไม่ใช่รูปภาพ) เช่น string, int, array, object, ...

ตัวอย่างเวลาใช้
include( 'okvcache.php' );

// start new class
$cache = new okvcache();

if ( false === $val = $cache->get( 'string-cache' ) ) {
    $data = 'this is string text.';
    $val = $data;
    $cache->save( 'string-cache', $data, 70 );
} else {
    echo '<strong>cached:</strong> ';
}
echo $val;