Home  >  Article  >  Backend Development  >  How much do you know about PHP caching? In-depth exploration of PHP caching principles and usage_PHP tutorial

How much do you know about PHP caching? In-depth exploration of PHP caching principles and usage_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:39:53833browse

Due to its power and scalability, PHP has made great progress in recent years. Compared with traditional ASP websites, PHP has an absolute advantage in speed. If you want to transfer 60,000 pieces of data from mssql to PHP, it takes 40 seconds. ASP takes no less than 2 minutes. However, as the website has more and more data, we are eager to call the data faster. We do not need to delete it from the database every time. We can call it from other places, such as a file or a certain memory address. , This is PHP’s caching technology, which is Cache technology.

In-depth analysis

Generally speaking, the purpose of caching is to put data in one place to make access faster. There is no doubt that memory is the fastest, but can hundreds of M of data be stored in it? This is unrealistic , Of course, sometimes it is temporarily placed in the server cache. For example, if the ob_start() cache page is turned on, the page content will be cached in the memory before sending the file header. Until the page output is automatically cleared, or waiting for the return of ob_get_contents, or ob_end_clean Display clearing can be well used in the generation of static pages and can be well reflected in templates.

In addition, there is an object application in ASP that can save public parameters. This is also a bit of caching. But in PHP, I have not seen developers produce such objects so far. Indeed, it is not necessary. ASP.NET Page caching technology uses viewstate, and cache is file association (not necessarily accurate). When the file is modified, the cache is updated. If the file is not modified and does not time out (note 1), the cache is read and the result is returned, which is this For ideas, take a look at this source code:

<ol class="dp-c">
<li class="alt"><span><span><?PHP </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>; 🎜>$expireTime</span></li>=180;<li><span class="keyword">//The cache time is 60 seconds </span><span> </span><span class="vars"></span><span>function</span><span class="comment"> __construct(</span><span>$cache_dirname</span></li>){ <li class="alt"><span class="keyword"></span><span> </span><span class="vars">if</span><span>(!@</span></li>is_dir<li><span>(</span><span class="keyword">$cache_dirname</span><span>)){ ></span>(!@<span class="func"></span>mkdir<span></span>(<span class="vars"></span>$cache_dirname<span></span>,0777)){ </li><li class="alt"><span></span> <span class="keyword"></span>$this<span></span>->warn(<span class="func"></span>The cache file does not exist and cannot be created, it needs to be created manually.<span></span>); <span class="vars"> </span><span></span> </span></span></li>
<li>return<span></span> false; <span class="vars"></span><span></span> } <span class="string"></span><span></span>$this</li>
<li class="alt">->cache_dir = <span></span>$cache_dirname<span class="keyword"></span>; <span></span>
</li>
<li>} <span></span>
</li>
<li class="alt">function<span></span> __destruct(){ </li>
<li>
<span class="vars"></span> <span></span>echo<span class="vars"></span> <span></span>Cache class bye.</li>
<li class="alt">; <span></span>
</li>
<li>} 🎜><span class="keyword">function</span><span> get_url() { </span>
</li>
<li class="alt">
<span> >$_SERVER</span><span class="func">[</span><span>REQUEST_URI</span><span class="string">])) { </span><span></span>
</li> <li> = <span></span>$_SERVER</li>
<li class="alt">[<span></span>REQUEST_URI</li>
<li>]; <span class="keyword">else</span><span>{                                                                         🎜>[</span>
</li>SCRIPT_NAME<li class="alt">
<span>]; ></span>empty<span class="keyword"></span>empty<span></span>(<span class="vars"></span>$_SERVER<span></span>[<span class="string"></span>QUERY_STRING<span></span>])) ?</li>
<li>?<span></span> . ><span class="vars"></span>; <span></span><span class="vars"></span>                                     🎜 >return<span></span> <span class="string"></span>$url<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><span class="tag-name">b</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">font</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">></span></font></strong><span>An error occurred:</span><strong><font color="#006699"><span class="tag"><</span><span class="tag-name">pre</span><span class="tag">></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(unable Open the cache file.);//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, exclusive lock </span></li>
<li class="alt">
<span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>warns(Unable to lock cache file.);//trigger_error </span>
</li>
<li><span> return false; </span></li>
<li class="alt"><span> } </span></li>
<li><span> if(!fwrite($fso,$pagedata)){//Write byte stream, serialize to write other formats </span></li>
<li class="alt">
<span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>warns(Unable to write cache file.);//trigger_error </span>
</li>
<li><span> return false; </span></li>
<li class="alt"><span> } </span></li>
<li><span> flock($fso,LOCK_UN);//Release lock </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 has made great progress in recent years due to its power and scalability. Compared with traditional ASP, PHP The website has an absolute advantage in terms of speed. If you want to transfer 60,000 pieces of data from mssql to PHP, it will take 40 seconds, and ASP will not be faster...</span>
</div>
<div class="art_confoot"></div>
</li>
</ol>
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