search
Homephp教程php手册对一个cache类的实际应用

cache

Class_Cache.php:
 
class cache
{
 
var $cacheDirectory;
 
var $cacheDuration=3600;
 
var $cacheFilename;
 
function cache($cacheDuration=3600,$cacheDirectory='./cache')
{
$this->cacheDuration = 0;
$this->cacheDirectory = '.';
$this->cacheFilename = '';
 
$this->updateCache($cacheDuration,$cacheDirectory);
}
 
function getCacheFilename()
{
return $this->cacheFilename;
}
 
function updateCache($cacheDuration=3600,$cacheFolder='./cache')
{
$this->cacheDuration = $cacheDuration;
$this->cacheDirectory = $cacheFolder;
$this->_makeCacheFolder();
}
 
function _makeCacheFolder()
{
/*if (!is_dir($this->cacheDirectory))
{
$temp = explode('/',$this->cacheDirectory);
$cur_dir = '';
for($i=0;$i{
$cur_dir .= $temp[$i].'/';
 
if (!is_dir($cur_dir))
{
if (@mkdir($cur_dir,777)&&($cur_dir!=getcwd()))
{
$this->_writeFile($cur_dir.'.htaccess','Deny from all');
$this->_writeFile($cur_dir.'index.html','');
}
}
}
}*/
if (!is_dir($this->cacheDirectory))
{
$cur_dir=$this->cacheDirectory;
//echo $cur_dir;
if (@mkdir($cur_dir,777))
{
$this->_writeFile($cur_dir.'.htaccess','Deny from all');
$this->_writeFile($cur_dir.'index.html','');
}
}
 
}
 
function _writeFile($filename,$contents)
{
if (!file_exists($filename))
{
$fp = @fopen($filename,'w');
if ($fp)
{
fputs($fp,$contents);
fclose($fp);
}
}else{
unlink($filename);
$fp = @fopen($filename,'w');
if ($fp)
{
fputs($fp,$contents);
fclose($fp);
}
}
}
 
function _setCacheFilename($contents)
{
//$this->cacheFilename = $this->cacheDirectory.'/'.md5($contents).'.txt';
 
/***********/
global $cache_file;
$this->cacheFilename = $this->cacheDirectory.'/'.$cache_file.'.txt';
/***********/
}
function returnCacheTime()
{
//return "asdfd";
$tim=filemtime($this->cacheFilename);
return date('Y年m月d日 H时i分s秒',$tim);
}
function inCache($contents,$sty='')
{
$this->_setCacheFilename($contents);
if($sty==1)
{
return file_exists($this->cacheFilename);
}else{
if(file_exists($this->cacheFilename))
{
$tim=filemtime($this->cacheFilename);
if((time()-$tim)>$this->cacheDuration)
{
return false;
}else{
return true;
}
}else{
return false;
}
}
}
 
function readCache()
{
$contents = '';
$fp = @fopen($this->cacheFilename,'r');
if ($fp)
{
while(!feof($fp)) 
$contents .= fread($fp,4096);
fclose($fp);
}
return $contents;
}
 
function saveInCache($contents,$filename='')
{
if (trim($filename)=='') $filename = $contents;
if ($this->inCache($filename,1))
{
if((time()-filemtime($this->cacheFilename))>$this->cacheDuration)
{
@unlink($this->cacheFilename);
}
}
$this->_writeFile($this->cacheFilename,$contents);
}
 
}
?>
cache.php:

 require_once("Class_Cache.php");?>

//---------页面缓存----------
$is_cache=1;//是否缓存
$cache_time=300;//缓存时间
if ((strstr($script_name,"/member/") == true) || (strstr($script_name,"/common/") == true))
$is_cache=0;
$cacheDirectory=$_SERVER['DOCUMENT_ROOT']."/cache/";
if($_SERVER['QUERY_STRING']=='')
$cache_file=$_SERVER['PHP_SELF'];
else
$cache_file=$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
if($_SERVER['PHP_SELF']=="/index.php")
$cache_file="___index.php";
$cache_file=preg_replace(array("/\//","/\?/"),array("",""),$cache_file);
//echo $cache_file;
 
