Home  >  Article  >  Backend Development  >  php page cache

php page cache

WBOY
WBOYOriginal
2016-07-29 08:51:021200browse

I have been exposed to the page caching of phpcms in the past few days and have some feelings. I won’t go into details about its benefits. It is generally used in pages with a lot of database queries. It is not suitable for pages that are inserted, modified and deleted.

 Here is a brief introduction to caching technology: http://www.cnblogs.com/penghcn/articles/2720202.html

 PHP page caching mainly uses the ob series of functions, such as ob_start(), ob_end_flush(), ob_get_contents()

 The following is the encoding part.

 1. Initialization function, usually setting the page cache path, cache file naming format, etc., which can be customized according to personal preferences. The identification ID used here is the encrypted $_SERVER[REQUEST_URI] parameter. There is also an if judgment at the end of this function: if the cache period has not passed, load the cache file, otherwise load the source file.

php page cache

<span> 1</span><span>function</span><span> page_init()
</span><span> 2</span><span>{    
</span><span> 3</span><span>$url</span> = <span>$_SERVER</span>['REQUEST_URI'];<span>//</span><span>子url,该参数一般是唯一的</span><span> 4</span><span>$pageid</span> = <span>md5</span>(<span>$url</span><span>);
</span><span> 5</span><span>$dir</span> = <span>str_replace</span>('/','_',<span>substr</span>(<span>$_SERVER</span>['SCRIPT_NAME'],1,-4<span>));
</span><span> 6</span><span>//</span><span>目录命名方式,如exp_index</span><span> 7</span><span>if</span>(!<span>file_exists</span>(<span>$pd</span> = PAGE_PATH.<span>$dir</span>.'/'))@<span>mkdir</span>(<span>$pd</span>,0777) or <span>die</span>("<span>$pd目录创建失败</span>"<span>);
</span><span> 8</span><span>//</span><span>如cache/page/exp_index/</span><span> 9</span><span>define</span>('PAGE_FILE',<span>$pd</span>.<span>$pageid</span>.'.html'<span>);
</span><span>10</span><span>//</span><span>如cache/page/exp_index/cc8ef22b405566745ed21305dd248f0e.html</span><span>11</span><span>$contents</span> = <span>file_get_contents</span>(PAGE_FILE);<span>//</span><span>读出</span><span>12</span><span>13</span><span>if</span>(<span>$contents</span> && <span>substr</span>(<span>$contents</span>, 13, 10) ><span> time() )<span>//</span><span>对应page_cache()函数中加上的自定义头部</span></span><span>14</span>     {
<span>15</span><span>echo</span><span>substr</span>(<span>$contents</span>, 27<span>);
</span><span>16</span><span>exit</span>(0<span>);
</span><span>17</span><span>    }
</span><span>18</span><span>return</span><span>true</span><span>;    
</span><span>19</span> }        

php page cache

  2. Page caching function, a trick is used here: add a header information - expiration time - to the content of the cache file, so each time you only need to Comparing the expiration time in the header with the current time (done in the page_init() function) can determine whether the cache has expired.

php page cache

<span>1</span><span>function</span> page_cache(<span>$ttl</span> = 0<span>)
</span><span>2</span><span>{    
</span><span>3</span><span>$ttl</span> = <span>$ttl</span> ? <span>$ttl</span> : PAGE_TTL;<span>//</span><span>缓存时间,默认3600s</span><span>4</span><span>$contents</span> = <span>ob_get_contents</span>();<span>//</span><span>从缓存中获取内容</span><span>5</span><span>$contents</span> = "<!--page_ttl:".(time() + <span>$ttl</span>)."-->\n".<span>$contents</span><span>;
</span><span>6</span><span>//</span><span>加上自定义头部:过期时间=生成时间+缓存时间</span><span>7</span><span>file_put_contents</span>(PAGE_FILE, <span>$contents</span>);<span>//</span><span>写入缓存文件中</span><span>8</span><span>ob_end_flush</span>();<span>//</span><span>释放缓存</span><span>9</span> }    

php page cache

   3. Function usage, pay attention to the execution order of these two functions, and don’t forget ob_start()

php page cache

<span>1</span> <span>php
</span><span>2</span>      page_init();<span>//</span><span>页面缓存初始化</span><span>3</span><span>ob_start</span>();<span>//</span><span>开启缓存        </span><span>4</span><span>5</span>      ...<span>//</span><span>代码段</span><span>6</span><span>7</span>      page_cache(60);<span>//</span><span>一般是最后一行</span><span>8</span><span>9</span>  ?>

php page cache

The above has introduced PHP page caching, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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