php页面缓存

WBOY
WBOYOriginal
2016-07-29 08:51:021241Durchsuche

  这几天接触了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判断:若未过缓存期,则加载缓存文件,否则加载源文件。

php页面缓存

<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页面缓存

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

php页面缓存

<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页面缓存

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

php页面缓存

<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页面缓存

以上就介绍了php页面缓存,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
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