if($is_cache==1)
{
$cache=new cache($cache_time,$cacheDirectory);
 
if($cache->incache($cache_file))
{
$output=$cache->readcache();
$CacheTime=$cache->returnCacheTime();
unset($cache);
//if( function_exists(return_execute_time()) )
$execute_time=return_execute_time();
$output=str_replace("",$execute_time."
缓存版本:".$CacheTime,$output);
print($output);
exit;
}else
ob_start();
 
}
function all_cache()
{
global $is_cache;
global $cache_file;
global $cache;
if($is_cache==1)
{
//这里是输出的内容
 
$output = ob_get_clean();
ob_end_clean(); 
$cache->saveInCache($output,$cache_file); 
$CacheTime=$cache->returnCacheTime();
unset($cache);
//if( function_exists(return_execute_time()) )
$execute_time=return_execute_time();
$output=str_replace("",$execute_time."
缓存版本:".$CacheTime,$output);
print($output);
//exit; 

}
?>
用法
在页面开头引用

 require("cache.php")?>
在页面最后加上

 all_cache();?>

实际应用http://www.scmetals.com
class_cache类 原贴:http://www.phpx.com/happy/thr83014.html
class_cache.php内容如下
 
class cache
{
    
    var $cacheDirectory;
    
    var $cacheDuration=3600;
    
    var $cacheFilename;
 
    function cache($cacheDuration=3600,$cacheDirectory='./cache')
    {
        $this->cacheDuration = 0;
        $this->cacheFilename = '';
        $this->cacheDirectory = '.';
        $this->updateCache($cacheDuration,$cacheDirectory);
    }
 
    function _makeCacheFolder()
    {
        if (!is_dir($this->cacheDirectory))
        {
            $temp = explode('/',$this->cacheDirectory);
            $cur_dir = '';
            for($i=0;$i            {
                $cur_dir .= $temp[$i].'/';
                if (!is_dir($cur_dir))
                {
                    if (@mkdir($cur_dir,777)&&($cur_dir!=getcwd()))
                    {
                         $this->_writeFile($cur_dir.'.htaccess','Deny from all');
                         $this->_writeFile($cur_dir.'index.html','');
                    }
                }
            }
        }
        
    }
 
    function getCacheFilename()
    {
        return $this->cacheFilename;
    }
 
     function _setCacheFilename($contents)
     {
        $this->cacheFilename = $this->cacheDirectory.'/'.md5($contents).'.txt';
     }
 
     function inCache($contents,$sty='')
     {
         $this->_setCacheFilename($contents);
        if($sty==1)
         {
            return file_exists($this->cacheFilename);
         }
         else
         {
            if(file_exists($this->cacheFilename))
             {
                $tim=filemtime($this->cacheFilename);
                if((time()-$tim)>$this->cacheDuration)
                 {
                    return false;
                 }
                 else
                 {
                    return true;
                 }
             }
             else
             {
                 return false;
             }
         }
     }
 
     function readCache()
     {
         $contents = '';
         $fp = @fopen($this->cacheFilename,'r');
        if ($fp)
        {
            while(!feof($fp)) $contents .= fread($fp,4096);
            fclose($fp);
        }
        return $contents;
     }
     
    function updateCache($cacheDuration=3600,$cacheFolder='./cache')
    {
        $this->cacheDuration = $cacheDuration;
        $this->cacheDirectory = $cacheFolder;
        $this->_makeCacheFolder();
    }
    
     function saveInCache($contents,$filename='')
     {
            if (trim($filename)=='') $filename = $contents;
            if ($this->inCache($filename,1))
            {
                if((time()-filemtime($this->cacheFilename))>$this->cacheDuration)
                {
                    @unlink($this->cacheFilename);
                }
            }
            $this->_writeFile($this->cacheFilename,$contents);
     }
 
     function _writeFile($filename,$contents)
     {
         if (!file_exists($filename))
         {
             $fp = @fopen($filename,'w');
             if ($fp)
             {
                fputs($fp,$contents);
                fclose($fp);
             }
         }
        else
         {
            unlink($filename);
            $fp = @fopen($filename,'w');
             if ($fp)
             {
                fputs($fp,$contents);
                fclose($fp);
             }
         }
     }
 
}
?>



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.