ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
เขียนโปรแกรมบน iPhone ด้วย MonoTouch
News
Php Tips
Ubuntu
Spring+Strut+Hibernate
Android Programming
Design Pattern By PHP
C# Design Pattern
Linux Quick Tips
C# Tips & Technique
C# using Linq น่าใช้จริงๆ
Java & JavaScript Tips
MAVEN
Database & SQL
ZengCode Framework Guide
Mac OSx
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     ZengCache มาสร้าง Caching ให้เว็บเพจไวเหมือนติดจรวดกัน  (2010-07-28)

ผมได้สร้าง Simple Cache Page ขึ้นมาใช้งานลองเอาไปใช้ หรือเอาไปแก้ไขดัดแปลงใช้ได้นะครับ
เหมาะสำหรับ หน้า page ที่ไม่ค่อยมีการอัพเดทเท่าไหร่เพื่อลด Connection กับ Database
เราสามาราถ set expiration ของ cache file ได้ (หน่วยเป็นนาทีครับ)
และเมื่อมีการ post ค่าข้อมูล cache ก็จะหมดอายุโดยอัตโนมัติ นำไปใช้กับพวก กระดานข่าวอะไรได้ครับ
สำหรับ Cache File ผมจะเก็บเป็น Text File นะครับ
สำหรับ Version หน้าผมจะสร้าง DB Cache ด้วยครับ คอยติดตามกันต่อไปนะครับ

 <?php
    /*************************************
    Chiwa Kantawong
    http://www.zengcode.com
    p@zengcode.com
    20 July 2010
    *************************************/
header('Content-type: text/html; charset=utf-8');
Class ZengCache
{
    public static $cachePath;
    public static $cacheTime = 5; //seconds
    public static $cacheName;
    public static $showLoadTime = false;
    
    public static function StartCache($path,$time=null)
    {
         if ( ZengCache::$showLoadTime == true )
            {
              $starttime = explode(' ', microtime());  
              $starttime =  $starttime[1] + $starttime[0];
            }
         ob_start();
         if ($_POST) return;
         if ($path == null || $path == "")
             die('!! Cache need save path !!');
         else
             ZengCache::$cachePath = $path;

         if ($time != null) ZengCache::$cacheTime = $time;
         ZengCache::$cacheName = md5($_SERVER['PHP_SELF']) ;

        $cachetime = ZengCache::$cacheTime * 60;
        if (file_exists( ZengCache::$cachePath.'\\'.ZengCache::$cacheName) &&
        (time() - $cachetime < filemtime( ZengCache::$cachePath.'\\'.ZengCache::$cacheName)))
        {
            include( ZengCache::$cachePath.'\\'.ZengCache::$cacheName);
            echo "\n<!-- From cache generated =>".ZengCache::$cacheName."-->";
            //Load time
             if ( ZengCache::$showLoadTime == true )
                {
                    $mtime = explode(' ', microtime());  
                    $totaltime = $mtime[0] +  $mtime[1] - $starttime;  
                     printf('<br>Page loaded in %.3f seconds.',  $totaltime);
                }
            ob_end_flush();    
            exit;
        }
    }//

    public static function EndCache()
    {
        $fp = fopen( ZengCache::$cachePath.'\\'.ZengCache::$cacheName, 'w');
        if (flock($fp, LOCK_EX)) {
            fwrite($fp, ob_get_contents());
            flock($fp, LOCK_UN);
        }
        fclose($fp);
        ob_end_flush();    
        //Load time
         if ( ZengCache::$showLoadTime == true )
            {
               $mtime = explode(' ', microtime());  
               $totaltime = $mtime[0] +  $mtime[1] - $starttime;  
               printf('<br>Page loaded in %.3f seconds.',  $totaltime);
            }
    }//
}//
?>


<?php
//use ZengCache
ZengCache::$showLoadTime = true;  
ZengCache::StartCache("C:\\ZengCache",1); //param1=path of cache file, param2= expiration time of cache file (minutes) รอ 1     นาทีดู load time ครับจะแตกต่างกัน

 

//======================ส่วนการทำงาน ปกติ ===============//
      for($i=1; $i<=5;$i++)
      {
          sleep(1); //sleep 1 วินาที สมมิตว่าตรงนี้เป็นส่วนที่ติดต่อ กับ database หรือใช้เวลาคำนวณมากๆ
         echo "\n<br> Zeng ครั้งที่ : ".$i;
      }
//======================ส่วนการทำงาน ปกติ ===============//


ZengCache::EndCache();
?>

สำหรับตัวอย่างนี้ผม Set Expiration เป็น 1 นาที ลองกด refresh ดูนะครับ จะเห็นความแตกต่าง
ของ Load Time

Download

 


Comment
Name
Comment
Security CodeCAPTCHA Image

web hit counter

This page took 0.047157 seconds to load.