PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>PHP CLASS> class.URI_Cache.php

class.URI_Cache.php

FROM: AUTHOR: TIME:2008-04-06 HITS:
<?php
//
// Simple REQUEST_URI-based caching system
//   author: yinsee@gmail.com
// * require output buffering to be enabled
//
// example use:
// include("class.URI_Cache.php");
// $c = new URI_Cache(); // with just 1 line of code to enable auto caching.
// $c = new URI_Cache(10); // set duration to 10 seconds

class
URI_Cache
{
    var
$cache_dir = "/.cache_pages"
;
    var
$cache_duration = 30;  
// cache duration, in seconds

welcome to phpsu.com


    
var $cache_ext = ".html"
;
    var
$debug = 0
;
    var
$need_cache = true
;

    
//class constructor, take care of cache directory creation
    //if autorun set to false, call Load() manually to load from cache
    
function URI_Cache($dur=0,$dir='',$ext='',$load_and_exit=true
) welcome to phpsu.com
    {
        if (!
ob_list_handlers
())
        {
            if (
$this->debug
)
                print(
"Warning: Output buffering is not enabled. I am enabling it."
);
            
ob_start('ob_gzhandler'
); phpsu
        }
        
        if (
$dur) $this->cache_duration = $dur
;
        if (
$dir) $this->cache_dir = $dir
;
        if (
$ext) $this->cache_ext = $ext
;
http://www.phpsu.com

        
        
$this->cache_dir = $_SERVER['DOCUMENT_ROOT']."/".$this->cache_dir
;
        if (
$load_and_exit) $this->Load
();

        
// check if output buffering is enabled...

phpsu提供的php教程


        
if (!file_exists($this->cache_dir
))
        {
            
mkdir($this->cache_dir
);
            
chmod($this->cache_dir,0777
); phpsu提供的php教程
        }
    }
    
    function
__destruct
()
    {
        if (
$this->need_cache) return $this->CachePage
();
    }

    
// load cached content.
    // if exit is true, output cached content and exit() when load success
    // if exit is false, return cached content
    
function Load($exit=true
) do you kown phpsu.com?
    {
        
$cachefile = "$this->cache_dir/".md5($_SERVER['REQUEST_URI']).$this->cache_ext
;
        if (
file_exists($cachefile
))
        {
            if (
filemtime($cachefile) > time()-$this->cache_duration
) phpsu.com
            {
                
$this->need_cache = false
;
                if (
$exit
) {
                    print
file_get_contents($cachefile
);
                    if (
$this->debug) print "<br />This page is cached by interval of $this->cache_duration.<br />Now: ".time
();
www.phpsu.com

                    exit;
                }
                return
file_get_contents($cachefile
);
            }
            else
                
unlink($cachefile);
// delete expired cache
        
} welcome to phpsu.com
        return
false
;
    }

    
//  cache the current page
    
function CachePage
()
    {
        
$cachefile = "$this->cache_dir/".md5($_SERVER['REQUEST_URI']).$this->cache_ext
;

phpsu is a phpschool


        if (
$this->debug) print "<br />Cached: ".time
();
        
file_put_contents($cachefile, ob_get_contents
());
        if (
$this->debug) print "<br />Cache renewed\n"
;
    } www.phpsu.com
}

TITLE:class.URI_Cache.php
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:56:34 GMT-8 2008