Cache website

Discussion in 'PHP' started by piluan, Jun 19, 2011.

  1. #1
    <?php

    class cache

    {

    var $cache_dir = './cache/';

    var $cache_time = 100000;// Thời gian để tạo cache mới

    var $caching = false;

    var $file = '';

    function cache()

    {

    $this->file = $this->cache_dir . urlencode( $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] );

    if ( file_exists ( $this->file ) && ( fileatime ( $this->file ) + $this->cache_time ) > time() )

    {

    $handle = fopen( $this->file , "r");

    do {

    $data = fread($handle, 8192);

    if (strlen($data) == 0) {

    break;

    }

    echo $data;

    } while (true);

    fclose($handle);

    exit();

    }

    else

    {

    $this->caching = true;

    ob_start();

    }

    }



    function close()

    {

    if ( $this->caching )

    {

    $data = ob_get_clean();

    echo $data;

    $fp = fopen( $this->file , 'w' );

    fwrite ( $fp , $data );

    fclose ( $fp );

    }

    }

    }

    ?>

    Link : http://www.congnghemoigialai.vn/thiet-ke-website-gia-re-gia-lai/thu-thuat-seo---danh-cho-webmaster/thu-vien-cachephp---cache-website.html
     
    piluan, Jun 19, 2011 IP
  2. MarPlo

    MarPlo Member

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    48
    #2
    Hy,
    I think that is better to use file_get_contents() to read the file, and file_put_contents() to write it, than fopen().
     
    MarPlo, Jun 19, 2011 IP
  3. Amator

    Amator Well-Known Member

    Messages:
    1,424
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    165
    #3
    What is your point/question ?
     
    Amator, Jun 19, 2011 IP