首页  >  文章  >  后端开发  >  PHP的缓存你了解多少?深入探索PHP缓存原理及使用_PHP教程

PHP的缓存你了解多少?深入探索PHP缓存原理及使用_PHP教程

WBOY
WBOY原创
2016-07-13 17:39:53877浏览

  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 class="keyword">class</span><span> cache{    </span>
</li>
<li class="alt">
<span class="keyword">private</span><span> </span><span class="vars">$cache_dir</span><span>;    </span>
</li>
<li>
<span class="keyword">private</span><span> </span><span class="vars">$expireTime</span><span>=180;</span><span class="comment">//缓存的时间是 60 秒   </span><span> </span>
</li>
<li class="alt">
<span class="keyword">function</span><span> __construct(</span><span class="vars">$cache_dirname</span><span>){    </span>
</li>
<li>
<span> </span><span class="keyword">if</span><span>(!@</span><span class="func">is_dir</span><span>(</span><span class="vars">$cache_dirname</span><span>)){    </span>
</li>
<li class="alt">
<span>  </span><span class="keyword">if</span><span>(!@</span><span class="func">mkdir</span><span>(</span><span class="vars">$cache_dirname</span><span>,0777)){    </span>
</li>
<li>
<span>  </span><span class="vars">$this</span><span>->warn(</span><span class="string">缓存文件不存在而且不能创建,需要手动创建.</span><span>);    </span>
</li>
<li class="alt">
<span>  </span><span class="keyword">return</span><span> false;    </span>
</li>
<li><span>  }    </span></li>
<li class="alt"><span> }    </span></li>
<li>
<span class="vars">$this</span><span>->cache_dir = </span><span class="vars">$cache_dirname</span><span>;    </span>
</li>
<li class="alt"><span>}    </span></li>
<li>
<span class="keyword">function</span><span> __destruct(){    </span>
</li>
<li class="alt">
<span> </span><span class="func">echo</span><span> </span><span class="string">Cache class bye.</span><span>;    </span>
</li>
<li><span>}    </span></li>
<li class="alt"><span>   </span></li>
<li>
<span class="keyword">function</span><span> get_url() {    </span>
</li>
<li class="alt">
<span>        </span><span class="keyword">if</span><span> (!isset(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">REQUEST_URI</span><span>])) {    </span>
</li>
<li>
<span>                </span><span class="vars">$url</span><span> = </span><span class="vars">$_SERVER</span><span>[</span><span class="string">REQUEST_URI</span><span>];    </span>
</li>
<li class="alt">
<span>        }</span><span class="keyword">else</span><span>{    </span>
</li>
<li>
<span>                </span><span class="vars">$url</span><span> = </span><span class="vars">$_SERVER</span><span>[</span><span class="string">SCRIPT_NAME</span><span>];    </span>
</li>
<li class="alt">
<span>                </span><span class="vars">$url</span><span> .= (!</span><span class="func">empty</span><span class="keyword">empty</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 class="vars">$_SERVER</span><span>[</span><span class="string">QUERY_STRING</span><span>] : </span><span class="string"></span><span>;    </span>
</li>
<li><span>        }    </span></li>
<li class="alt"><span>   </span></li>
<li>
<span>        </span><span class="keyword">return</span><span> </span><span class="vars">$url</span><span>;    </span>
</li>
<li class="alt"><span>}   </span></li>
<li class="alt"><span><span>function warn($errorstring){  </span></span></li>
<li>
<span>echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">b</span><span class="tag">></span><span class="tag"><span class="tag-name">font</span></span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">color</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">red</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>发生错误:</span><strong><font color="#006699"><span class="tag"><span class="tag-name">pre</span><span class="tag">></span></span></font></strong><span>".$errorstring."</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">pre</span><span class="tag">></span><span class="tag"></span><span class="tag-name">font</span><span class="tag">></span><span class="tag"></span><span class="tag-name">b</span><span class="tag">></span></font></strong><span>";  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>function cache_page($pageurl,$pagedata){  </span></li>
<li>
<span> if(!$</span><span class="attribute"><font color="#ff0000">fso</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">fopen</font></span><span>($pageurl,w)){  </span>
</li>
<li class="alt">
<span>  $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>warns(无法打开缓存文件.);//trigger_error  </span>
</li>
<li><span>  return false;  </span></li>
<li class="alt"><span> }  </span></li>
<li><span> if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型锁定  </span></li>
<li class="alt">
<span>  $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>warns(无法锁定缓存文件.);//trigger_error  </span>
</li>
<li><span>  return false;  </span></li>
<li class="alt"><span> }  </span></li>
<li><span> if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式  </span></li>
<li class="alt">
<span>  $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>warns(无法写入缓存文件.);//trigger_error  </span>
</li>
<li><span>  return false;  </span></li>
<li class="alt"><span> }  </span></li>
<li><span> flock($fso,LOCK_UN);//释放锁定  </span></li>
<li class="alt"><span> fclose($fso);  </span></li>
<li><span> return true;  </span></li>
<li class="</p">
<p align="left"></p>
<div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486256.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486256.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">PHP由于它的强大和可伸缩性,近几年来得到长足的发展,PHP相比传统的ASP网站,在速度上有绝对的优势,想mssql转6万条数据PHP如需要40秒,ASP不下...</span>
</div>
<div class="art_confoot"></div>
</li>
</ol>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn