Heim php教程 php手册 php页面缓存 - jerrylsxu

php页面缓存 - jerrylsxu

May 20, 2016 am 10:14 AM

  这几天接触了phpcms的页面缓存,有些感触。其好处就不多说了,它一般是用在数据库查询较多的页面中,对于插入修改删除的页面就不大合适了。

  这里有缓存技术的简单介绍:http://www.cnblogs.com/penghcn/articles/2720202.html

  php页面缓存主要用到的是ob系列函数,如ob_start(),ob_end_flush(),ob_get_contents()

  下面是编码部分。

  1.初始化函数,一般是设置页面缓存路径、缓存文件命名格式等,可按个人喜好自定义。这里用到的识别ID是经加密的$_SERVER[REQUEST_URI]参数。这个函数中最后还有一个if判断:若未过缓存期,则加载缓存文件,否则加载源文件。

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

  2.页面缓存函数,这里使用到一个技巧:在缓存文件的内容中加上一个头部信息--过期时间,所以每次只需要对头部中的过期时间和当前时间进行比较(在page_init()函数中进行)就能判断缓存是否过期了。

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

  3.函数使用,注意这两个函数有先后执行顺序,还有别忘了ob_start()

复制代码
<span style="color: #008080;">1</span> <span style="color: #000000;">php
</span><span style="color: #008080;">2</span>      page_init();<span style="color: #008000;">//</span><span style="color: #008000;">页面缓存初始化</span>
<span style="color: #008080;">3</span>      <span style="color: #008080;">ob_start</span>();<span style="color: #008000;">//</span><span style="color: #008000;">开启缓存        </span>
<span style="color: #008080;">4</span>  
<span style="color: #008080;">5</span>      ...<span style="color: #008000;">//</span><span style="color: #008000;">代码段</span>
<span style="color: #008080;">6</span>  
<span style="color: #008080;">7</span>      page_cache(60);<span style="color: #008000;">//</span><span style="color: #008000;">一般是最后一行</span>
<span style="color: #008080;">8</span>  
<span style="color: #008080;">9</span>  ?>
Nach dem Login kopieren
复制代码
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)