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">".$errorstring." ";