Home > php教程 > php手册 > PHP缓存原理及使用方法

PHP缓存原理及使用方法

WBOY
Release: 2016-06-21 08:52:41
Original
942 people have browsed it

PHP由于它的强大和可伸缩性,近几年来得到长足的发展,PHP相比传统的ASP网站,在速度上有绝对的 优势,想mssql转6万条数据PHP如需要40秒,ASP不下2分钟.但是,由于网站的数据越来越多,我们渴求能更快速的调用数据,不必要每次都从数据 库掉,我们可以从其他的地方,比方一个文件,或者某个内存地址,这就是PHP的缓存技术,也就是Cache技术。

分析深入
一般来说,缓存的目的是把数据放在一个地方让访问的更快点,毫无疑问,内存是最快的,但是,几百M的数据能往内存放么?这不现实,当然,有的时 候临时放如服务器缓存,如ob_start()这个缓存页面开启的话在发送文件头之前页面内容都被缓存在内存中,知道等页面输出自动清楚或者等待 ob_get_contents的返回,或者被ob_end_clean显示的清除,这在静态页面的生成中能很好的利用,在模板中能得到很好的体现。
另外,在ASP中有一对象application,可以保存公用的参数,这也算点缓存,但在PHP,我至今没看到开发者产出这种对象,的确,没 必要.ASP.NET的页面缓存技术就用的是viewstate,而cache就是文件关联,(不一定准确),文件被修改,更新缓存,文件没被修改而且不 超时(注释1),就读取缓存,返回结果,就是这个思路,看看这个源码:

<ol class="dp-c">
<li class="alt"><span><span><?PHP     </span></span></span></li>
<li><span>    <span class="keyword">class</span><span> cache{     </span></span></li>
<li class="alt"><span>    <span class="keyword">private</span><span> </span><span class="vars">$cache_dir</span><span>;     </span></span></li>
<li><span>    <span class="keyword">private</span><span> </span><span class="vars">$expireTime</span><span>=180;</span><span class="comment">//缓存的时间是 60 秒    </span><span> </span></span></li>
<li class="alt"><span>    <span class="keyword">function</span><span> __construct(</span><span class="vars">$cache_dirname</span><span>){     </span></span></li>
<li><span>     <span class="keyword">if</span><span>(!@</span><span class="func">is_dir</span><span>(</span><span class="vars">$cache_dirname</span><span>)){     </span></span></li>
<li class="alt"><span>      <span class="keyword">if</span><span>(!@</span><span class="func">mkdir</span><span>(</span><span class="vars">$cache_dirname</span><span>,0777)){     </span></span></li>
<li><span>      <span class="vars">$this</span><span>->warn(</span><span class="string">'缓存文件不存在而且不能创建,需要手动创建.'</span><span>);     </span></span></li>
<li class="alt"><span>      <span class="keyword">return</span><span> false;     </span></span></li>
<li><span>      }     </span></li>
<li class="alt"><span>     }     </span></li>
<li><span>    <span class="vars">$this</span><span>->cache_dir = </span><span class="vars">$cache_dirname</span><span>;     </span></span></li>
<li class="alt"><span>    }     </span></li>
<li><span>    <span class="keyword">function</span><span> __destruct(){     </span></span></li>
<li class="alt"><span>     <span class="func">echo</span><span> </span><span class="string">'Cache class bye.'</span><span>;     </span></span></li>
<li><span>    }     </span></li>
<li class="alt"><span>        </span></li>
<li><span>    <span class="keyword">function</span><span> get_url() {     </span></span></li>
<li class="alt"><span>            <span class="keyword">if</span><span> (!isset(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'REQUEST_URI'</span><span>])) {     </span></span></li>
<li><span>                    <span class="vars">$url</span><span> = </span><span class="vars">$_SERVER</span><span>[</span><span class="string">'REQUEST_URI'</span><span>];     </span></span></li>
<li class="alt"><span>            }<span class="keyword">else</span><span>{     </span></span></li>
<li><span>                    <span class="vars">$url</span><span> = </span><span class="vars">$_SERVER</span><span>[</span><span class="string">'SCRIPT_NAME'</span><span>];     </span></span></li>
<li class="alt"><span>                    <span class="vars">$url</span><span> .= (!emptyempty(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'QUERY_STRING'</span><span>])) ? </span><span class="string">'?'</span><span> . </span><span class="vars">$_SERVER</span><span>[</span><span class="string">'QUERY_STRING'</span><span>] : </span><span class="string">''</span><span>;     </span></span></li>
<li><span>            }     </span></li>
<li class="alt"><span>        </span></li>
<li><span>            <span class="keyword">return</span><span> </span><span class="vars">$url</span><span>;     </span></span></li>
<li class="alt"><span>    }    </span></li>
<li><span>    <span class="keyword">function</span><span> warn(</span><span class="vars">$errorstring</span><span>){   </span></span></li>
<li class="alt"><span>    <span class="func">echo</span><span> </span><span class="string">"<b><font>发生错误:<pre class="brush:php;toolbar:false">"
Copy after login
.$errorstring." ";   
  •     }   
  •       
  •     function cache_page($pageurl,$pagedata){   
  •      if(!$fso=fopen($pageurl,'w')){   
  •       $this->warns('无法打开缓存文件.');//trigger_error   
  •       return false;   
  •      }   
  •      if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型锁定   
  •       $this->warns('无法锁定缓存文件.');//trigger_error   
  •       return false;   
  •      }   
  •      if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式   
  •       $this->warns('无法写入缓存文件.');//trigger_error   
  •       return false;   
  •      }   
  •      flock($fso,LOCK_UN);//释放锁定   
  •      fclose($fso);   
  •      return true;   
  •     }   
  •       
  •     function display_cache($cacheFile){   
  •                 if(!file_exists($cacheFile)){   
  •         $this->warn('无法读取缓存文件.');//trigger_error   
  •         return false;   
  •                 }   
  •        echo '读取缓存文件:'.$cacheFile;   
  •     //return unserialize(file_get_contents($cacheFile));   
  •             $fso = fopen($cacheFile'r');   
  •             $data = fread($fsofilesize($cacheFile));   
  •             fclose($fso);   
  •      return $data;   
  •     }   
  •       
  •     function readData($cacheFile='default_cache.txt'){   
  •      $cacheFile = $this->cache_dir."/".$cacheFile;   
  •      if(file_exists($cacheFile)&&filemtime($cacheFile)>(time()-$this->expireTime)){   
  •       $data=$this->display_cache($cacheFile);   
  •       }else{   
  •        $data="from here wo can get it from mysql database,update time is ".date('l dS \of F Y h:i:s A').",过期时间是:".date('l dS \of F Y h:i:s A',time()+$this->expireTime)."----------";   
  •        $this->cache_page($cacheFile,$data);   
  •      }   
  •       return $data;   
  •     }   
  •     }   
  •     ?>  


  • Related labels:
    source:php.cn
    Statement of this Website
    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
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template